24 protected $default_intervals = [
25 'minute' =>
'* * * * *',
26 'fiveminute' =>
'*/5 * * * *',
27 'fifteenmin' =>
'*/15 * * * *',
28 'halfhour' =>
'*/30 * * * *',
29 'hourly' =>
'0 * * * *',
30 'daily' =>
'0 0 * * *',
31 'weekly' =>
'0 0 * * 0',
32 'monthly' =>
'0 0 1 * *',
33 'yearly' =>
'0 0 1 1 *',
47 $this->events = $events;
59 public function run(array $intervals = null, $force =
false) {
61 if (!isset($intervals)) {
62 $intervals = array_keys($this->default_intervals);
65 $allowed_intervals = $this->getConfiguredIntervals();
67 $scheduler =
new Scheduler();
70 foreach ($intervals as $interval) {
71 if (!array_key_exists($interval, $allowed_intervals)) {
72 throw new CronException(
"$interval is not a recognized cron interval");
75 $cron_interval = $force ? $allowed_intervals[
'minute'] : $allowed_intervals[$interval];
78 ->call(
function () use ($interval,
$time) {
79 return $this->execute($interval,
$time);
82 ->before(
function () use ($interval,
$time) {
83 $this->before($interval,
$time);
85 ->then(
function (
$output) use ($interval) {
86 $this->after(
$output, $interval);
90 return $scheduler->run(
$time);
107 $this->events->triggerBefore(
'cron', $interval,
$time);
110 set_time_limit((
int) ini_get(
'max_execution_time'));
112 $now = new \DateTime();
114 $msg =
elgg_echo(
'admin:cron:started', [$interval,
$time->format(DATE_RFC2822)]) . PHP_EOL;
115 $msg .=
elgg_echo(
'admin:cron:started:actual', [$interval, $now->format(DATE_RFC2822)]) . PHP_EOL;
117 $this->cronLog(
'output', $interval, $msg);
134 $now = new \DateTime();
139 $output[] =
elgg_echo(
'admin:cron:started:actual', [$interval, $now->format(DATE_RFC2822)]);
143 $old_stdout = $this->events->triggerResults(
'cron', $interval, [
144 'time' =>
$time->getTimestamp(),
148 $output[] = ob_get_clean();
149 $output[] = $old_stdout;
151 $now = new \DateTime();
153 $output[] =
elgg_echo(
'admin:cron:complete', [$interval, $now->format(DATE_RFC2822)]);
155 return implode(PHP_EOL, array_filter($output));
168 $time = new \DateTime();
170 $this->cronLog(
'output', $interval,
$output);
171 $this->cronLog(
'completion', $interval,
$time->getTimestamp());
175 $this->events->triggerAfter(
'cron', $interval,
$time);
187 protected function cronLog($setting, $interval, $msg =
'') {
188 $suffix = $setting ?:
'output';
190 $fh = new \ElggFile();
192 $fh->setFilename(
"{$interval}-{$suffix}.log");
207 public function getLog($setting, $interval) {
208 $suffix = $setting ?:
'output';
210 $fh = new \ElggFile();
212 $fh->setFilename(
"{$interval}-{$suffix}.log");
214 if (!$fh->exists()) {
235 $result = $this->events->triggerResults(
'cron:intervals',
'system', [], $this->default_intervals);
237 $this->
getLogger()->warning(
"The event 'cron:intervals', 'system' should return an array, " . gettype(
$result) .
' given');
239 $result = $this->default_intervals;
getConfiguredIntervals(bool $only_names=false)
Get the cron interval configuration.
before($interval,\DateTime $time=null)
Execute commands before cron interval is run.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
if(!$annotation instanceof ElggAnnotation) $time
trait TimeUsing
Adds methods for setting the current time (for testing)
getCurrentTime($modifier= '')
Get the (cloned) time.
trait Loggable
Enables adding a logger.
execute($interval,\DateTime $time=null)
Execute handlers attached to a specific cron interval.
after($output, $interval)
Printers handler result.
elgg_get_site_entity()
Get the current site entity.
getLogger()
Returns logger.
getLog($setting, $interval)
Get the log contents of a cron interval.
A generic parent class for cron exceptions.
run(array $intervals=null, $force=false)
Executes handlers for periods that have elapsed since last cron.
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
cronLog($setting, $interval, $msg= '')
Log the results of a cron interval.
__construct(EventsService $events)
Constructor.