* @param string $taskname Name of task e.g. 'mod_whatever\task\do_something'
*/
public function i_run_the_scheduled_task($taskname) {
+ global $CFG;
+ require_once("{$CFG->libdir}/cronlib.php");
+
$task = \core\task\manager::get_scheduled_task($taskname);
if (!$task) {
throw new DriverException('The "' . $taskname . '" scheduled task does not exist');
}
try {
+ // Prepare the renderer.
+ cron_prepare_core_renderer();
+
// Discard task output as not appropriate for Behat output!
ob_start();
$task->execute();
ob_end_clean();
+ // Restore the previous renderer.
+ cron_prepare_core_renderer(true);
+
// Mark task complete.
\core\task\manager::scheduled_task_complete($task);
} catch (Exception $e) {
+ // Restore the previous renderer.
+ cron_prepare_core_renderer(true);
+
// Mark task failed and throw exception.
\core\task\manager::scheduled_task_failed($task);
+
throw new DriverException('The "' . $taskname . '" scheduled task failed', 0, $e);
}
}
* @throws DriverException
*/
public function i_run_all_adhoc_tasks() {
+ global $CFG, $DB;
+ require_once("{$CFG->libdir}/cronlib.php");
+
// Do setup for cron task.
cron_setup_user();
- // Run tasks. Locking is handled by get_next_adhoc_task.
- $now = time();
- ob_start(); // Discard task output as not appropriate for Behat output!
- while (($task = \core\task\manager::get_next_adhoc_task($now)) !== null) {
-
- try {
- $task->execute();
-
- // Mark task complete.
- \core\task\manager::adhoc_task_complete($task);
- } catch (Exception $e) {
- // Mark task failed and throw exception.
- \core\task\manager::adhoc_task_failed($task);
- ob_end_clean();
- throw new DriverException('An adhoc task failed', 0, $e);
+ // Discard task output as not appropriate for Behat output!
+ ob_start();
+
+ // Run all tasks which have a scheduled runtime of before now.
+ $timenow = time();
+
+ while (!\core\task\manager::static_caches_cleared_since($timenow) &&
+ $task = \core\task\manager::get_next_adhoc_task($timenow)) {
+ // Clean the output buffer between tasks.
+ ob_clean();
+
+ // Run the task.
+ cron_run_inner_adhoc_task($task);
+
+ // Check whether the task record still exists.
+ // If a task was successful it will be removed.
+ // If it failed then it will still exist.
+ if ($DB->record_exists('task_adhoc', ['id' => $task->get_id()])) {
+ // End ouptut buffering and flush the current buffer.
+ // This should be from just the current task.
+ ob_end_flush();
+
+ throw new DriverException('An adhoc task failed', 0);
}
}
ob_end_clean();