20 'minute' =>
'* * * * *',
21 'fiveminute' =>
'*/5 * * * *',
22 'fifteenmin' =>
'*/15 * * * *',
23 'halfhour' =>
'*/30 * * * *',
24 'hourly' =>
'0 * * * *',
25 'daily' =>
'0 0 * * *',
26 'weekly' =>
'0 0 * * 0',
27 'monthly' =>
'0 0 1 * *',
28 'yearly' =>
'0 0 1 1 *',
49 $this->hooks = $hooks;
50 $this->logger = $logger;
51 $this->events = $events;
63 public function run(array $intervals = null, $force =
false) {
65 if (!isset($intervals)) {
66 $intervals = array_keys(self::$intervals);
69 $scheduler =
new Scheduler();
73 foreach ($intervals as $interval) {
74 if (!array_key_exists($interval, self::$intervals)) {
75 throw new \CronException(
"$interval is not a recognized cron interval");
78 $cron_interval = $force ? self::$intervals[
'minute'] : self::$intervals[$interval];
81 ->call(
function () use ($interval,
$time) {
82 return $this->execute($interval,
$time);
85 ->before(
function () use ($interval,
$time) {
86 $this->before($interval,
$time);
88 ->then(
function (
$output) use ($interval) {
89 $this->after(
$output, $interval);
93 return $scheduler->run(
$time);
110 $this->events->triggerBefore(
'cron', $interval,
$time);
113 set_time_limit((
int) ini_get(
'max_execution_time'));
117 $msg =
elgg_echo(
'admin:cron:started', [$interval,
$time->format(DATE_RFC2822)]) . PHP_EOL;
118 $msg .=
elgg_echo(
'admin:cron:started:actual', [$interval, $now->format(DATE_RFC2822)]) . PHP_EOL;
120 $this->
log(
'output', $interval, $msg);
142 $output[] =
elgg_echo(
'admin:cron:started:actual', [$interval, $now->format(DATE_RFC2822)]);
146 $old_stdout = $this->hooks->trigger(
'cron', $interval, [
147 'time' =>
$time->getTimestamp(),
151 $output[] = ob_get_clean();
152 $output[] = $old_stdout;
156 $output[] =
elgg_echo(
'admin:cron:complete', [$interval, $now->format(DATE_RFC2822)]);
158 return implode(PHP_EOL, array_filter($output));
174 $this->
log(
'completion', $interval,
$time->getTimestamp());
178 $this->events->triggerAfter(
'cron', $interval,
$time);
190 protected function log($setting, $interval, $msg =
'') {
191 $suffix = $setting ? :
'output';
193 $fh = new \ElggFile();
195 $fh->setFilename(
"{$interval}-{$suffix}.log");
210 public function getLog($setting, $interval) {
211 $suffix = $setting ? :
'output';
213 $fh = new \ElggFile();
215 $fh->setFilename(
"{$interval}-{$suffix}.log");
217 if (!$fh->exists()) {
log($msg, $level=LogLevel::NOTICE)
Log a message.
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($header)) if(!empty($body)) $contents
trait Loggable
Enables adding a logger.
getCurrentTime($modifier= '')
Get the (cloned) time.
catch(LoginException $e) if($request->isXhr()) $output
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
execute($interval, DateTime $time=null)
Execute handlers attached to a specific cron interval.
if(!$entity instanceof ElggEntity) $time
__construct(PluginHooksService $hooks, Logger $logger, EventsService $events)
Constructor.
log($setting, $interval, $msg= '')
Log the results of a cron interval.
after($output, $interval)
Printers handler result.
elgg_get_site_entity()
Get the current site entity.
before($interval, DateTime $time=null)
Execute commands before cron interval is run.
getLog($setting, $interval)
Get the log contents of a cron interval.
trait TimeUsing
Adds methods for setting the current time (for testing)
run(array $intervals=null, $force=false)
Executes handlers for periods that have elapsed since last cron.