Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 0 additions & 170 deletions administrator/components/com_scheduler/src/Helper/ExecRuleHelper.php

This file was deleted.

11 changes: 0 additions & 11 deletions administrator/components/com_scheduler/src/Model/TaskModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Table\Table;
use Joomla\Component\Scheduler\Administrator\Helper\ExecRuleHelper;
use Joomla\Component\Scheduler\Administrator\Helper\SchedulerHelper;
use Joomla\Component\Scheduler\Administrator\Table\TaskTable;
use Joomla\Component\Scheduler\Administrator\Task\TaskOption;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
use Symfony\Component\OptionsResolver\Exception\AccessException;
Expand Down Expand Up @@ -453,15 +451,6 @@ private function buildLockQuery($db, $now, $options)
->set($db->quoteName('locked') . ' = :now1')
->bind(':now1', $now);

$activeRoutines = array_map(
static function (TaskOption $taskOption): string {
return $taskOption->id;
},
SchedulerHelper::getTaskOptions()->options
);

$lockQuery->whereIn($db->quoteName('type'), $activeRoutines, ParameterType::STRING);

if (!$options['includeCliExclusive']) {
$lockQuery->where($db->quoteName('cli_exclusive') . ' = 0');
}
Expand Down
51 changes: 46 additions & 5 deletions administrator/components/com_scheduler/src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

namespace Joomla\Component\Scheduler\Administrator\Task;

use Cron\CronExpression;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Helper\ExecRuleHelper;
use Joomla\Component\Scheduler\Administrator\Helper\SchedulerHelper;
use Joomla\Component\Scheduler\Administrator\Scheduler\Scheduler;
use Joomla\Component\Scheduler\Administrator\Table\TaskTable;
Expand Down Expand Up @@ -134,8 +135,11 @@ class Task implements LoggerAwareInterface
public function __construct(object $record)
{
// Workaround because Registry dumps private properties otherwise.
$taskOption = $record->taskOption;
$record->params = json_decode($record->params, true);
$taskOption = $record->taskOption;

if (\is_string($record->params)) {
$record->params = json_decode($record->params, true);
}

$this->taskRegistry = new Registry($record);

Expand Down Expand Up @@ -245,7 +249,7 @@ public function run(): bool
$this->set('last_exit_code', $this->snapshot['status']);

if ($this->snapshot['status'] !== Status::WILL_RESUME) {
$this->set('next_execution', (new ExecRuleHelper($this->taskRegistry->toObject()))->nextExec());
$this->set('next_execution', $this->computeNextExecution());
$this->set('times_executed', $this->get('times_executed') + 1);
} else {
/**
Expand Down Expand Up @@ -436,7 +440,7 @@ public function skipExecution(): void
$query = $db->getQuery(true);

$id = $this->get('id');
$nextExec = (new ExecRuleHelper($this->taskRegistry->toObject()))->nextExec(true, true);
$nextExec = $this->computeNextExecution(true, true);

$query->update($db->quoteName('#__scheduler_tasks', 't'))
->set('t.next_execution = :nextExec')
Expand Down Expand Up @@ -559,4 +563,41 @@ public static function isValidId(string $id): bool

return true;
}

/**
* Compute the next execution datetime for the task. This method is used once when a task is created or modified,
* and each time a task is triggered.
*
* @param boolean $asString If true, an SQL formatted string is returned.
* @param boolean $basisNow If true, the current date-time is used as the basis for projecting the next
* execution.
*
* @return ?Date|string Next due execution.
*
* @since 4.1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 4.1.0
* @since __DEPLOY_VERSION__

* @throws \Exception
*/
public function computeNextExecution(bool $asString = true, bool $basisNow = false)
{
$expression = $this->get('cron_rules.exp');

switch ($this->get('cron_rules.type')) {
case 'interval':
$lastExec = Factory::getDate($basisNow ? 'now' : $this->get('last_execution'), 'UTC');
$interval = new \DateInterval($expression);
$nextExec = $lastExec->add($interval);
break;
case 'cron-expression':
$cronExpression = new CronExpression($expression);
$nextExec = $cronExpression->getNextRunDate('now', 0, false, 'UTC');
break;
default:
// 'manual' execution is handled here.
$nextExec = null;
}

return ($asString && !empty($nextExec))
? $nextExec->format($this->db->getDateFormat())
: $nextExec;
}
}