mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 01:16:44 +02:00
MDL-55980 Scheduled tasks: Run individual scheduled tasks from web
This commit is contained in:
parent
0f59b6dd75
commit
38fa1ca558
10 changed files with 348 additions and 48 deletions
|
@ -59,6 +59,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
|
|||
$temp->add(new admin_setting_configcheckbox('cronclionly', new lang_string('cronclionly', 'admin'), new lang_string
|
||||
('configcronclionly', 'admin'), 1));
|
||||
$temp->add(new admin_setting_configpasswordunmask('cronremotepassword', new lang_string('cronremotepassword', 'admin'), new lang_string('configcronremotepassword', 'admin'), ''));
|
||||
$temp->add(new admin_setting_configcheckbox('tool_task/enablerunnow', new lang_string('enablerunnow', 'tool_task'),
|
||||
new lang_string('enablerunnow_desc', 'tool_task'), 1));
|
||||
|
||||
$options = array(0=>get_string('no'), 3=>3, 5=>5, 7=>7, 10=>10, 20=>20, 30=>30, 50=>50, 100=>100);
|
||||
$temp->add(new admin_setting_configselect('lockoutthreshold', new lang_string('lockoutthreshold', 'admin'), new lang_string('lockoutthreshold_desc', 'admin'), 0, $options));
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
*/
|
||||
|
||||
$string['asap'] = 'ASAP';
|
||||
$string['backtoscheduledtasks'] = 'Back to scheduled tasks';
|
||||
$string['blocking'] = 'Blocking';
|
||||
$string['component'] = 'Component';
|
||||
$string['corecomponent'] = 'Core';
|
||||
|
@ -30,6 +31,8 @@ $string['default'] = 'Default';
|
|||
$string['disabled'] = 'Disabled';
|
||||
$string['disabled_help'] = 'Disabled scheduled tasks are not executed from cron, however they can still be executed manually via the CLI tool.';
|
||||
$string['edittaskschedule'] = 'Edit task schedule: {$a}';
|
||||
$string['enablerunnow'] = 'Allow ‘Run now’ for scheduled tasks';
|
||||
$string['enablerunnow_desc'] = 'Allows administrators to run a single scheduled task immediately, rather than waiting for it to run as scheduled. The task runs on the web server, so some sites may wish to disable this feature to avoid potential performance issues.';
|
||||
$string['faildelay'] = 'Fail delay';
|
||||
$string['lastruntime'] = 'Last run';
|
||||
$string['nextruntime'] = 'Next run';
|
||||
|
@ -37,6 +40,8 @@ $string['plugindisabled'] = 'Plugin disabled';
|
|||
$string['pluginname'] = 'Scheduled task configuration';
|
||||
$string['resettasktodefaults'] = 'Reset task schedule to defaults';
|
||||
$string['resettasktodefaults_help'] = 'This will discard any local changes and revert the schedule for this task back to its original settings.';
|
||||
$string['runnow'] = 'Run now';
|
||||
$string['runnow_confirm'] = 'Are you sure you want to run this task ‘{$a}’ now? The task will run on the web server and may take some time to complete.';
|
||||
$string['scheduledtasks'] = 'Scheduled tasks';
|
||||
$string['scheduledtaskchangesdisabled'] = 'Modifications to the list of scheduled tasks have been prevented in Moodle configuration';
|
||||
$string['taskdisabled'] = 'Task disabled';
|
||||
|
|
|
@ -104,11 +104,19 @@ class tool_task_renderer extends plugin_renderer_base {
|
|||
$nextrun = $asap;
|
||||
}
|
||||
|
||||
$runnow = '';
|
||||
if (!$disabled && get_config('tool_task', 'enablerunnow')) {
|
||||
$runnow = html_writer::div(html_writer::link(
|
||||
new moodle_url('/admin/tool/task/schedule_task.php',
|
||||
array('task' => get_class($task))),
|
||||
get_string('runnow', 'tool_task')), 'task-runnow');
|
||||
}
|
||||
|
||||
$row = new html_table_row(array(
|
||||
$namecell,
|
||||
$componentcell,
|
||||
new html_table_cell($editlink),
|
||||
new html_table_cell($lastrun),
|
||||
new html_table_cell($lastrun . $runnow),
|
||||
new html_table_cell($nextrun),
|
||||
new html_table_cell($task->get_minute()),
|
||||
new html_table_cell($task->get_hour()),
|
||||
|
@ -133,4 +141,14 @@ class tool_task_renderer extends plugin_renderer_base {
|
|||
$table->data = $data;
|
||||
return html_writer::table($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a link back to the scheduled tasks page (used from the 'run now' screen).
|
||||
*
|
||||
* @return string HTML code
|
||||
*/
|
||||
public function link_back() {
|
||||
return $this->render_from_template('tool_task/link_back',
|
||||
array('url' => new moodle_url('/admin/tool/task/scheduledtasks.php')));
|
||||
}
|
||||
}
|
||||
|
|
101
admin/tool/task/schedule_task.php
Normal file
101
admin/tool/task/schedule_task.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// 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
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Web cron single task
|
||||
*
|
||||
* This script runs a single scheduled task from the web UI.
|
||||
*
|
||||
* @package tool_task
|
||||
* @copyright 2016 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require('../../../config.php');
|
||||
|
||||
require_once($CFG->libdir.'/cronlib.php');
|
||||
|
||||
/**
|
||||
* Function used to handle mtrace by outputting the text to normal browser window.
|
||||
*
|
||||
* @param string $message Message to output
|
||||
* @param string $eol End of line character
|
||||
*/
|
||||
function tool_task_mtrace_wrapper($message, $eol) {
|
||||
echo s($message . $eol);
|
||||
// Both types of flush may be necessary in order to actually output progressively to browser.
|
||||
// It depends on the theme.
|
||||
if (ob_get_status()) {
|
||||
ob_flush();
|
||||
}
|
||||
flush();
|
||||
}
|
||||
|
||||
// Allow execution of single task. This requires login and has different rules.
|
||||
$taskname = required_param('task', PARAM_RAW_TRIMMED);
|
||||
|
||||
// Basic security checks.
|
||||
require_login();
|
||||
$context = context_system::instance();
|
||||
require_capability('moodle/site:config', $context);
|
||||
|
||||
if (!get_config('tool_task', 'enablerunnow')) {
|
||||
print_error('nopermissions', 'error', '', get_string('runnow', 'tool_task'));
|
||||
}
|
||||
|
||||
// Check input parameter against all existing tasks (this ensures it isn't possible to
|
||||
// create some kind of security problem by specifying a class that isn't a task or whatever).
|
||||
$task = \core\task\manager::get_scheduled_task($taskname);
|
||||
if (!$task) {
|
||||
print_error('cannotfindinfo', 'error', $taskname);
|
||||
}
|
||||
|
||||
// Start output.
|
||||
$PAGE->set_url(new moodle_url('/admin/tool/task/schedule_task.php'));
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->navbar->add(get_string('scheduledtasks', 'tool_task'), new moodle_url('/admin/tool/task/scheduledtasks.php'));
|
||||
$PAGE->navbar->add(s($task->get_name()));
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading($task->get_name());
|
||||
|
||||
// The initial request just shows the confirmation page; we don't do anything further unless
|
||||
// they confirm.
|
||||
if (!optional_param('confirm', 0, PARAM_INT)) {
|
||||
echo $OUTPUT->confirm(get_string('runnow_confirm', 'tool_task', $task->get_name()),
|
||||
new single_button(new moodle_url('/admin/tool/task/schedule_task.php',
|
||||
array('task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey())),
|
||||
get_string('runnow', 'tool_task')),
|
||||
new single_button(new moodle_url('/admin/tool/task/scheduledtasks.php'),
|
||||
get_string('cancel'), false));
|
||||
echo $OUTPUT->footer();
|
||||
exit;
|
||||
}
|
||||
|
||||
// Action requires session key.
|
||||
require_sesskey();
|
||||
|
||||
// Prepare to handle output via mtrace.
|
||||
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).
|
||||
cron_run_single_task($task);
|
||||
echo html_writer::end_tag('pre');
|
||||
|
||||
$output = $PAGE->get_renderer('tool_task');
|
||||
echo $output->link_back();
|
||||
|
||||
echo $OUTPUT->footer();
|
|
@ -9,3 +9,7 @@
|
|||
/*rtl:ignore*/
|
||||
direction: ltr;
|
||||
}
|
||||
|
||||
#page-admin-tool-task-scheduledtasks .task-runnow {
|
||||
font-size: 0.75em;
|
||||
}
|
||||
|
|
31
admin/tool/task/templates/link_back.mustache
Normal file
31
admin/tool/task/templates/link_back.mustache
Normal file
|
@ -0,0 +1,31 @@
|
|||
{{!
|
||||
This file is part of Moodle - http://moodle.org/
|
||||
|
||||
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
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
}}
|
||||
{{!
|
||||
@template tool_task/link_back
|
||||
|
||||
Template for displaying link back from another page to the scheduled tasks page.
|
||||
|
||||
Context variables required for this template:
|
||||
* url - moodle_url|string URL of scheduled tasks page
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"url" : "https://www.example.org/admin/tool/task/scheduledtasks.php"
|
||||
}
|
||||
|
||||
}}
|
||||
<p><a href="{{url}}">{{# str }} backtoscheduledtasks, tool_task {{/str}}</a></p>
|
35
admin/tool/task/tests/behat/run_task_now.feature
Normal file
35
admin/tool/task/tests/behat/run_task_now.feature
Normal file
|
@ -0,0 +1,35 @@
|
|||
@tool @tool_task
|
||||
Feature: Run tasks from web interface
|
||||
In order to run scheduled tasks immediately
|
||||
As an admin
|
||||
I need to be able to run a task from the web interface
|
||||
|
||||
Scenario: Run a task
|
||||
Given I log in as "admin"
|
||||
When I navigate to "Scheduled tasks" node in "Site administration > Server"
|
||||
Then I should see "Never" in the "Log table cleanup" "table_row"
|
||||
|
||||
And I click on "Run now" "text" in the "Log table cleanup" "table_row"
|
||||
And I should see "Are you sure you want to run this task"
|
||||
And I press "Run now"
|
||||
|
||||
And I should see "Log table cleanup" in the "h2" "css_element"
|
||||
And I should see "Scheduled task complete: Log table cleanup"
|
||||
|
||||
And I follow "Back to scheduled tasks"
|
||||
And I should not see "Never" in the "Log table cleanup" "table_row"
|
||||
|
||||
Scenario: Cancel running a task
|
||||
Given I log in as "admin"
|
||||
When I navigate to "Scheduled tasks" node in "Site administration > Server"
|
||||
And I click on "Run now" "text" in the "Log table cleanup" "table_row"
|
||||
And I press "Cancel"
|
||||
# Confirm we're back on the scheduled tasks page by looking for the table.
|
||||
Then "Log table cleanup" "table_row" should exist
|
||||
|
||||
Scenario: Cannot run a task when the option is disabled
|
||||
Given the following config values are set as admin:
|
||||
| enablerunnow | 0 | tool_task |
|
||||
When I log in as "admin"
|
||||
And I navigate to "Scheduled tasks" node in "Site administration > Server"
|
||||
Then I should not see "Run now"
|
Loading…
Add table
Add a link
Reference in a new issue