mirror of
https://github.com/moodle/moodle.git
synced 2025-07-25 08:11:39 +02:00
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.
This commit is contained in:
@@ -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 <toni@moodle.com>
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
|
@@ -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');
|
||||
|
||||
|
@@ -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))),
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user