Skip to content

Commit 2875f27

Browse files
DenitzQuyjoomla-botlaoneoMacJoom
authored
[4.4] Optimize scheduler running plugin (#37120)
* fix * CS * CS * bind it! * Make Postgres happy * Postgressssss * CS * Update plugins/system/schedulerunner/schedulerunner.php Quy's suggestion Co-authored-by: Quy <[email protected]> * Update plugins/system/schedulerunner/schedulerunner.php Quy's suggestion Co-authored-by: Quy <[email protected]> * Update plugins/system/schedulerunner/schedulerunner.php Quy's suggestion Co-authored-by: Quy <[email protected]> * fix * Use model method * Phase 1 convert BRANCH to PSR-12 * Phase 2 convert BRANCH to PSR-12 * fix CS * fix CS * Update administrator/components/com_scheduler/src/Model/TasksModel.php Co-authored-by: Allon Moritz <[email protected]> * Update administrator/components/com_scheduler/src/Model/TasksModel.php Co-authored-by: Allon Moritz <[email protected]> --------- Co-authored-by: Denitz <[email protected]> Co-authored-by: Quy <[email protected]> Co-authored-by: Joomla! Bot <[email protected]> Co-authored-by: Allon Moritz <[email protected]> Co-authored-by: Martin Carl Kopp <[email protected]>
1 parent f19d92c commit 2875f27

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

administrator/components/com_scheduler/src/Model/TasksModel.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Joomla\Component\Scheduler\Administrator\Model;
1212

1313
use Joomla\CMS\Component\ComponentHelper;
14+
use Joomla\CMS\Date\Date;
1415
use Joomla\CMS\Factory;
1516
use Joomla\CMS\Language\Text;
1617
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
@@ -366,7 +367,7 @@ protected function _getList($query, $limitstart = 0, $limit = 0): array
366367
{
367368
// Get stuff from the model state
368369
$listOrder = $this->getState('list.ordering', 'a.title');
369-
$listDirectionN = strtolower($this->getState('list.direction', 'asc')) == 'desc' ? -1 : 1;
370+
$listDirectionN = strtolower($this->getState('list.direction', 'asc')) === 'desc' ? -1 : 1;
370371

371372
// Set limit parameters and get object list
372373
$query->setLimit($limit, $limitstart);
@@ -395,7 +396,7 @@ static function (array $arr) {
395396
$this->attachTaskOptions($responseList);
396397

397398
// If ordering by non-db fields, we need to sort here in code
398-
if ($listOrder == 'j.type_title') {
399+
if ($listOrder === 'j.type_title') {
399400
$responseList = ArrayHelper::sortObjects($responseList, 'safeTypeTitle', $listDirectionN, true, false);
400401
}
401402

@@ -437,4 +438,34 @@ protected function populateState($ordering = 'a.title', $direction = 'ASC'): voi
437438
// Call the parent method
438439
parent::populateState($ordering, $direction);
439440
}
441+
442+
/**
443+
* Check if we have any enabled due tasks and no locked tasks.
444+
*
445+
* @param Date $time The next execution time to check against
446+
*
447+
* @return boolean
448+
* @since __DEPLOY_VERSION__
449+
*/
450+
public function hasDueTasks(Date $time): bool
451+
{
452+
$db = $this->getDatabase();
453+
$now = $time->toSql();
454+
455+
$query = $db->getQuery(true)
456+
// Count due tasks
457+
->select('SUM(CASE WHEN ' . $db->quoteName('a.next_execution') . ' <= :now THEN 1 ELSE 0 END) AS due_count')
458+
// Count locked tasks
459+
->select('SUM(CASE WHEN ' . $db->quoteName('a.locked') . ' IS NULL THEN 0 ELSE 1 END) AS locked_count')
460+
->from($db->quoteName('#__scheduler_tasks', 'a'))
461+
->where($db->quoteName('a.state') . ' = 1')
462+
->bind(':now', $now);
463+
464+
$db->setQuery($query);
465+
466+
$taskDetails = $db->loadObject();
467+
468+
// False if we don't have due tasks, or we have locked tasks
469+
return $taskDetails && $taskDetails->due_count && !$taskDetails->locked_count;
470+
}
440471
}

plugins/system/schedulerunner/src/Extension/ScheduleRunner.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Joomla\CMS\Session\Session;
2020
use Joomla\CMS\Table\Extension;
2121
use Joomla\CMS\User\UserHelper;
22+
use Joomla\Component\Scheduler\Administrator\Model\TasksModel;
2223
use Joomla\Component\Scheduler\Administrator\Scheduler\Scheduler;
2324
use Joomla\Component\Scheduler\Administrator\Task\Task;
2425
use Joomla\Event\Event;
@@ -109,22 +110,13 @@ public function injectLazyJS(EventInterface $event): void
109110
return;
110111
}
111112

112-
// Check if any task is due to decrease the load
113+
/** @var TasksModel $model */
113114
$model = $this->getApplication()->bootComponent('com_scheduler')
114115
->getMVCFactory()->createModel('Tasks', 'Administrator', ['ignore_request' => true]);
115116

116-
$model->setState('filter.state', 1);
117-
$model->setState('filter.due', 1);
117+
$now = Factory::getDate('now', 'UTC');
118118

119-
$items = $model->getItems();
120-
121-
// See if we are running currently
122-
$model->setState('filter.locked', 1);
123-
$model->setState('filter.due', 0);
124-
125-
$items2 = $model->getItems();
126-
127-
if (empty($items) || !empty($items2)) {
119+
if (!$model->hasDueTasks($now)) {
128120
return;
129121
}
130122

@@ -262,7 +254,7 @@ public function runTestCron(Event $event)
262254
]
263255
);
264256

265-
if (!is_null($task)) {
257+
if ($task) {
266258
$task->run();
267259
$event->addArgument('result', $task->getContent());
268260
} else {
@@ -286,7 +278,7 @@ public function runTestCron(Event $event)
286278
* @return ?Task
287279
*
288280
* @since 4.1.0
289-
* @throws RuntimeException
281+
* @throws \RuntimeException
290282
*/
291283
private function runScheduler(int $id = 0): ?Task
292284
{
@@ -301,7 +293,7 @@ private function runScheduler(int $id = 0): ?Task
301293
* @return void
302294
*
303295
* @since 4.1.0
304-
* @throws UnexpectedValueException|RuntimeException
296+
* @throws \UnexpectedValueException|\RuntimeException
305297
*
306298
* @todo Move to another plugin?
307299
*/

0 commit comments

Comments
 (0)