Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| check_schedules | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| get_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | // This file is part of Moodle - https://moodle.org/ |
| 3 | // |
| 4 | // Moodle is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // Moodle is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with Moodle. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | namespace tool_autoflow\task; |
| 18 | |
| 19 | use tool_autoflow\local\engine\cron_expression; |
| 20 | use tool_autoflow\local\runner; |
| 21 | use tool_autoflow\local\workflow_store; |
| 22 | |
| 23 | /** |
| 24 | * Every minute: queue the scheduled workflows that are due, in the site timezone. |
| 25 | * |
| 26 | * @package tool_autoflow |
| 27 | * @copyright 2026 DevLMS <contact@devlms.com> |
| 28 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | */ |
| 30 | class check_schedules extends \core\task\scheduled_task { |
| 31 | /** |
| 32 | * Name shown in the task list. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function get_name(): string { |
| 37 | return get_string('task:checkschedules', 'tool_autoflow'); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Run the check now. |
| 42 | */ |
| 43 | public function execute() { |
| 44 | $this->check(time()); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Queue every scheduled workflow with a matching minute since its last check. |
| 49 | * |
| 50 | * @param int $now |
| 51 | */ |
| 52 | public function check(int $now): void { |
| 53 | global $DB; |
| 54 | $timezone = \core_date::get_server_timezone_object(); |
| 55 | foreach (workflow_store::scheduled() as $workflow) { |
| 56 | // A workflow never checked before only looks at the last minute. |
| 57 | $from = (int) $workflow->lastscheduled ?: $now - 60; |
| 58 | if ( |
| 59 | cron_expression::is_valid($workflow->triggerdata) |
| 60 | && cron_expression::parse($workflow->triggerdata)->due_between($from, $now, $timezone) |
| 61 | ) { |
| 62 | runner::queue((int) $workflow->id, ['time' => $now], 'schedule'); |
| 63 | } |
| 64 | $DB->set_field('tool_autoflow_workflow', 'lastscheduled', $now, ['id' => $workflow->id]); |
| 65 | } |
| 66 | } |
| 67 | } |