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:
cescobedo
2020-05-11 10:53:23 +02:00
parent 079736ccb3
commit db15746c2d
7 changed files with 90 additions and 112 deletions
+59
View File
@@ -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;
}
}