diff --git a/admin/classes/task_log_table.php b/admin/classes/task_log_table.php index 949e757422e..673e9a18ca4 100644 --- a/admin/classes/task_log_table.php +++ b/admin/classes/task_log_table.php @@ -57,6 +57,8 @@ class task_log_table extends \table_sql { 'userid' => get_string('user', 'admin'), 'timestart' => get_string('task_starttime', 'admin'), 'duration' => get_string('task_duration', 'admin'), + 'hostname' => get_string('hostname', 'tool_task'), + 'pid' => get_string('pid', 'tool_task'), 'db' => get_string('task_dbstats', 'admin'), 'result' => get_string('task_result', 'admin'), 'actions' => '', @@ -132,6 +134,7 @@ class task_log_table extends \table_sql { $sql = "SELECT tl.id, tl.type, tl.component, tl.classname, tl.userid, tl.timestart, tl.timeend, + tl.hostname, tl.pid, tl.dbreads, tl.dbwrites, tl.result, tl.dbreads + tl.dbwrites AS db, tl.timeend - tl.timestart AS duration, diff --git a/admin/cli/adhoc_task.php b/admin/cli/adhoc_task.php index b0ed21d3768..dd1cc14b478 100644 --- a/admin/cli/adhoc_task.php +++ b/admin/cli/adhoc_task.php @@ -37,11 +37,13 @@ list($options, $unrecognized) = cli_get_params( 'showsql' => false, 'showdebugging' => false, 'ignorelimits' => false, + 'force' => false, ], [ 'h' => 'help', 'e' => 'execute', 'k' => 'keep-alive', 'i' => 'ignorelimits', + 'f' => 'force', ] ); @@ -61,6 +63,7 @@ Options: -e, --execute Run all queued adhoc tasks -k, --keep-alive=N Keep this script alive for N seconds and poll for new adhoc tasks -i --ignorelimits Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits + -f, --force Run even if cron is disabled Example: \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute @@ -92,6 +95,12 @@ if (moodle_needs_upgrading()) { if (empty($options['execute'])) { exit(0); } + +if (!get_config('core', 'cron_enabled') && !$options['force']) { + mtrace('Cron is disabled. Use --force to override.'); + exit(1); +} + if (empty($options['keep-alive'])) { $options['keep-alive'] = 0; } diff --git a/admin/cli/cron.php b/admin/cli/cron.php index fe726830117..958062a5143 100644 --- a/admin/cli/cron.php +++ b/admin/cli/cron.php @@ -36,14 +36,23 @@ require_once($CFG->libdir.'/cronlib.php'); // now get cli options list($options, $unrecognized) = cli_get_params( - array( + [ 'help' => false, 'stop' => false, - ), - array( + 'list' => false, + 'force' => false, + 'enable' => false, + 'disable' => false, + 'disable-wait' => false, + ], [ 'h' => 'help', 's' => 'stop', - ) + 'l' => 'list', + 'f' => 'force', + 'e' => 'enable', + 'd' => 'disable', + 'w' => 'disable-wait', + ] ); if ($unrecognized) { @@ -56,8 +65,13 @@ if ($options['help']) { "Execute periodic cron actions. Options: --h, --help Print out this help --s, --stop Notify all other running cron processes to stop after the current task +-h, --help Print out this help +-s, --stop Notify all other running cron processes to stop after the current task +-l, --list Show the list of currently running tasks and how long they have been running +-f, --force Execute task even if cron is disabled +-e, --enable Enable cron +-d, --disable Disable cron +-w, --disable-wait=600 Disable cron and wait until all tasks finished or fail after N seconds (optional param) Example: \$sudo -u www-data /usr/bin/php admin/cli/cron.php @@ -74,6 +88,91 @@ if ($options['stop']) { die; } +if ($options['enable']) { + set_config('cron_enabled', 1); + mtrace('Cron has been enabled for the site.'); + exit(0); +} + +if ($options['disable']) { + set_config('cron_enabled', 0); + \core\task\manager::clear_static_caches(); + mtrace('Cron has been disabled for the site.'); + exit(0); +} + +if ($options['list']) { + $tasks = \core\task\manager::get_running_tasks(); + mtrace('The list of currently running tasks:'); + $format = "%7s %-12s %-9s %-20s %-52s\n"; + printf ($format, + 'PID', + 'HOST', + 'TYPE', + 'TIME', + 'CLASSNAME' + ); + foreach ($tasks as $task) { + printf ($format, + $task->pid, + substr($task->hostname, 0, 12), + $task->type, + format_time(time() - $task->timestarted), + substr($task->classname, 0, 52) + ); + } + exit(0); +} + +if ($wait = $options['disable-wait']) { + $started = time(); + if (true === $wait) { + // Default waiting time. + $waitsec = 600; + } else { + $waitsec = $wait; + $wait = true; + } + + set_config('cron_enabled', 0); + \core\task\manager::clear_static_caches(); + mtrace('Cron has been disabled for the site.'); + mtrace('Allocating '. format_time($waitsec) . ' for the tasks to finish.'); + + $lastcount = 0; + while ($wait) { + $tasks = \core\task\manager::get_running_tasks(); + + if (count($tasks) == 0) { + mtrace(''); + mtrace('All scheduled and adhoc tasks finished.'); + exit(0); + } + + if (time() - $started >= $waitsec) { + mtrace(''); + mtrace('Wait time ('. format_time($waitsec) . ') elapsed, but ' . count($tasks) . ' task(s) still running.'); + mtrace('Exiting with code 1.'); + exit(1); + } + + if (count($tasks) !== $lastcount) { + mtrace(''); + mtrace(count($tasks) . " tasks currently running.", ''); + $lastcount = count($tasks); + } else { + mtrace('.', ''); + } + + sleep(1); + } +} + +if (!get_config('core', 'cron_enabled') && !$options['force']) { + mtrace('Cron is disabled. Use --force to override.'); + exit(1); +} + \core\local\cli\shutdown::script_supports_graceful_exit(); cron_run(); diff --git a/admin/cli/scheduled_task.php b/admin/cli/scheduled_task.php index f825f46da35..b181b1549bf 100644 --- a/admin/cli/scheduled_task.php +++ b/admin/cli/scheduled_task.php @@ -30,8 +30,17 @@ require_once("$CFG->libdir/clilib.php"); require_once("$CFG->libdir/cronlib.php"); list($options, $unrecognized) = cli_get_params( - array('help' => false, 'list' => false, 'execute' => false, 'showsql' => false, 'showdebugging' => false), - array('h' => 'help') + [ + 'help' => false, + 'list' => false, + 'execute' => false, + 'showsql' => false, + 'showdebugging' => false, + 'force' => false, + ], [ + 'h' => 'help', + 'f' => 'force', + ] ); if ($unrecognized) { @@ -49,6 +58,7 @@ if ($options['help'] or (!$options['list'] and !$options['execute'])) { --showsql Show sql queries before they are executed --showdebugging Show developer level debugging information -h, --help Print out this help + -f, --force Execute task even if cron is disabled Example: \$sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php --execute=\\core\\task\\session_cleanup_task @@ -121,6 +131,13 @@ if ($execute = $options['execute']) { exit(1); } + if (!get_config('core', 'cron_enabled') && !$options['force']) { + mtrace('Cron is disabled. Use --force to override.'); + exit(1); + } + + \core\task\manager::scheduled_task_starting($task); + // Increase memory limit. raise_memory_limit(MEMORY_EXTRA); diff --git a/admin/settings/server.php b/admin/settings/server.php index e86bbc95101..e9fac46fc8b 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -216,6 +216,16 @@ $ADMIN->add('server', $temp); $ADMIN->add('server', new admin_category('taskconfig', new lang_string('taskadmintitle', 'admin'))); $temp = new admin_settingpage('taskprocessing', new lang_string('taskprocessing','admin')); + +$setting = new admin_setting_configcheckbox( + 'cron_enabled', + new lang_string('cron_enabled', 'admin'), + new lang_string('cron_enabled_desc', 'admin'), + 1 +); +$setting->set_updatedcallback('theme_reset_static_caches'); +$temp->add($setting); + $temp->add( new admin_setting_configtext( 'task_scheduled_concurrency_limit', diff --git a/admin/tool/task/classes/running_tasks_table.php b/admin/tool/task/classes/running_tasks_table.php new file mode 100644 index 00000000000..d08f458f71d --- /dev/null +++ b/admin/tool/task/classes/running_tasks_table.php @@ -0,0 +1,141 @@ +. + +/** + * Running tasks table. + * + * @package tool_task + * @copyright 2019 The Open University + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_task; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir . '/tablelib.php'); + +/** + * Table to display list of running task. + * + * @copyright 2019 The Open University + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class running_tasks_table extends \table_sql { + + /** + * Constructor for the running tasks table. + */ + public function __construct() { + parent::__construct('runningtasks'); + + $columnheaders = [ + 'classname' => get_string('classname', 'tool_task'), + 'type' => get_string('tasktype', 'admin'), + 'time' => get_string('time'), + 'timestarted' => get_string('started', 'tool_task'), + 'hostname' => get_string('hostname', 'tool_task'), + 'pid' => get_string('pid', 'tool_task'), + ]; + $this->define_columns(array_keys($columnheaders)); + $this->define_headers(array_values($columnheaders)); + + // The name column is a header. + $this->define_header_column('classname'); + + // This table is not collapsible. + $this->collapsible(false); + + // Allow pagination. + $this->pageable(true); + } + + /** + * Query the db. Store results in the table object for use by build_table. + * + * @param int $pagesize size of page for paginated displayed table. + * @param bool $useinitialsbar do you want to use the initials bar. Bar + * will only be used if there is a fullname column defined for the table. + * @throws \dml_exception + */ + public function query_db($pagesize, $useinitialsbar = true) { + $sort = $this->get_sql_sort(); + $this->rawdata = \core\task\manager::get_running_tasks($sort); + } + + /** + * Format the classname cell. + * + * @param \stdClass $row + * @return string + */ + public function col_classname($row) : string { + $output = $row->classname; + if ($row->type == 'scheduled') { + if (class_exists($row->classname)) { + $task = new $row->classname; + if ($task instanceof \core\task\scheduled_task) { + $output .= \html_writer::tag('div', $task->get_name(), ['class' => 'task-class']); + } + } + } else if ($row->type == 'adhoc') { + $output .= \html_writer::tag('div', + get_string('adhoctaskid', 'tool_task', $row->id), ['class' => 'task-class']); + } + return $output; + } + + /** + * Format the type cell. + * + * @param \stdClass $row + * @return string + * @throws \coding_exception + */ + public function col_type($row) : string { + if ($row->type == 'scheduled') { + $output = \html_writer::span(get_string('scheduled', 'tool_task'), 'badge badge-primary'); + } else if ($row->type == 'adhoc') { + $output = \html_writer::span(get_string('adhoc', 'tool_task'), 'badge badge-warning'); + } else { + // This shouldn't ever happen. + $output = ''; + } + return $output; + } + + /** + * Format the time cell. + * + * @param \stdClass $row + * @return string + */ + public function col_time($row) : string { + return format_time($row->time); + } + + /** + * Format the timestarted cell. + * + * @param \stdClass $row + * @return string + */ + public function col_timestarted($row) : string { + return userdate($row->timestarted); + } +} diff --git a/admin/tool/task/lang/en/tool_task.php b/admin/tool/task/lang/en/tool_task.php index 299603f3f89..08cf8194086 100644 --- a/admin/tool/task/lang/en/tool_task.php +++ b/admin/tool/task/lang/en/tool_task.php @@ -22,6 +22,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['adhoc'] = 'Ad-hoc'; +$string['adhoctaskid'] = 'Ad-hoc task id: {$a}'; +$string['adhoctasks'] = 'Ad-hoc tasks'; $string['asap'] = 'ASAP'; $string['adhocempty'] = 'Ad hoc task queue is empty'; $string['adhocqueuesize'] = 'Ad hoc task queue has {$a} tasks'; @@ -32,9 +35,11 @@ $string['cannotfindthepathtothecli'] = 'Cannot find the path to the PHP CLI exec $string['checkadhocqueue'] = 'Ad hoc task queue'; $string['checkcronrunning'] = 'Cron running'; $string['checkmaxfaildelay'] = 'Tasks max fail delay'; +$string['classname'] = 'Class name'; $string['clearfaildelay_confirm'] = 'Are you sure you want to clear the fail delay for task \'{$a}\'? After clearing the delay, the task will run according to its normal schedule.'; $string['component'] = 'Component'; $string['corecomponent'] = 'Core'; +$string['crondisabled'] = 'Cron is disabled. No new tasks will be started. The system will not operate properly until it is enabled again.'; $string['cronok'] = 'Cron is running frequently'; $string['default'] = 'Default'; $string['defaultx'] = 'Default: {$a}'; @@ -45,18 +50,24 @@ $string['enablerunnow'] = 'Allow \'Run now\' for scheduled tasks'; $string['enablerunnow_desc'] = 'Allows administrators to run a single scheduled task immediately, rather than waiting for it to run as scheduled. The feature requires \'Path to PHP CLI\' (pathtophp) to be set in System paths. The task runs on the web server, so you may wish to disable this feature to avoid potential performance issues.'; $string['faildelay'] = 'Fail delay'; $string['fromcomponent'] = 'From component: {$a}'; +$string['hostname'] = 'Host name'; $string['lastruntime'] = 'Last run'; +$string['lastupdated'] = 'Last updated {$a}.'; $string['nextruntime'] = 'Next run'; +$string['pid'] = 'PID'; $string['plugindisabled'] = 'Plugin disabled'; $string['pluginname'] = 'Scheduled task configuration'; $string['resettasktodefaults'] = 'Reset task schedule to defaults'; $string['resettasktodefaults_help'] = 'This will discard any local changes and revert the schedule for this task back to its original settings.'; +$string['runningtasks'] = 'Tasks running now'; $string['runnow'] = 'Run now'; $string['runagain'] = 'Run again'; $string['runnow_confirm'] = 'Are you sure you want to run this task \'{$a}\' now? The task will run on the web server and may take some time to complete.'; $string['runpattern'] = 'Run pattern'; +$string['scheduled'] = 'Scheduled'; $string['scheduledtasks'] = 'Scheduled tasks'; $string['scheduledtaskchangesdisabled'] = 'Modifications to the list of scheduled tasks have been prevented in Moodle configuration'; +$string['started'] = 'Started'; $string['taskdisabled'] = 'Task disabled'; $string['taskfailures'] = '{$a} task(s) failing'; $string['tasklogs'] = 'Task logs'; diff --git a/admin/tool/task/renderer.php b/admin/tool/task/renderer.php index ec0bb0cac4a..ec85231c5a3 100644 --- a/admin/tool/task/renderer.php +++ b/admin/tool/task/renderer.php @@ -261,6 +261,16 @@ class tool_task_renderer extends plugin_renderer_base { return $cell; } + /** + * Displays a warning on the page if cron is disabled. + * + * @return string HTML code for information about cron being disabled + * @throws moodle_exception + */ + public function cron_disabled(): string { + return $this->output->notification(get_string('crondisabled', 'tool_task'), 'warning'); + } + /** * Renders a link back to the scheduled tasks page (used from the 'run now' screen). * diff --git a/admin/tool/task/runningtasks.php b/admin/tool/task/runningtasks.php new file mode 100644 index 00000000000..05829a31112 --- /dev/null +++ b/admin/tool/task/runningtasks.php @@ -0,0 +1,51 @@ +. + +/** + * Running task admin page. + * + * @package tool_task + * @copyright 2019 The Open University + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../../config.php'); +require_once($CFG->libdir.'/adminlib.php'); +require_once($CFG->libdir.'/tablelib.php'); + +$pageurl = new \moodle_url('/admin/tool/task/runningtasks.php'); +$heading = get_string('runningtasks', 'tool_task'); +$PAGE->set_url($pageurl); +$PAGE->set_context(context_system::instance()); +$PAGE->set_pagelayout('admin'); +$PAGE->set_title($heading); +$PAGE->set_heading($heading); + +admin_externalpage_setup('runningtasks'); + +echo $OUTPUT->header(); + +if (!get_config('core', 'cron_enabled')) { + $renderer = $PAGE->get_renderer('tool_task'); + echo $renderer->cron_disabled(); +} + +$table = new \tool_task\running_tasks_table(); +$table->baseurl = $pageurl; +$table->out(100, false); + +echo $OUTPUT->footer(); diff --git a/admin/tool/task/scheduledtasks.php b/admin/tool/task/scheduledtasks.php index d256f3d7175..243039e6b6d 100644 --- a/admin/tool/task/scheduledtasks.php +++ b/admin/tool/task/scheduledtasks.php @@ -95,6 +95,9 @@ if ($mform && ($mform->is_cancelled() || !empty($CFG->preventscheduledtaskchange } else { echo $OUTPUT->header(); + if (!get_config('core', 'cron_enabled')) { + echo $renderer->cron_disabled(); + } $tasks = core\task\manager::get_all_scheduled_tasks(); echo $renderer->scheduled_tasks_table($tasks, $lastchanged); echo $OUTPUT->footer(); diff --git a/admin/tool/task/settings.php b/admin/tool/task/settings.php index ac9858ed61e..3fd9669b81f 100644 --- a/admin/tool/task/settings.php +++ b/admin/tool/task/settings.php @@ -33,4 +33,13 @@ if ($hassiteconfig) { "$CFG->wwwroot/$CFG->admin/tool/task/scheduledtasks.php" ) ); + + $ADMIN->add( + 'taskconfig', + new admin_externalpage( + 'runningtasks', + new lang_string('runningtasks', 'tool_task'), + "$CFG->wwwroot/$CFG->admin/tool/task/runningtasks.php" + ) + ); } diff --git a/admin/tool/task/styles.css b/admin/tool/task/styles.css index 0e846ce3c5c..f297fd6afaf 100644 --- a/admin/tool/task/styles.css +++ b/admin/tool/task/styles.css @@ -1,4 +1,5 @@ -#page-admin-tool-task-scheduledtasks .task-class { +#page-admin-tool-task-scheduledtasks .task-class, +#page-admin-tool-task-runningtasks .task-class { display: block; padding: 0 0.5em; color: #888; diff --git a/admin/tool/task/tests/behat/cron_disabled.feature b/admin/tool/task/tests/behat/cron_disabled.feature new file mode 100644 index 00000000000..24c7e87ac4b --- /dev/null +++ b/admin/tool/task/tests/behat/cron_disabled.feature @@ -0,0 +1,20 @@ +@tool @tool_task +Feature: See warning message if cron is disabled + In order to manage scheduled tasks + As a Moodle Administrator + I need to be able to view a warning message if cron is disabled + + Background: + Given I log in as "admin" + + Scenario: If cron is disabled, I should see the message + When the following config values are set as admin: + | cron_enabled | 0 | + And I navigate to "Server > Tasks > Scheduled tasks" in site administration + Then I should see "Cron is disabled" + + Scenario: If cron is enabled, I should not see the message + When the following config values are set as admin: + | cron_enabled | 1 | + And I navigate to "Server > Tasks > Scheduled tasks" in site administration + Then I should not see "Cron is disabled" diff --git a/admin/tool/task/tests/behat/running_tasks.feature b/admin/tool/task/tests/behat/running_tasks.feature new file mode 100644 index 00000000000..e5db69ced50 --- /dev/null +++ b/admin/tool/task/tests/behat/running_tasks.feature @@ -0,0 +1,40 @@ +@tool @tool_task +Feature: See running scheduled tasks + In order to configure scheduled tasks + As an admin + I need to see if tasks are running + + Background: + Given I log in as "admin" + + Scenario: If no task is running, I should see the corresponding message + Given I navigate to "Server > Tasks > Tasks running now" in site administration + Then I should see "Nothing to display" + + Scenario: If tasks are running, I should see task details + Given the following "tool_task > scheduled tasks" exist: + | classname | seconds | hostname | pid | + | \core\task\automated_backup_task | 121 | c69335460f7f | 1914 | + And the following "tool_task > adhoc tasks" exist: + | classname | seconds | hostname | pid | + | \core\task\asynchronous_backup_task | 7201 | c69335460f7f | 1915 | + | \core\task\asynchronous_restore_task | 172800 | c69335460f7f | 1916 | + And I navigate to "Server > Tasks > Tasks running now" in site administration + + # Check the scheduled task details. + Then I should see "Scheduled" in the "\core\task\automated_backup_task" "table_row" + And I should see "2 mins" in the "Automated backups" "table_row" + And I should see "c69335460f7f" in the "Automated backups" "table_row" + And I should see "1914" in the "Automated backups" "table_row" + + # Check the "asynchronous_backup_task" adhoc task details. + And I should see "Ad-hoc" in the "\core\task\asynchronous_backup_task" "table_row" + And I should see "2 hours" in the "core\task\asynchronous_backup_task" "table_row" + And I should see "c69335460f7f" in the "core\task\asynchronous_backup_task" "table_row" + And I should see "1915" in the "core\task\asynchronous_backup_task" "table_row" + + # Check the "asynchronous_restore_task" adhoc task details. + And I should see "Ad-hoc" in the "\core\task\asynchronous_restore_task" "table_row" + And I should see "2 days" in the "core\task\asynchronous_restore_task" "table_row" + And I should see "c69335460f7f" in the "core\task\asynchronous_restore_task" "table_row" + And I should see "1916" in the "core\task\asynchronous_restore_task" "table_row" diff --git a/admin/tool/task/tests/generator/behat_tool_task_generator.php b/admin/tool/task/tests/generator/behat_tool_task_generator.php new file mode 100644 index 00000000000..307ad8103a6 --- /dev/null +++ b/admin/tool/task/tests/generator/behat_tool_task_generator.php @@ -0,0 +1,57 @@ +. + +/** + * Behat data generator for tool_task. + * + * @package tool_task + * @category test + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Behat data generator for tool_task. + * + * @package tool_task + * @category test + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_tool_task_generator extends behat_generator_base { + + /** + * Get a list of the entities that can be created. + + * @return array entity name => information about how to generate. + */ + protected function get_creatable_entities(): array { + return [ + 'scheduled tasks' => [ + 'singular' => 'scheduled task', + 'datagenerator' => 'scheduled_tasks', + 'required' => ['classname', 'seconds', 'hostname', 'pid'], + ], + 'adhoc tasks' => [ + 'singular' => 'adhoc task', + 'datagenerator' => 'adhoc_tasks', + 'required' => ['classname', 'seconds', 'hostname', 'pid'], + ], + ]; + } +} diff --git a/admin/tool/task/tests/generator/lib.php b/admin/tool/task/tests/generator/lib.php new file mode 100644 index 00000000000..b9324c5db60 --- /dev/null +++ b/admin/tool/task/tests/generator/lib.php @@ -0,0 +1,69 @@ +. + +/** + * Tool task test data generator class + * + * @package tool_task + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tool task test data generator class + * + * @package tool_task + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_task_generator extends testing_module_generator { + + /** + * Mark a scheduled task as running. + * + * @param array $data Scheduled task properties + * @throws dml_exception + */ + public function create_scheduled_tasks($data) { + global $DB; + $conditions = ['classname' => $data['classname']]; + $record = $DB->get_record('task_scheduled', $conditions, '*', MUST_EXIST); + $record->timestarted = time() - $data['seconds']; + $record->hostname = $data['hostname']; + $record->pid = $data['pid']; + $DB->update_record('task_scheduled', $record); + } + + /** + * Mark an adhoc task as running. + * + * @param array $data Adhoc task properties + * @throws dml_exception + */ + public function create_adhoc_tasks($data) { + global $DB; + $adhoctask = (object)[ + 'classname' => $data['classname'], + 'nextruntime' => 0, + 'timestarted' => time() - $data['seconds'], + 'hostname' => $data['hostname'], + 'pid' => $data['pid'], + ]; + $DB->insert_record('task_adhoc', $adhoctask); + } +} diff --git a/admin/tool/task/version.php b/admin/tool/task/version.php index 18232735309..151504cddcf 100644 --- a/admin/tool/task/version.php +++ b/admin/tool/task/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021052500; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2021052501; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2021052500; // Requires this Moodle version $plugin->component = 'tool_task'; // Full name of the plugin (used for diagnostics) diff --git a/lang/en/admin.php b/lang/en/admin.php index 5cb42af68cc..2066a80fc52 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -424,6 +424,8 @@ $string['courseswithsummarieslimit'] = 'Courses with summaries limit'; $string['creatornewroleid'] = 'Creators\' role in new courses'; $string['creatornewroleid_help'] = 'If the user does not already have the permission to manage the new course, the user is automatically enrolled using this role.'; $string['cron'] = 'Cron'; +$string['cron_enabled'] = 'Enable cron'; +$string['cron_enabled_desc'] = 'If disabled prevents the system from starting new background tasks. This option is intended for temporary use only, e.g. before a restart. Leaving it off for a long time will prevent important functionality from working.'; $string['cron_help'] = 'The cron.php script runs a number of tasks at different scheduled intervals, such as sending forum post notification emails. The script should be run regularly - ideally every minute.'; $string['cron_link'] = 'admin/cron'; $string['cronclionly'] = 'Cron execution via command line only'; diff --git a/lib/classes/task/database_logger.php b/lib/classes/task/database_logger.php index 4e25171a647..d1978fd2423 100644 --- a/lib/classes/task/database_logger.php +++ b/lib/classes/task/database_logger.php @@ -75,6 +75,8 @@ class database_logger implements task_logger { 'dbwrites' => $dbwrites, 'result' => (int) $failed, 'output' => file_get_contents($logpath), + 'hostname' => $task->get_hostname(), + 'pid' => $task->get_pid(), ]; if (is_a($task, adhoc_task::class) && $userid = $task->get_userid()) { diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 552cd49eeb1..08b624222ac 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -255,6 +255,9 @@ class manager { $record->dayofweek = $task->get_day_of_week(); $record->month = $task->get_month(); $record->disabled = $task->get_disabled(); + $record->timestarted = $task->get_timestarted(); + $record->hostname = $task->get_hostname(); + $record->pid = $task->get_pid(); return $record; } @@ -276,6 +279,9 @@ class manager { $record->customdata = $task->get_custom_data_as_string(); $record->userid = $task->get_userid(); $record->timecreated = time(); + $record->timestarted = $task->get_timestarted(); + $record->hostname = $task->get_hostname(); + $record->pid = $task->get_pid(); return $record; } @@ -313,6 +319,15 @@ class manager { if (isset($record->userid)) { $task->set_userid($record->userid); } + if (isset($record->timestarted)) { + $task->set_timestarted($record->timestarted); + } + if (isset($record->hostname)) { + $task->set_hostname($record->hostname); + } + if (isset($record->pid)) { + $task->set_pid($record->pid); + } return $task; } @@ -367,6 +382,15 @@ class manager { if (isset($record->disabled)) { $task->set_disabled($record->disabled); } + if (isset($record->timestarted)) { + $task->set_timestarted($record->timestarted); + } + if (isset($record->hostname)) { + $task->set_hostname($record->hostname); + } + if (isset($record->pid)) { + $task->set_pid($record->pid); + } return $task; } @@ -709,6 +733,9 @@ class manager { */ public static function adhoc_task_failed(adhoc_task $task) { global $DB; + // Finalise the log output. + logmanager::finalise_log(true); + $delay = $task->get_fail_delay(); // Reschedule task with exponential fall off for failing tasks. @@ -724,6 +751,9 @@ class manager { } // Reschedule and then release the locks. + $task->set_timestarted(); + $task->set_hostname(); + $task->set_pid(); $task->set_next_run_time(time() + $delay); $task->set_fail_delay($delay); $record = self::record_from_adhoc_task($task); @@ -734,9 +764,31 @@ class manager { $task->get_cron_lock()->release(); } $task->get_lock()->release(); + } - // Finalise the log output. - logmanager::finalise_log(true); + /** + * Records that a adhoc task is starting to run. + * + * @param adhoc_task $task Task that is starting + * @param int $time Start time (leave blank for now) + * @throws \dml_exception + * @throws \coding_exception + */ + public static function adhoc_task_starting(adhoc_task $task, int $time = 0) { + global $DB; + $pid = (int)getmypid(); + $hostname = (string)gethostname(); + + if (empty($time)) { + $time = time(); + } + + $task->set_timestarted($time); + $task->set_hostname($hostname); + $task->set_pid($pid); + + $record = self::record_from_adhoc_task($task); + $DB->update_record('task_adhoc', $record); } /** @@ -749,6 +801,9 @@ class manager { // Finalise the log output. logmanager::finalise_log(); + $task->set_timestarted(); + $task->set_hostname(); + $task->set_pid(); // Delete the adhoc task record - it is finished. $DB->delete_records('task_adhoc', array('id' => $task->get_id())); @@ -768,6 +823,8 @@ class manager { */ public static function scheduled_task_failed(scheduled_task $task) { global $DB; + // Finalise the log output. + logmanager::finalise_log(true); $delay = $task->get_fail_delay(); @@ -783,20 +840,24 @@ class manager { $delay = 86400; } + $task->set_timestarted(); + $task->set_hostname(); + $task->set_pid(); + $classname = self::get_canonical_class_name($task); $record = $DB->get_record('task_scheduled', array('classname' => $classname)); $record->nextruntime = time() + $delay; $record->faildelay = $delay; + $record->timestarted = null; + $record->hostname = null; + $record->pid = null; $DB->update_record('task_scheduled', $record); if ($task->is_blocking()) { $task->get_cron_lock()->release(); } $task->get_lock()->release(); - - // Finalise the log output. - logmanager::finalise_log(true); } /** @@ -816,6 +877,34 @@ class manager { $DB->update_record('task_scheduled', $record); } + /** + * Records that a scheduled task is starting to run. + * + * @param scheduled_task $task Task that is starting + * @param int $time Start time (0 = current) + * @throws \dml_exception If the task doesn't exist + */ + public static function scheduled_task_starting(scheduled_task $task, int $time = 0) { + global $DB; + $pid = (int)getmypid(); + $hostname = (string)gethostname(); + + if (!$time) { + $time = time(); + } + + $task->set_timestarted($time); + $task->set_hostname($hostname); + $task->set_pid($pid); + + $classname = self::get_canonical_class_name($task); + $record = $DB->get_record('task_scheduled', ['classname' => $classname], '*', MUST_EXIST); + $record->timestarted = $time; + $record->hostname = $hostname; + $record->pid = $pid; + $DB->update_record('task_scheduled', $record); + } + /** * This function indicates that a scheduled task was completed successfully and should be rescheduled. * @@ -826,6 +915,9 @@ class manager { // Finalise the log output. logmanager::finalise_log(); + $task->set_timestarted(); + $task->set_hostname(); + $task->set_pid(); $classname = self::get_canonical_class_name($task); $record = $DB->get_record('task_scheduled', array('classname' => $classname)); @@ -833,6 +925,9 @@ class manager { $record->lastruntime = time(); $record->faildelay = 0; $record->nextruntime = $task->get_next_scheduled_time(); + $record->timestarted = null; + $record->hostname = null; + $record->pid = null; $DB->update_record('task_scheduled', $record); } @@ -844,6 +939,47 @@ class manager { $task->get_lock()->release(); } + /** + * Gets a list of currently-running tasks. + * + * @param string $sort Sorting method + * @return array Array of scheduled and adhoc tasks + * @throws \dml_exception + */ + public static function get_running_tasks($sort = ''): array { + global $DB; + if (empty($sort)) { + $sort = 'timestarted ASC, classname ASC'; + } + $params = ['now1' => time(), 'now2' => time()]; + + $sql = "SELECT subquery.* + FROM (SELECT concat('s', ts.id) as uniqueid, + ts.id, + 'scheduled' as type, + ts.classname, + (:now1 - ts.timestarted) as time, + ts.timestarted, + ts.hostname, + ts.pid + FROM {task_scheduled} ts + WHERE ts.timestarted IS NOT NULL + UNION ALL + SELECT concat('a', ta.id) as uniqueid, + ta.id, + 'adhoc' as type, + ta.classname, + (:now2 - ta.timestarted) as time, + ta.timestarted, + ta.hostname, + ta.pid + FROM {task_adhoc} ta + WHERE ta.timestarted IS NOT NULL) subquery + ORDER BY " . $sort; + + return $DB->get_records_sql($sql, $params); + } + /** * This function is used to indicate that any long running cron processes should exit at the * next opportunity and restart. This is because something (e.g. DB changes) has changed and @@ -959,7 +1095,7 @@ class manager { // Shell-escaped task name. $classname = get_class($task); - $taskarg = escapeshellarg("--execute={$classname}"); + $taskarg = escapeshellarg("--execute={$classname}") . " " . escapeshellarg("--force"); // Build the CLI command. $command = "{$phpbinary} {$scriptpath} {$taskarg}"; diff --git a/lib/classes/task/task_base.php b/lib/classes/task/task_base.php index 05e68a5b36a..bffd73682d7 100644 --- a/lib/classes/task/task_base.php +++ b/lib/classes/task/task_base.php @@ -50,6 +50,15 @@ abstract class task_base { /** @var int $nextruntime - When this task is due to run next */ private $nextruntime = 0; + /** @var int $timestarted - When this task was started */ + private $timestarted = null; + + /** @var string $hostname - Hostname where this task was started and PHP process ID */ + private $hostname = null; + + /** @var int $pid - PHP process ID that is running the task */ + private $pid = null; + /** * Set the current lock for this task. * @param \core\lock\lock $lock @@ -151,4 +160,52 @@ abstract class task_base { * Throw exceptions on errors (the job will be retried). */ public abstract function execute(); + + /** + * Setter for $timestarted. + * @param int $timestarted + */ + public function set_timestarted($timestarted = null) { + $this->timestarted = $timestarted; + } + + /** + * Getter for $timestarted. + * @return int + */ + public function get_timestarted() { + return $this->timestarted; + } + + /** + * Setter for $hostname. + * @param string $hostname + */ + public function set_hostname($hostname = null) { + $this->hostname = $hostname; + } + + /** + * Getter for $hostname. + * @return string + */ + public function get_hostname() { + return $this->hostname; + } + + /** + * Setter for $pid. + * @param int $pid + */ + public function set_pid($pid = null) { + $this->pid = $pid; + } + + /** + * Getter for $pid. + * @return int + */ + public function get_pid() { + return $this->pid; + } } diff --git a/lib/cronlib.php b/lib/cronlib.php index 74a83d5b1d7..4190bf2892d 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -237,6 +237,7 @@ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true) function cron_run_inner_scheduled_task(\core\task\task_base $task) { global $CFG, $DB; + \core\task\manager::scheduled_task_starting($task); \core\task\logmanager::start_logging($task); $fullname = $task->get_name() . ' (' . get_class($task) . ')'; @@ -295,6 +296,7 @@ function cron_run_inner_scheduled_task(\core\task\task_base $task) { function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) { global $DB, $CFG; + \core\task\manager::adhoc_task_starting($task); \core\task\logmanager::start_logging($task); mtrace("Execute adhoc task: " . get_class($task)); diff --git a/lib/db/install.xml b/lib/db/install.xml index d38d57e47dc..e40f3eba38f 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -3352,6 +3352,9 @@ + + + @@ -3371,6 +3374,9 @@ + + + @@ -3393,6 +3399,8 @@ + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index aa2bb5d75d5..7ebbef2c8e8 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2576,7 +2576,6 @@ function xmldb_main_upgrade($oldversion) { } if ($oldversion < 2021052500.04) { - // Define field metadatasettings to be added to h5p_libraries. $table = new xmldb_table('h5p_libraries'); $field = new xmldb_field('metadatasettings', XMLDB_TYPE_TEXT, null, null, null, null, null, 'coreminor'); @@ -2616,5 +2615,51 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.04); } + if ($oldversion < 2021052500.05) { + // Define fields to be added to task_scheduled. + $table = new xmldb_table('task_scheduled'); + $field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'disabled'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timestarted'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Define fields to be added to task_adhoc. + $table = new xmldb_table('task_adhoc'); + $field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'blocking'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timestarted'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Define fields to be added to task_log. + $table = new xmldb_table('task_log'); + $field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'output'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021052500.05); + } + return true; } diff --git a/lib/outputlib.php b/lib/outputlib.php index fb4a29c98f9..f324c4b2afb 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -270,6 +270,16 @@ function theme_reset_all_caches() { } } +/** + * Reset static caches. + * + * This method indicates that all running cron processes should exit at the + * next opportunity. + */ +function theme_reset_static_caches() { + \core\task\manager::clear_static_caches(); +} + /** * Enable or disable theme designer mode. * diff --git a/lib/tests/task_running_test.php b/lib/tests/task_running_test.php new file mode 100644 index 00000000000..f533938c595 --- /dev/null +++ b/lib/tests/task_running_test.php @@ -0,0 +1,185 @@ +. + +/** + * This file contains unit tests for the 'task running' data. + * + * @package core + * @copyright 2019 The Open University + * @copyright 2020 Mikhail Golenkov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\task; + +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/fixtures/task_fixtures.php'); + +/** + * This file contains unit tests for the 'task running' data. + * + * @copyright 2019 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class task_running_testcase extends \advanced_testcase { + + /** + * Test for ad-hoc tasks. + */ + public function test_adhoc_task_running() { + $this->resetAfterTest(); + + // Specify lock factory. The reason is that Postgres locks don't work within a single + // process (i.e. if you try to get a lock that you already locked, it will just let you) + // which is usually OK but not here where we are simulating running two tasks at once in + // the same process. + set_config('lock_factory', '\core\lock\db_record_lock_factory'); + + // Create and queue 2 new ad-hoc tasks. + $task1 = new adhoc_test_task(); + $task1->set_next_run_time(time() - 20); + manager::queue_adhoc_task($task1); + $task2 = new adhoc_test2_task(); + $task2->set_next_run_time(time() - 10); + manager::queue_adhoc_task($task2); + + // Check no tasks are marked running. + $running = manager::get_running_tasks(); + $this->assertEmpty($running); + + // Mark the first task running and check results. Because adhoc tasks are pseudo-randomly + // shuffled, it is safer if we can cope with either of them being first. + $before = time(); + $next1 = manager::get_next_adhoc_task(time()); + $task2goesfirst = get_class($next1) === 'core\task\adhoc_test2_task'; + manager::adhoc_task_starting($next1); + $after = time(); + $running = manager::get_running_tasks(); + $this->assertCount(1, $running); + foreach ($running as $item) { + $this->assertEquals('adhoc', $item->type); + $this->assertEquals($task2goesfirst ? '\core\task\adhoc_test2_task' : '\core\task\adhoc_test_task', + $item->classname); + $this->assertLessThanOrEqual($after, $item->timestarted); + $this->assertGreaterThanOrEqual($before, $item->timestarted); + } + + // Mark the second task running and check results. + $next2 = manager::get_next_adhoc_task(time()); + manager::adhoc_task_starting($next2); + $running = manager::get_running_tasks(); + $this->assertCount(2, $running); + if ($task2goesfirst) { + $item = array_shift($running); + $this->assertEquals('\core\task\adhoc_test2_task', $item->classname); + $item = array_shift($running); + $this->assertEquals('\core\task\adhoc_test_task', $item->classname); + } else { + $item = array_shift($running); + $this->assertEquals('\core\task\adhoc_test_task', $item->classname); + $item = array_shift($running); + $this->assertEquals('\core\task\adhoc_test2_task', $item->classname); + } + + // Second task completes successfully. + manager::adhoc_task_complete($next2); + $running = manager::get_running_tasks(); + $this->assertCount(1, $running); + foreach ($running as $item) { + $this->assertEquals($task2goesfirst ? '\core\task\adhoc_test2_task' : '\core\task\adhoc_test_task', + $item->classname); + } + + // First task fails. + manager::adhoc_task_failed($next1); + $running = manager::get_running_tasks(); + $this->assertCount(0, $running); + } + + /** + * Test for scheduled tasks. + */ + public function test_scheduled_task_running() { + global $DB; + $this->resetAfterTest(); + + // Check no tasks are marked running. + $running = manager::get_running_tasks(); + $this->assertEmpty($running); + + // Disable all the tasks, except two, and set those two due to run. + $DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ? AND classname != ?', + ['\core\task\session_cleanup_task', '\core\task\file_trash_cleanup_task']); + $DB->set_field('task_scheduled', 'nextruntime', 1, + ['classname' => '\core\task\session_cleanup_task']); + $DB->set_field('task_scheduled', 'nextruntime', 1, + ['classname' => '\core\task\file_trash_cleanup_task']); + $DB->set_field('task_scheduled', 'lastruntime', time() - 1000, + ['classname' => '\core\task\session_cleanup_task']); + $DB->set_field('task_scheduled', 'lastruntime', time() - 500, + ['classname' => '\core\task\file_trash_cleanup_task']); + + // Get the first task and start it off. + $next1 = manager::get_next_scheduled_task(time()); + $before = time(); + manager::scheduled_task_starting($next1); + $after = time(); + $running = manager::get_running_tasks(); + $this->assertCount(1, $running); + foreach ($running as $item) { + $this->assertLessThanOrEqual($after, $item->timestarted); + $this->assertGreaterThanOrEqual($before, $item->timestarted); + $this->assertEquals('\core\task\session_cleanup_task', $item->classname); + } + + // Mark the second task running and check results. We have to change the times so the other + // one comes up first, otherwise it repeats the same one. + $DB->set_field('task_scheduled', 'lastruntime', time() - 1500, + ['classname' => '\core\task\file_trash_cleanup_task']); + + // Make sure that there is a time gap between task to sort them as expected. + sleep(1); + $next2 = manager::get_next_scheduled_task(time()); + manager::scheduled_task_starting($next2); + + // Check default sorting by timestarted. + $running = manager::get_running_tasks(); + $this->assertCount(2, $running); + $item = array_shift($running); + $this->assertEquals('\core\task\session_cleanup_task', $item->classname); + $item = array_shift($running); + $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname); + + // Check sorting by time ASC. + $running = manager::get_running_tasks('time ASC'); + $this->assertCount(2, $running); + $item = array_shift($running); + $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname); + $item = array_shift($running); + $this->assertEquals('\core\task\session_cleanup_task', $item->classname); + + // Complete the file trash one. + manager::scheduled_task_complete($next2); + $running = manager::get_running_tasks(); + $this->assertCount(1, $running); + + // Other task fails. + manager::scheduled_task_failed($next1); + $running = manager::get_running_tasks(); + $this->assertCount(0, $running); + } +} diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 60a675eba42..fef4e504ee8 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -36,6 +36,9 @@ information provided here is intended especially for developers. a callback function instead of an array of options. * Admin setting admin_setting_configselect now supports validating the selection by supplying a callback function. +* The task system has new functions adhoc_task_starting() and scheduled_task_starting() which must + be called before executing a task, and a new function \core\task\manager::get_running_tasks() + returns information about currently-running tasks. === 3.9 === * Following function has been deprecated, please use \core\task\manager::run_from_cli(). diff --git a/version.php b/version.php index 7d5bcfe5844..0eb75416613 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.04; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.05; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20200822)'; // Human-friendly version name