Skip to content

Commit

Permalink
fix(cron): the event param 'dt' can no longer be modified by callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Dec 20, 2024
1 parent c226b84 commit 8432e77
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions engine/classes/Elgg/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,28 @@ public function run(array $intervals = null, bool $force = false): array {

$scheduler = new Scheduler();
$time = $this->getCurrentTime();
$immutable = \DateTimeImmutable::createFromInterface($time);

foreach ($intervals as $interval) {
if (!array_key_exists($interval, $allowed_intervals)) {
throw new CronException("{$interval} is not a recognized cron interval. Please use one of the following: " . implode(', ', array_keys($allowed_intervals)));
}

$cron_interval = $force ? $allowed_intervals['minute'] : $allowed_intervals[$interval];
$filename = $this->getLogFilename($interval, $time);
$filename = $this->getLogFilename($interval, $immutable);

$cron_logger = \Elgg\Logger\Cron::factory([
'interval' => $interval,
'filename' => $filename,
]);

$scheduler
->call(function () use ($interval, $time, $cron_logger, $filename) {
return $this->execute($interval, $cron_logger, $filename, $time);
->call(function () use ($interval, $immutable, $cron_logger, $filename) {
return $this->execute($interval, $cron_logger, $filename, $immutable);
})
->at($cron_interval)
->before(function () use ($interval, $time, $cron_logger) {
$this->before($interval, $cron_logger, $time);
->before(function () use ($interval, $immutable, $cron_logger) {
$this->before($interval, $cron_logger, $immutable);
})
->then(function ($output) use ($interval, $cron_logger) {
$this->after($output, $interval, $cron_logger);
Expand All @@ -94,17 +95,13 @@ public function run(array $intervals = null, bool $force = false): array {
/**
* Execute commands before cron interval is run
*
* @param string $interval Interval name
* @param \Elgg\Logger\Cron $cron_logger Cron logger
* @param null|\DateTime $time Time of the cron initialization (default: current service time)
* @param string $interval Interval name
* @param \Elgg\Logger\Cron $cron_logger Cron logger
* @param \DateTimeImmutable $time Time of the cron initialization
*
* @return void
*/
protected function before(string $interval, \Elgg\Logger\Cron $cron_logger, \DateTime $time = null): void {
if (!isset($time)) {
$time = $this->getCurrentTime();
}

protected function before(string $interval, \Elgg\Logger\Cron $cron_logger, \DateTimeImmutable $time): void {
try {
$this->events->triggerBefore('cron', $interval, $time);
} catch (\Throwable $t) {
Expand All @@ -123,18 +120,14 @@ protected function before(string $interval, \Elgg\Logger\Cron $cron_logger, \Dat
/**
* Execute handlers attached to a specific cron interval
*
* @param string $interval Cron interval to execute
* @param \Elgg\Logger\Cron $cron_logger Cron logger
* @param string $filename Filename of the cron log
* @param null|\DateTime $time Time of cron initialization (default: current service time)
* @param string $interval Cron interval to execute
* @param \Elgg\Logger\Cron $cron_logger Cron logger
* @param string $filename Filename of the cron log
* @param \DateTimeImmutable $time Time of cron initialization
*
* @return string
*/
protected function execute(string $interval, \Elgg\Logger\Cron $cron_logger, string $filename, \DateTime $time = null): string {
if (!isset($time)) {
$time = $this->getCurrentTime();
}

protected function execute(string $interval, \Elgg\Logger\Cron $cron_logger, string $filename, \DateTimeImmutable $time): string {
try {
$begin_callback = function (array $params) use ($cron_logger) {
$readable_callable = (string) elgg_extract('readable_callable', $params);
Expand Down Expand Up @@ -297,16 +290,12 @@ public function getConfiguredIntervals(bool $only_names = false): array {
/**
* Get a filename to log in
*
* @param string $interval cron interval to log
* @param \DateTime|null $time start time of the cron
* @param string $interval cron interval to log
* @param \DateTimeInterface $time start time of the cron
*
* @return string
*/
protected function getLogFilename(string $interval, \DateTime $time = null): string {
if (!isset($time)) {
$time = $this->getCurrentTime();
}

protected function getLogFilename(string $interval, \DateTimeInterface $time): string {
$date = $time->format(\DateTimeInterface::ATOM);
$date = str_replace('+', 'p', $date);
$date = preg_replace('/[^a-zA-Z0-9_-]+/', '-', $date);
Expand Down

0 comments on commit 8432e77

Please sign in to comment.