From db15746c2dbde3b051d88b9dabfe8b99f0da28e4 Mon Sep 17 00:00:00 2001 From: cescobedo Date: Thu, 13 Feb 2020 18:08:59 +0100 Subject: [PATCH] MDL-63580 core_task: Deprecation cron_run_single_task and run_from_cli Also we have move the functions in \tool_task\run_from_cli to \core\task\manager and we have deprecated that class. --- admin/tool/task/classes/run_from_cli.php | 35 +++------- admin/tool/task/renderer.php | 2 +- admin/tool/task/schedule_task.php | 2 +- h5p/classes/helper.php | 2 +- lib/classes/task/manager.php | 59 +++++++++++++++++ lib/cronlib.php | 83 ------------------------ lib/deprecatedlib.php | 19 ++++++ 7 files changed, 90 insertions(+), 112 deletions(-) diff --git a/admin/tool/task/classes/run_from_cli.php b/admin/tool/task/classes/run_from_cli.php index 06e24ef9cba..9b91a142384 100644 --- a/admin/tool/task/classes/run_from_cli.php +++ b/admin/tool/task/classes/run_from_cli.php @@ -17,6 +17,9 @@ /** * 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.3 MDL-63594. + * * @package tool_task * @copyright 2018 Toni Barbera * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -55,7 +58,9 @@ class run_from_cli { * @return bool */ public static function is_runnable():bool { - return self::find_php_cli_path() !== false; + debugging('run_from_cli class is deprecated. Please use \core\task\manager::run_from_cli() instead.', + DEBUG_DEVELOPER); + return \core\task\manager::is_runnable(); } /** @@ -66,30 +71,8 @@ class run_from_cli { * @throws \moodle_exception */ public static function execute(\core\task\task_base $task):bool { - global $CFG; - - if (!self::is_runnable()) { - $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']); - throw new \moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out()); - } else { - // Shell-escaped path to the PHP binary. - $phpbinary = escapeshellarg(self::find_php_cli_path()); - - // Shell-escaped path CLI script. - $pathcomponents = [$CFG->dirroot, $CFG->admin, 'tool', 'task', 'cli', 'schedule_task.php']; - $scriptpath = escapeshellarg(implode(DIRECTORY_SEPARATOR, $pathcomponents)); - - // Shell-escaped task name. - $classname = get_class($task); - $taskarg = escapeshellarg("--execute={$classname}"); - - // Build the CLI command. - $command = "{$phpbinary} {$scriptpath} {$taskarg}"; - - // Execute it. - passthru($command); - } - - return true; + 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/renderer.php b/admin/tool/task/renderer.php index 0a01d0acdea..a003eab27a8 100644 --- a/admin/tool/task/renderer.php +++ b/admin/tool/task/renderer.php @@ -75,7 +75,7 @@ class tool_task_renderer extends plugin_renderer_base { $data = []; $yes = get_string('yes'); $no = get_string('no'); - $canruntasks = tool_task\run_from_cli::is_runnable(); + $canruntasks = \core\task\manager::is_runnable(); foreach ($tasks as $task) { $classname = get_class($task); $defaulttask = \core\task\manager::get_default_scheduled_task($classname, false); diff --git a/admin/tool/task/schedule_task.php b/admin/tool/task/schedule_task.php index b404a82f583..1b0de98ed27 100644 --- a/admin/tool/task/schedule_task.php +++ b/admin/tool/task/schedule_task.php @@ -88,7 +88,7 @@ echo html_writer::start_tag('pre'); $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper'; // Run the specified task (this will output an error if it doesn't exist). -\tool_task\run_from_cli::execute($task); +\core\task\manager::run_from_cli($task); echo html_writer::end_tag('pre'); diff --git a/h5p/classes/helper.php b/h5p/classes/helper.php index f34236ea5c4..34be78b3195 100644 --- a/h5p/classes/helper.php +++ b/h5p/classes/helper.php @@ -259,7 +259,7 @@ class helper { '/admin/tool/task/scheduledtasks.php', array('action' => 'edit', 'task' => get_class($task)) ); - if ($status && \tool_task\run_from_cli::is_runnable() && get_config('tool_task', 'enablerunnow')) { + if ($status && \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow')) { $statusaction = \html_writer::link( new \moodle_url('/admin/tool/task/schedule_task.php', array('task' => get_class($task))), diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index fdf4e3eb96f..7bf811b9c2f 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -910,4 +910,63 @@ class manager { return null; } + + /** + * 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 { + return self::find_php_cli_path() !== false; + } + + /** + * 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 run_from_cli(\core\task\task_base $task):bool { + global $CFG; + + if (!self::is_runnable()) { + $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']); + throw new \moodle_exception('cannotfindthepathtothecli', 'core_task', $redirecturl->out()); + } else { + // Shell-escaped path to the PHP binary. + $phpbinary = escapeshellarg(self::find_php_cli_path()); + + // Shell-escaped path CLI script. + $pathcomponents = [$CFG->dirroot, $CFG->admin, 'cli', 'scheduled_task.php']; + $scriptpath = escapeshellarg(implode(DIRECTORY_SEPARATOR, $pathcomponents)); + + // Shell-escaped task name. + $classname = get_class($task); + $taskarg = escapeshellarg("--execute={$classname}"); + + // Build the CLI command. + $command = "{$phpbinary} {$scriptpath} {$taskarg}"; + + // Execute it. + passthru($command); + } + + return true; + } } diff --git a/lib/cronlib.php b/lib/cronlib.php index f15fae8dc85..917e5b5f1f7 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -365,89 +365,6 @@ function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) { get_mailer('close'); } -/** - * 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! - * - * @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) { - global $CFG, $DB, $USER; - - if (CLI_MAINTENANCE) { - echo "CLI maintenance mode active, cron execution suspended.\n"; - return false; - } - - if (moodle_needs_upgrading()) { - echo "Moodle upgrade pending, cron execution suspended.\n"; - return false; - } - - // Check task and component is not disabled. - $taskname = get_class($task); - if ($task->get_disabled()) { - echo "Task is disabled ($taskname).\n"; - return false; - } - $component = $task->get_component(); - if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) { - if ($plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) { - echo "Component is not enabled ($component).\n"; - return false; - } - } - - // Enable debugging features as per config settings. - if (!empty($CFG->showcronsql)) { - $DB->set_debug(true); - } - if (!empty($CFG->showcrondebugging)) { - set_debugging(DEBUG_DEVELOPER, true); - } - - // Increase time and memory limits. - core_php_time_limit::raise(); - raise_memory_limit(MEMORY_EXTRA); - - // Switch to admin account for cron tasks, but close the session so we don't send this stuff - // to the browser. - session_write_close(); - $realuser = clone($USER); - cron_setup_user(null, null, true); - - // Get lock for cron task. - $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); - if (!$cronlock = $cronlockfactory->get_lock('core_cron', 1)) { - echo "Unable to get cron lock.\n"; - return false; - } - if (!$lock = $cronlockfactory->get_lock($taskname, 1)) { - $cronlock->release(); - echo "Unable to get task lock for $taskname.\n"; - return false; - } - $task->set_lock($lock); - if (!$task->is_blocking()) { - $cronlock->release(); - } else { - $task->set_cron_lock($cronlock); - } - - // Run actual tasks. - cron_run_inner_scheduled_task($task); - - // Go back to real user account. - cron_setup_user($realuser, null, true); - - return true; -} - /** * Output some standard information during cron runs. Specifically current time * and memory usage. This method also does gc_collect_cycles() (before displaying diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 01b389e59e5..938746e1847 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3406,3 +3406,22 @@ function get_module_metadata($course, $modnames, $sectionreturn = null) { core_collator::asort_objects_by_property($return, 'title'); return $return; } + +/** + * 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.3 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); +}