|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.baseclasses |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the base class used for all jobs run by MidCOM CRON. |
|
13
|
|
|
* |
|
14
|
|
|
* It gives you an easy to use way of building cron jobs. You should rely only on |
|
15
|
|
|
* the two event handlers _on_initialize and execute, which are called by the |
|
16
|
|
|
* cron service. |
|
17
|
|
|
* |
|
18
|
|
|
* A simple (and useless) handler class would look like this: |
|
19
|
|
|
* |
|
20
|
|
|
* <code> |
|
21
|
|
|
* <?php |
|
22
|
|
|
* class net_nehmer_static_cron_test extends midcom_baseclasses_components_cron_handler |
|
23
|
|
|
* { |
|
24
|
|
|
* public function execute() |
|
25
|
|
|
* { |
|
26
|
|
|
* $this->print_error("Executing..."); |
|
27
|
|
|
* $this->print_error(strftime('%x %X')); |
|
28
|
|
|
* } |
|
29
|
|
|
* } |
|
30
|
|
|
* </code> |
|
31
|
|
|
* |
|
32
|
|
|
* <b>Cron Job implementation suggestions</b> |
|
33
|
|
|
* |
|
34
|
|
|
* You should keep output to stdout to an absolute minimum. Normally, no output whatsoever |
|
35
|
|
|
* should be made, as the cron service itself is invoked using some kind of Cron Daemon. Only |
|
36
|
|
|
* if you output nothing, no status mail will be generated by cron. |
|
37
|
|
|
* |
|
38
|
|
|
* @see midcom_services_cron |
|
39
|
|
|
* @package midcom.baseclasses |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract class midcom_baseclasses_components_cron_handler |
|
42
|
|
|
{ |
|
43
|
1 |
|
use midcom_baseclasses_components_base; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var OutputInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $output; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initialize the cron job. Before calling the on_initialize callback, it prepares |
|
52
|
|
|
* the instance with various configuration variables |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function initialize(OutputInterface $output) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->output = $output; |
|
57
|
|
|
|
|
58
|
1 |
|
return $this->_on_initialize(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* This callback is executed immediately after object construction. You can initialize your class here. |
|
63
|
|
|
* If you return false here, the handler is not executed, the system skips it. |
|
64
|
|
|
* |
|
65
|
|
|
* All class members are already initialized when this event handler is called. |
|
66
|
|
|
* |
|
67
|
|
|
* @return boolean Returns true, if initialization was successful, false to abort execution |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function _on_initialize() : bool |
|
70
|
|
|
{ |
|
71
|
1 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* This is the actual handler operation, it is called only after successful operation. |
|
76
|
|
|
* You should use the print_error() helper of this class in case you need to notify |
|
77
|
|
|
* the user of any errors. As long as everything goes fine, you should not print anything |
|
78
|
|
|
* to avoid needless cron mailings. |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract public function execute(); |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Echo the error message to the client, automatically appending |
|
84
|
|
|
* the classname to the prefix. Passed messages are also written to the error log. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function print_error(string $message, $var = null) |
|
87
|
|
|
{ |
|
88
|
|
|
$prefix = 'ERROR'; |
|
89
|
|
|
if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
|
90
|
|
|
$prefix .= ' ' . get_class($this); |
|
91
|
|
|
} |
|
92
|
|
|
$this->output->writeln("$prefix: $message"); |
|
93
|
|
|
debug_add($message, MIDCOM_LOG_ERROR); |
|
94
|
|
|
if ($var !== null) { |
|
95
|
|
|
debug_print_r('Passed argument: ', $var); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|