From 1b28ca2520ec230cd6d88488fd43c37436833b8f Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Fri, 27 May 2022 18:30:29 +0200 Subject: [PATCH 1/2] MDL-63594 core: Final deprecation and removal of cron_run_single_task() Signed-off-by: Daniel Ziegenberg --- lib/deprecatedlib.php | 16 +++------------- lib/upgrade.txt | 3 ++- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 9d9208c882c..92385cba87f 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -2869,22 +2869,12 @@ function get_module_metadata($course, $modnames, $sectionreturn = null) { } /** - * Runs a single cron task. This function assumes it is displaying output in pseudo-CLI mode. - * - * The function will fail if the task is disabled. - * - * Warning: Because this function closes the browser session, it may not be safe to continue - * with other processing (other than displaying the rest of the page) after using this function! - * * @deprecated since Moodle 3.9 MDL-63580. Please use the \core\task\manager::run_from_cli($task). - * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594. - * @param \core\task\scheduled_task $task Task to run - * @return bool True if cron run successful */ function cron_run_single_task(\core\task\scheduled_task $task) { - debugging('cron_run_single_task() is deprecated. Please use \\core\task\manager::run_from_cli() instead.', - DEBUG_DEVELOPER); - return \core\task\manager::run_from_cli($task); + throw new coding_exception( + 'cron_run_single_task() has been removed. Please use \\core\task\manager::run_from_cli() instead.' + ); } /** diff --git a/lib/upgrade.txt b/lib/upgrade.txt index f4fcd3a0298..6de407576bf 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -3,7 +3,8 @@ information provided here is intended especially for developers. === 4.1 === -* The method ensure_adhoc_task_qos() in lib/classes/task/manager.php has been deprecated, please use get_next_adhoc_task() +* Final deprecation and removal of cron_run_single_task(), please use \core\task\manager::run_from_cli(). +* The method ensure_adhoc_task_qos() in lib/classes/task/manager.php has been deprecated, please use get_next_adhoc_task() instead. * New setting $CFG->enrolments_sync_interval controls the minimum time in seconds between re-synchronization of enrollment via enrol_check_plugins. This only applies to web requests without a session such as webservice calls, tokenpluginfile.php and rss links Function From e2d788f61732bdf243a26391ab4dbbdae9f3f198 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Fri, 27 May 2022 18:56:32 +0200 Subject: [PATCH 2/2] MDL-63594 core: Final deprecation of CLI script from admin/tool/task/cli Following MDL-63580 this now finally deprecates and removes the class \tool_task\run_from_cli, please use \core\task\manager. Following MDL-63580 this now finally deprecates and removes - admin/tool/task/cli/schedule_task.php, use admin/cli/scheduled_task.php - admin/tool/task/cli/adhoc_task.php, use admin/cli/adhoc_task.php Also mentions and references to the old CLI scripts are updated to point to the new counterparts. Signed-off-by: Daniel Ziegenberg --- admin/tool/task/classes/run_from_cli.php | 78 --------- admin/tool/task/cli/adhoc_task.php | 126 --------------- admin/tool/task/cli/schedule_task.php | 191 ----------------------- enrol/flatfile/cli/sync.php | 6 +- lib/tests/behat/behat_general.php | 2 +- lib/upgrade.txt | 5 + 6 files changed, 9 insertions(+), 399 deletions(-) delete mode 100644 admin/tool/task/classes/run_from_cli.php delete mode 100644 admin/tool/task/cli/adhoc_task.php delete mode 100644 admin/tool/task/cli/schedule_task.php diff --git a/admin/tool/task/classes/run_from_cli.php b/admin/tool/task/classes/run_from_cli.php deleted file mode 100644 index 35a722e3841..00000000000 --- a/admin/tool/task/classes/run_from_cli.php +++ /dev/null @@ -1,78 +0,0 @@ -. - -/** - * Form for scheduled tasks admin pages. - * - * @deprecated since Moodle 3.9 MDL-63580. Please use the \core\task\manager. - * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594. - * - * @package tool_task - * @copyright 2018 Toni Barbera - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace tool_task; - -defined('MOODLE_INTERNAL') || die(); - -/** - * Running tasks from CLI. - * - * @copyright 2018 Toni Barbera - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class run_from_cli { - - /** - * Find the path of PHP CLI binary. - * - * @return string|false The PHP CLI executable PATH - */ - protected static function find_php_cli_path() { - global $CFG; - - if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) { - return $CFG->pathtophp; - } - - return false; - } - - /** - * Returns if Moodle have access to PHP CLI binary or not. - * - * @return bool - */ - public static function is_runnable():bool { - debugging('run_from_cli class is deprecated. Please use \core\task\manager::run_from_cli() instead.', - DEBUG_DEVELOPER); - return \core\task\manager::is_runnable(); - } - - /** - * Executes a cron from web invocation using PHP CLI. - * - * @param \core\task\task_base $task Task that be executed via CLI. - * @return bool - * @throws \moodle_exception - */ - public static function execute(\core\task\task_base $task):bool { - debugging('run_from_cli class is deprecated. Please use \core\task\manager::run_from_cli() instead.', - DEBUG_DEVELOPER); - return \core\task\manager::run_from_cli($task); - } -} diff --git a/admin/tool/task/cli/adhoc_task.php b/admin/tool/task/cli/adhoc_task.php deleted file mode 100644 index 0c1135beffe..00000000000 --- a/admin/tool/task/cli/adhoc_task.php +++ /dev/null @@ -1,126 +0,0 @@ -. - -/** - * Task executor for adhoc tasks. - * - * @deprecated since Moodle 3.9 MDL-63580. Please use the admin/cli/adhoc_task.php. - * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594. - * - * @package tool_task - * @copyright 2018 Andrew Nicols - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -define('CLI_SCRIPT', true); - -require(__DIR__ . '/../../../../config.php'); -require_once("{$CFG->libdir}/clilib.php"); -require_once("{$CFG->libdir}/cronlib.php"); - -list($options, $unrecognized) = cli_get_params( - [ - 'execute' => false, - 'help' => false, - 'keep-alive' => 0, - 'showsql' => false, - 'showdebugging' => false, - 'ignorelimits' => false, - ], [ - 'h' => 'help', - 'e' => 'execute', - 'k' => 'keep-alive', - 'i' => 'ignorelimits', - ] -); - -debugging('admin/tool/task/cli/adhoc_task.php is deprecated. Please use admin/cli/adhoc_task.php instead.', DEBUG_DEVELOPER); - -if ($unrecognized) { - $unrecognized = implode("\n ", $unrecognized); - cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); -} - -if ($options['help'] or empty($options['execute'])) { - $help = <<set_debug(true); -} - -if (CLI_MAINTENANCE) { - echo "CLI maintenance mode active, cron execution suspended.\n"; - exit(1); -} - -if (moodle_needs_upgrading()) { - echo "Moodle upgrade pending, cron execution suspended.\n"; - exit(1); -} - -if (empty($options['execute'])) { - exit(0); -} -if (empty($options['keep-alive'])) { - $options['keep-alive'] = 0; -} - -if (!empty($CFG->showcronsql)) { - $DB->set_debug(true); -} -if (!empty($CFG->showcrondebugging)) { - set_debugging(DEBUG_DEVELOPER, true); -} - -$checklimits = empty($options['ignorelimits']); - -core_php_time_limit::raise(); - -// Increase memory limit. -raise_memory_limit(MEMORY_EXTRA); - -// Emulate normal session - we use admin account by default. -cron_setup_user(); - -$humantimenow = date('r', time()); -$keepalive = (int)$options['keep-alive']; - -\core\local\cli\shutdown::script_supports_graceful_exit(); - -mtrace("Server Time: {$humantimenow}\n"); -cron_run_adhoc_tasks(time(), $keepalive, $checklimits); diff --git a/admin/tool/task/cli/schedule_task.php b/admin/tool/task/cli/schedule_task.php deleted file mode 100644 index 5b1038a8616..00000000000 --- a/admin/tool/task/cli/schedule_task.php +++ /dev/null @@ -1,191 +0,0 @@ -. - -/** - * CLI task execution. - * - * @deprecated since Moodle 3.9 MDL-63580. Please use the admin/cli/schedule_task.php. - * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594. - * - * @package tool_task - * @copyright 2014 Petr Skoda - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -define('CLI_SCRIPT', true); - -require(__DIR__ . '/../../../../config.php'); -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') -); - -debugging('admin/tool/task/cli/schedule_task.php is deprecated. Please use admin/cli/scheduled_task.php instead.', DEBUG_DEVELOPER); - -if ($unrecognized) { - $unrecognized = implode("\n ", $unrecognized); - cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); -} - -if ($options['help'] or (!$options['list'] and !$options['execute'])) { - $help = -"Scheduled cron tasks. - -Options: ---execute=\\\\some\\\\task Execute scheduled task manually ---list List all scheduled tasks ---showsql Show sql queries before they are executed ---showdebugging Show developer level debugging information --h, --help Print out this help - -Example: -\$sudo -u www-data /usr/bin/php admin/tool/task/cli/schedule_task.php --execute=\\\\core\\\\task\\\\session_cleanup_task - -"; - - echo $help; - die; -} - -if ($options['showdebugging']) { - set_debugging(DEBUG_DEVELOPER, true); -} - -if ($options['showsql']) { - $DB->set_debug(true); -} - -if ($options['list']) { - cli_heading("List of scheduled tasks ($CFG->wwwroot)"); - - $shorttime = get_string('strftimedatetimeshort'); - - $tasks = \core\task\manager::get_all_scheduled_tasks(); - echo str_pad(get_string('scheduledtasks', 'tool_task'), 50, ' ') . ' ' . str_pad(get_string('runpattern', 'tool_task'), 17, ' ') - . ' ' . str_pad(get_string('lastruntime', 'tool_task'), 40, ' ') . get_string('nextruntime', 'tool_task') . "\n"; - foreach ($tasks as $task) { - $class = '\\' . get_class($task); - $schedule = $task->get_minute() . ' ' - . $task->get_hour() . ' ' - . $task->get_day() . ' ' - . $task->get_day_of_week() . ' ' - . $task->get_month() . ' ' - . $task->get_day_of_week(); - $nextrun = $task->get_next_run_time(); - $lastrun = $task->get_last_run_time(); - - $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); - $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled(); - - if ($plugindisabled) { - $nextrun = get_string('plugindisabled', 'tool_task'); - } else if ($task->get_disabled()) { - $nextrun = get_string('taskdisabled', 'tool_task'); - } else if ($nextrun > time()) { - $nextrun = userdate($nextrun); - } else { - $nextrun = get_string('asap', 'tool_task'); - } - - if ($lastrun) { - $lastrun = userdate($lastrun); - } else { - $lastrun = get_string('never'); - } - - echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . - ' ' . str_pad($lastrun, 40, ' ') . ' ' . $nextrun . "\n"; - } - exit(0); -} - -if ($execute = $options['execute']) { - if (!$task = \core\task\manager::get_scheduled_task($execute)) { - mtrace("Task '$execute' not found"); - exit(1); - } - - if (moodle_needs_upgrading()) { - mtrace("Moodle upgrade pending, cannot execute tasks."); - exit(1); - } - - // Increase memory limit. - raise_memory_limit(MEMORY_EXTRA); - - // Emulate normal session - we use admin account by default. - cron_setup_user(); - - $predbqueries = $DB->perf_get_queries(); - $pretime = microtime(true); - - \core\task\logmanager::start_logging($task); - $fullname = $task->get_name() . ' (' . get_class($task) . ')'; - mtrace('Execute scheduled task: ' . $fullname); - // NOTE: it would be tricky to move this code to \core\task\manager class, - // because we want to do detailed error reporting. - $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); - if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) { - mtrace('Cannot obtain cron lock'); - exit(129); - } - if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) { - $cronlock->release(); - mtrace('Cannot obtain task lock'); - exit(130); - } - - $task->set_lock($lock); - if (!$task->is_blocking()) { - $cronlock->release(); - } else { - $task->set_cron_lock($cronlock); - } - - try { - get_mailer('buffer'); - $task->execute(); - if (isset($predbqueries)) { - mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries"); - mtrace("... used " . (microtime(1) - $pretime) . " seconds"); - } - mtrace('Scheduled task complete: ' . $fullname); - \core\task\manager::scheduled_task_complete($task); - get_mailer('close'); - exit(0); - } catch (Exception $e) { - if ($DB->is_transaction_started()) { - $DB->force_transaction_rollback(); - } - mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries"); - mtrace("... used " . (microtime(true) - $pretime) . " seconds"); - mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage()); - if ($CFG->debugdeveloper) { - if (!empty($e->debuginfo)) { - mtrace("Debug info:"); - mtrace($e->debuginfo); - } - mtrace("Backtrace:"); - mtrace(format_backtrace($e->getTrace(), true)); - } - \core\task\manager::scheduled_task_failed($task); - get_mailer('close'); - exit(1); - } -} diff --git a/enrol/flatfile/cli/sync.php b/enrol/flatfile/cli/sync.php index 2b248c10c3d..834a2dcd29d 100644 --- a/enrol/flatfile/cli/sync.php +++ b/enrol/flatfile/cli/sync.php @@ -27,14 +27,14 @@ * This plugin now has a enrolment sync scheduled task. Scheduled tasks were * introduced in Moodle 2.7. It is possible to override the scheduled tasks * configuration and run a single scheduled task immediately using the - * admin/tool/task/cli/schedule_task.php script. This is the recommended + * admin/cli/scheduled_task.php script. This is the recommended * method to use for immediate enrollment synchronisation. * * Usage help: - * $ php admin/tool/task/cli/schedule_task.php -h + * $ php admin/cli/scheduled_task.php -h * * Execute task: - * $ sudo -u www-data /usr/bin/php admin/tool/task/cli/schedule_task.php / + * $ sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php / * --execute=\\enrol_flatfile\\task\\flatfile_sync_task * * @package enrol_flatfile diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index ae75bf74b78..8cb02b45bdc 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -1085,7 +1085,7 @@ EOF; * the Behat result look ugly. * * Note: Most of the code relating to running a task is based on - * admin/tool/task/cli/schedule_task.php. + * admin/cli/scheduled_task.php. * * @Given /^I run the scheduled task "(?P[^"]+)"$/ * @param string $taskname Name of task e.g. 'mod_whatever\task\do_something' diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 6de407576bf..cfeaff0c717 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -3,6 +3,11 @@ information provided here is intended especially for developers. === 4.1 === +* Final deprecation and removal of the following class, please use \core\task\manager. + - \tool_task\run_from_cli +* Final deprecation and removal of the following CLI scripts: + - admin/tool/task/cli/schedule_task.php please use admin/cli/scheduled_task.php + - admin/tool/task/cli/adhoc_task.php please use admin/cli/adhoc_task.php * Final deprecation and removal of cron_run_single_task(), please use \core\task\manager::run_from_cli(). * The method ensure_adhoc_task_qos() in lib/classes/task/manager.php has been deprecated, please use get_next_adhoc_task() instead.