Commit 35b76161 authored by jun's avatar jun
Browse files

Merge branch 'MDL-60514-master-new-cron-execution' of

parents c3d1878f c759ae5d
......@@ -7,7 +7,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
// "systempaths" settingpage
$temp = new admin_settingpage('systempaths', new lang_string('systempaths','admin'));
$temp->add(new admin_setting_configexecutable('pathtophp', new lang_string('pathtophp', 'admin'),
new lang_string('configpathtophp', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('pathtodu', new lang_string('pathtodu', 'admin'), new lang_string('configpathtodu', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('aspellpath', new lang_string('aspellpath', 'admin'), new lang_string('edhelpaspellpath'), ''));
$temp->add(new admin_setting_configexecutable('pathtodot', new lang_string('pathtodot', 'admin'), new lang_string('pathtodot_help', 'admin'), ''));
// This file is part of Moodle -
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <>.
* Form for scheduled tasks admin pages.
* @package tool_task
* @copyright 2018 Toni Barbera <>
* @license GNU GPL v3 or later
namespace tool_task;
defined('MOODLE_INTERNAL') || die();
* Running tasks from CLI.
* @copyright 2018 Toni Barbera <>
* @license 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 {
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 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.
return true;
......@@ -25,6 +25,7 @@
$string['asap'] = 'ASAP';
$string['backtoscheduledtasks'] = 'Back to scheduled tasks';
$string['blocking'] = 'Blocking';
$string['cannotfindthepathtothecli'] = 'Cannot find the path to the PHP CLI executable so task execution aborted. Set the "Path to PHP CLI" setting in "Site administration / Server / System paths"';
$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';
......@@ -58,3 +59,4 @@ $string['taskscheduleminute_help'] = 'Minute field for task schedule. The field
$string['taskschedulemonth'] = 'Month';
$string['taskschedulemonth_help'] = 'Month field for task schedule. The field uses the same format as unix cron. Some examples are:<br/><ul><li><strong>*</strong> Every month</li><li><strong>*/2</strong> Every second month</li><li><strong>1</strong> Every January</li><li><strong>1,5</strong> Every January and May</li></ul>';
$string['privacy:metadata'] = 'The Scheduled task configuration plugin does not store any personal data.';
......@@ -62,6 +62,7 @@ class tool_task_renderer extends plugin_renderer_base {
$asap = get_string('asap', 'tool_task');
$disabledstr = get_string('taskdisabled', 'tool_task');
$plugindisabledstr = get_string('plugindisabled', 'tool_task');
$runnabletasks = tool_task\run_from_cli::is_runnable();
foreach ($tasks as $task) {
$customised = $task->is_customised() ? $no : $yes;
if (empty($CFG->preventscheduledtaskchanges)) {
......@@ -105,7 +106,7 @@ class tool_task_renderer extends plugin_renderer_base {
$runnow = '';
if (!$disabled && get_config('tool_task', 'enablerunnow')) {
if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {
$runnow = html_writer::div(html_writer::link(
new moodle_url('/admin/tool/task/schedule_task.php',
array('task' => get_class($task))),
......@@ -88,7 +88,8 @@ 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).
echo html_writer::end_tag('pre');
$output = $PAGE->get_renderer('tool_task');
......@@ -872,6 +872,11 @@ $CFG->admin = 'admin';
// and 'gsdll32.dll' to a new folder without a space in the path)
// $CFG->pathtogs = '/usr/bin/gs';
// Path to PHP CLI.
// Probably something like /usr/bin/php. If you enter this, cron scripts can be
// executed from admin web interface.
// $CFG->pathtophp = '';
// Path to du.
// Probably something like /usr/bin/du. If you enter this, pages that display
// directory contents will run much faster for directories with a lot of files.
......@@ -302,6 +302,7 @@ $string['configoverride'] = 'Defined in config.php';
$string['configpasswordpolicy'] = 'Turning this on will make Moodle check user passwords against a valid password policy. Use the settings below to specify your policy (they will be ignored if you set this to \'No\').';
$string['configpasswordresettime'] = 'This specifies the amount of time people have to validate a password reset request before it expires. Usually 30 minutes is a good value.';
$string['configpathtodu'] = 'Path to du. Probably something like /usr/bin/du. If you enter this, pages that display directory contents will run much faster for directories with a lot of files.';
$string['configpathtophp'] = 'Path to PHP CLI. Probably something like /usr/bin/php. If you enter this, cron scripts can be executed from admin web interface.';
$string['configperfdebug'] = 'If you turn this on, performance info will be printed in the footer of the standard theme';
$string['configprofileroles'] = 'List of roles that are visible on user profiles and participation page.';
$string['configprofilesforenrolledusersonly'] = 'To prevent misuse by spammers, profile descriptions of users who are not yet enrolled in any course are hidden. New users must enrol in at least one course before they can add a profile description.';
......@@ -860,6 +861,7 @@ $string['passwordreuselimit'] = 'Password rotation limit';
$string['passwordreuselimit_desc'] = 'Number of times a user must change their password before they are allowed to reuse a password. Hashes of previously used passwords are stored in local database table. This feature might not be compatible with some external authentication plugins.';
$string['pathtodot'] = 'Path to dot';
$string['pathtodot_help'] = 'Path to dot. On Linux it is something like /usr/bin/dot. On Windows it is something like C:\Program Files (x86)\Graphviz2.38\bin\dot.exe. On Mac it is something like /opt/local/bin/dot. To be able to generate graphics from DOT files, you must have installed the dot executable and point to it here.';
$string['pathtophp'] = 'Path to PHP CLI';
$string['pathtodu'] = 'Path to du';
$string['pathtogs'] = 'Path to ghostscript';
$string['pathtogs_help'] = 'On most Linux installs, this can be left as \'/usr/bin/gs\'. On Windows it will be something like \'c:\\gs\\bin\\gswin32c.exe\' (make sure there are no spaces in the path - if necessary copy the files \'gswin32c.exe\' and \'gsdll32.dll\' to a new folder without a space in the path)';
......@@ -219,7 +219,7 @@ function behat_clean_init_config() {
'wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
'umaskpermissions', 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix',
'dboptions', 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword',
'proxybypass', 'theme', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade',
'proxybypass', 'theme', 'pathtogs', 'pathtophp', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade',
'altcacheconfigpath', 'pathtounoconv', 'alternative_file_system_class', 'pathtopython'
......@@ -181,7 +181,7 @@ $CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CF
$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
'altcacheconfigpath', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot',
'altcacheconfigpath', 'pathtogs', 'pathtphp', 'pathtodu', 'aspellpath', 'pathtodot',
'pathtounoconv', 'alternative_file_system_class', 'pathtopython'
$productioncfg = (array)$CFG;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment