first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
<?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/>.
/**
* Ad hoc task list.
*
* @package tool_task
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('adhoctasks');
$failedonly = optional_param('failedonly', false, PARAM_BOOL);
$classname = optional_param('classname', null, PARAM_RAW);
$renderer = $PAGE->get_renderer('tool_task');
if ($classname) {
$pageurl = new moodle_url('/admin/tool/task/adhoctasks.php');
$PAGE->navbar->add(get_string('adhoctasks', 'tool_task'), $pageurl);
$PAGE->navbar->add(s($classname), $PAGE->url);
$tasks = core\task\manager::get_adhoc_tasks($classname, $failedonly);
echo $OUTPUT->header();
if (!get_config('core', 'cron_enabled')) {
echo $renderer->cron_disabled();
}
echo $renderer->adhoc_tasks_class_table($classname, $tasks, ['failedonly' => $failedonly]);
} else {
$summary = core\task\manager::get_adhoc_tasks_summary();
echo $OUTPUT->header();
if (!get_config('core', 'cron_enabled')) {
echo $renderer->cron_disabled();
}
echo $renderer->adhoc_tasks_summary_table($summary);
}
echo $OUTPUT->footer();
@@ -0,0 +1,88 @@
<?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/>.
namespace tool_task\check;
use action_link;
use core\check\check;
use core\check\result;
use moodle_url;
/**
* Ad hoc queue checks
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adhocqueue extends check {
/**
* Return result
* @return result
*/
public function get_result(): result {
global $DB, $CFG;
$stats = $DB->get_record_sql('
SELECT count(*) cnt,
MAX(? - nextruntime) age
FROM {task_adhoc}', [time()]);
$status = result::OK;
$summary = get_string('adhocempty', 'tool_task');
$details = '';
if ($stats->cnt > 0) {
// A large queue size by itself is not an issue, only when tasks
// are not being processed in a timely fashion is it an issue.
$status = result::INFO;
$summary = get_string('adhocqueuesize', 'tool_task', $stats->cnt);
}
$max = $CFG->adhoctaskagewarn ?? 10 * MINSECS;
if ($stats->age > $max) {
$status = result::WARNING;
$summary = get_string('adhocqueueold', 'tool_task', [
'age' => format_time($stats->age),
'max' => format_time($max),
]);
}
$max = $CFG->adhoctaskageerror ?? 4 * HOURSECS;
if ($stats->age > $max) {
$status = result::ERROR;
$summary = get_string('adhocqueueold', 'tool_task', [
'age' => format_time($stats->age),
'max' => format_time($max),
]);
}
return new result($status, $summary, $details);
}
/**
* Link to the Ad hoc tasks report
*
* @return action_link|null
*/
public function get_action_link(): ?action_link {
return new action_link(
new moodle_url('/admin/tool/task/adhoctasks.php'),
get_string('adhoctasks', 'tool_task'),
);
}
}
@@ -0,0 +1,125 @@
<?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/>.
/**
* Cron running check
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_task\check;
defined('MOODLE_INTERNAL') || die();
use core\check\check;
use core\check\result;
/**
* Cron running check
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cronrunning extends check {
/**
* A link the running tasks report
*
* @return action_link|null
*/
public function get_action_link(): ?\action_link {
return new \action_link(
new \moodle_url('/admin/tool/task/runningtasks.php'),
get_string('runningtasks', 'tool_task'));
}
/**
* Return result
* @return result
*/
public function get_result(): result {
global $CFG;
// Eventually this should replace cron_overdue_warning and
// cron_infrequent_warning.
$lastcron = get_config('tool_task', 'lastcronstart');
$expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
$delta = time() - $lastcron;
$lastcroninterval = get_config('tool_task', 'lastcroninterval');
$formatdelta = format_time($delta);
$formatexpected = format_time($expectedfrequency);
$formatinterval = format_time($lastcroninterval);
// Inform user the time since last cron start.
$details = get_string('lastcronstart', 'tool_task', $formatdelta);
if ($delta > $expectedfrequency + MINSECS) {
$status = result::WARNING;
if ($delta > DAYSECS) {
$status = result::CRITICAL;
}
if (empty($lastcron)) {
if (empty($CFG->cronclionly)) {
$url = new \moodle_url('/admin/cron.php');
$summary = get_string('cronwarningneverweb', 'admin', [
'url' => $url->out(),
'expected' => $formatexpected,
]);
} else {
$summary = get_string('cronwarningnever', 'admin', [
'expected' => $formatexpected,
]);
}
} else if (empty($CFG->cronclionly)) {
$url = new \moodle_url('/admin/cron.php');
$summary = get_string('cronwarning', 'admin', [
'url' => $url->out(),
'actual' => $formatdelta,
'expected' => $formatexpected,
]);
} else {
$summary = get_string('cronwarningcli', 'admin', [
'actual' => $formatdelta,
'expected' => $formatexpected,
]);
}
return new result($status, $summary, $details);
}
// Add MINSECS to avoid spurious warning if cron is only a few seconds overdue.
if ($lastcroninterval > $expectedfrequency + MINSECS) {
$status = result::WARNING;
$summary = get_string('croninfrequent', 'admin', [
'actual' => $formatinterval,
'expected' => $formatexpected,
]);
return new result($status, $summary, $details);
}
$status = result::OK;
$summary = get_string('cronok', 'tool_task');
return new result($status, $summary, $details);
}
}
@@ -0,0 +1,89 @@
<?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/>.
namespace tool_task\check;
use core\check\check;
use core\check\result;
use core\task\manager;
/**
* Long running tasks check
*
* @package tool_task
* @author Qihui Chan (qihuichan@catalyst-au.net)
* @copyright 2022 Catalyst IT Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class longrunningtasks extends check {
/**
* Links to the running task list
*
* @return \action_link|null
* @throws \coding_exception
*/
public function get_action_link(): ?\action_link {
$url = new \moodle_url('/admin/tool/task/runningtasks.php');
return new \action_link($url, get_string('runningtasks', 'tool_task'));
}
/**
* Return result
* @return result
*/
public function get_result(): result {
global $CFG;
$status = result::OK;
$slowtasks = 0;
$details = '';
$maxruntime = 0;
$runtime = 0;
$tasks = \core\task\manager::get_running_tasks();
foreach ($tasks as $record) {
$taskmethod = "{$record->type}_task_from_record";
$task = manager::$taskmethod($record);
$taskname = $task->get_name();
$result = $task->get_runtime_result();
$taskstatus = $result->get_status();
$runtime = $task->get_runtime();
$runtimedetails = get_string('taskrunningtime', 'tool_task', format_time($runtime));
$maxruntime = ($runtime > $maxruntime) ? $runtime : $maxruntime;
if ($taskstatus == result::OK) {
continue;
}
$slowtasks++;
$details .= strtoupper($taskstatus) . ": {$taskname}. {$runtimedetails} <br>";
// The overall check status is the worst tasks status.
if ($status !== result::ERROR) {
$status = $taskstatus;
}
}
$summary = get_string('checklongrunningtaskcount', 'tool_task', $slowtasks);
$conclusion = get_string('taskdetails', 'tool_task', ['count' => $slowtasks,
'time' => format_time($CFG->taskruntimewarn), 'maxtime' => format_time($maxruntime)]);
$details = ($slowtasks ? $conclusion : $summary) . "<br>{$details}";
return new result($status, $summary, $details);
}
}
@@ -0,0 +1,107 @@
<?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/>.
/**
* Task fail delay check
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_task\check;
defined('MOODLE_INTERNAL') || die();
use core\check\check;
use core\check\result;
/**
* Task fail delay check
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class maxfaildelay extends check {
/**
* Links to the task log report
*
* @return \action_link|null
*/
public function get_action_link(): ?\action_link {
$url = new \moodle_url('/admin/tasklogs.php');
return new \action_link($url, get_string('tasklogs', 'tool_task'));
}
/**
* Return result
* @return result
*/
public function get_result(): result {
global $CFG;
$status = result::OK;
$summary = get_string('tasknofailures', 'tool_task');
$details = '';
$failures = 0;
$maxdelay = 0;
$tasks = \core\task\manager::get_all_scheduled_tasks();
foreach ($tasks as $task) {
if ($task->get_disabled()) {
continue;
}
$faildelay = $task->get_fail_delay();
if ($faildelay > $maxdelay) {
$maxdelay = $faildelay;
}
if ($faildelay > 0) {
$failures++;
$details .= get_string('faildelay', 'tool_task') . ': ' . format_time($faildelay);
$details .= ' - ' . $task->get_name() . ' (' .get_class($task) . ")<br>";
}
}
$tasks = \core\task\manager::get_failed_adhoc_tasks();
foreach ($tasks as $task) {
$faildelay = $task->get_fail_delay();
if ($faildelay > $maxdelay) {
$maxdelay = $faildelay;
}
if ($faildelay > 0) {
$failures++;
$details .= get_string('faildelay', 'tool_task') . ': ' . format_time($faildelay);
$details .= ' - ' .get_class($task) . " ID = " . $task->get_id() ."<br>";
}
}
if ($failures > 0) {
// Intermittent failures are not yet a warning.
$status = result::INFO;
$summary = get_string('taskfailures', 'tool_task', $failures);
}
if ($maxdelay > 5 * MINSECS) {
$status = result::WARNING;
}
if ($maxdelay > 4 * HOURSECS) {
$status = result::ERROR;
}
return new result($status, $summary, $details);
}
}
@@ -0,0 +1,153 @@
<?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/>.
/**
* Form for scheduled tasks admin pages.
*
* @package tool_task
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Edit scheduled task form.
*
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_task_edit_scheduled_task_form extends moodleform {
public function definition() {
global $PAGE;
$mform = $this->_form;
/** @var \core\task\scheduled_task $task */
$task = $this->_customdata;
$defaulttask = \core\task\manager::get_default_scheduled_task(get_class($task), false);
$renderer = $PAGE->get_renderer('tool_task');
$mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'),
$renderer->last_run_time($task));
$mform->addElement('static', 'nextrun', get_string('nextruntime', 'tool_task'),
$renderer->next_run_time($task));
$mform->addGroup([
$mform->createElement('text', 'minute'),
$mform->createElement('static', 'minutedefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_minute())),
], 'minutegroup', get_string('taskscheduleminute', 'tool_task'), null, false);
$mform->setType('minute', PARAM_RAW);
$mform->addHelpButton('minutegroup', 'taskscheduleminute', 'tool_task');
$mform->addGroup([
$mform->createElement('text', 'hour'),
$mform->createElement('static', 'hourdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_hour())),
], 'hourgroup', get_string('taskschedulehour', 'tool_task'), null, false);
$mform->setType('hour', PARAM_RAW);
$mform->addHelpButton('hourgroup', 'taskschedulehour', 'tool_task');
$mform->addGroup([
$mform->createElement('text', 'day'),
$mform->createElement('static', 'daydefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_day())),
], 'daygroup', get_string('taskscheduleday', 'tool_task'), null, false);
$mform->setType('day', PARAM_RAW);
$mform->addHelpButton('daygroup', 'taskscheduleday', 'tool_task');
$mform->addGroup([
$mform->createElement('text', 'month'),
$mform->createElement('static', 'monthdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_month())),
], 'monthgroup', get_string('taskschedulemonth', 'tool_task'), null, false);
$mform->setType('month', PARAM_RAW);
$mform->addHelpButton('monthgroup', 'taskschedulemonth', 'tool_task');
$mform->addGroup([
$mform->createElement('text', 'dayofweek'),
$mform->createElement('static', 'dayofweekdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_day_of_week())),
], 'dayofweekgroup', get_string('taskscheduledayofweek', 'tool_task'), null, false);
$mform->setType('dayofweek', PARAM_RAW);
$mform->addHelpButton('dayofweekgroup', 'taskscheduledayofweek', 'tool_task');
$mform->addElement('advcheckbox', 'disabled', get_string('disabled', 'tool_task'));
$mform->addHelpButton('disabled', 'disabled', 'tool_task');
$mform->addElement('advcheckbox', 'resettodefaults', get_string('resettasktodefaults', 'tool_task'));
$mform->addHelpButton('resettodefaults', 'resettasktodefaults', 'tool_task');
$mform->disabledIf('minute', 'resettodefaults', 'checked');
$mform->disabledIf('hour', 'resettodefaults', 'checked');
$mform->disabledIf('day', 'resettodefaults', 'checked');
$mform->disabledIf('dayofweek', 'resettodefaults', 'checked');
$mform->disabledIf('month', 'resettodefaults', 'checked');
$mform->disabledIf('disabled', 'resettodefaults', 'checked');
$mform->addElement('hidden', 'task', get_class($task));
$mform->setType('task', PARAM_RAW);
$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_ALPHANUMEXT);
$this->add_action_buttons(true, get_string('savechanges'));
// Do not use defaults for existing values, the set_data() is the correct way.
$this->set_data(\core\task\manager::record_from_scheduled_task($task));
}
/**
* Custom validations.
*
* @param array $data
* @param array $files
*
* @return array
*/
public function validation($data, $files) {
$error = parent::validation($data, $files);
// Use a checker class.
$checker = new \tool_task\scheduled_checker_task();
$checker->set_minute($data['minute']);
$checker->set_hour($data['hour']);
$checker->set_month($data['month']);
$checker->set_day_of_week($data['dayofweek']);
$checker->set_day($data['day']);
$checker->set_disabled(false);
$checker->set_customised(false);
if (!$checker->is_valid($checker::FIELD_MINUTE)) {
$error['minutegroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_HOUR)) {
$error['hourgroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_DAY)) {
$error['daygroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_MONTH)) {
$error['monthgroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_DAYOFWEEK)) {
$error['dayofweekgroup'] = get_string('invaliddata', 'core_error');
}
return $error;
}
}
@@ -0,0 +1,46 @@
<?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/>.
/**
* Privacy Subsystem implementation for tool_task.
*
* @package tool_task
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_task\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for tool_task implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -0,0 +1,156 @@
<?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/>.
/**
* Running tasks table.
*
* @package tool_task
* @copyright 2019 The Open University
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_task;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/tablelib.php');
use core\task\manager;
/**
* Table to display list of running task.
*
* @copyright 2019 The Open University
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class running_tasks_table extends \table_sql {
/**
* Constructor for the running tasks table.
*/
public function __construct() {
parent::__construct('runningtasks');
$columnheaders = [
'classname' => get_string('classname', 'tool_task'),
'type' => get_string('tasktype', 'admin'),
'time' => get_string('taskage', 'tool_task'),
'timestarted' => get_string('started', 'tool_task'),
'hostname' => get_string('hostname', 'tool_task'),
'pid' => get_string('pid', 'tool_task'),
];
$this->define_columns(array_keys($columnheaders));
$this->define_headers(array_values($columnheaders));
// The name column is a header.
$this->define_header_column('classname');
// This table is not collapsible.
$this->collapsible(false);
// Allow pagination.
$this->pageable(true);
}
/**
* Query the db. Store results in the table object for use by build_table.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar. Bar
* will only be used if there is a fullname column defined for the table.
* @throws \dml_exception
*/
public function query_db($pagesize, $useinitialsbar = true) {
$sort = $this->get_sql_sort();
$this->rawdata = \core\task\manager::get_running_tasks($sort);
}
/**
* Format the classname cell.
*
* @param \stdClass $row
* @return string
*/
public function col_classname($row): string {
$output = $row->classname;
if ($row->type == 'scheduled') {
if (class_exists($row->classname)) {
$task = new $row->classname;
if ($task instanceof \core\task\scheduled_task) {
$output .= \html_writer::tag('div', $task->get_name(), ['class' => 'task-class']);
}
}
} else if ($row->type == 'adhoc') {
$output .= \html_writer::tag('div',
get_string('adhoctaskid', 'tool_task', $row->id), ['class' => 'task-class']);
}
return $output;
}
/**
* Format the type cell.
*
* @param \stdClass $row
* @return string
* @throws \coding_exception
*/
public function col_type($row): string {
if ($row->type == 'scheduled') {
$output = \html_writer::span(get_string('scheduled', 'tool_task'), 'badge bg-primary text-white');
} else if ($row->type == 'adhoc') {
$output = \html_writer::span(get_string('adhoc', 'tool_task'), 'badge bg-dark text-white');
} else {
// This shouldn't ever happen.
$output = '';
}
return $output;
}
/**
* Format the time cell.
*
* @param \stdClass $row
* @return string
*/
public function col_time($row): string {
global $OUTPUT;
$taskmethod = "{$row->type}_task_from_record";
$task = manager::$taskmethod($row);
$result = $task->get_runtime_result();
$extra = '';
if ($result->get_status() != $result::OK) {
$extra = '<br>';
$extra .= $OUTPUT->check_result($result);
$extra .= ' ';
$extra .= $result->get_details();
}
return format_time($row->time) . $extra;
}
/**
* Format the timestarted cell.
*
* @param \stdClass $row
* @return string
*/
public function col_timestarted($row): string {
return userdate($row->timestarted);
}
}
@@ -0,0 +1,40 @@
<?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/>.
namespace tool_task;
/**
* Checker class. Fake scheduled task used only to check that crontab settings are valid.
*
* @package tool_task
* @copyright 2021 Jordi Pujol-Ahulló <jpahullo@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduled_checker_task extends \core\task\scheduled_task {
/**
* Gets the checker task name.
*/
public function get_name() {
return "Checker task";
}
/**
* Does nothing.
*/
public function execute() {
}
}
+68
View File
@@ -0,0 +1,68 @@
<?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/>.
/**
* Script clears the fail delay for a task and reschedules its next execution.
*
* @package tool_task
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require('../../../config.php');
// Basic security checks.
require_admin();
$context = context_system::instance();
// Get task and check the parameter is valid.
$taskname = required_param('task', PARAM_RAW_TRIMMED);
$task = \core\task\manager::get_scheduled_task($taskname);
if (!$task) {
throw new \moodle_exception('cannotfindinfo', 'error', $taskname);
}
$returnurl = new moodle_url('/admin/tool/task/scheduledtasks.php',
['lastchanged' => get_class($task)]);
// If actually doing the clear, then carry out the task and redirect to the scheduled task page.
if (optional_param('confirm', 0, PARAM_INT)) {
require_sesskey();
\core\task\manager::clear_fail_delay($task);
redirect($returnurl);
}
// 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()));
$PAGE->navbar->add(get_string('clear'));
echo $OUTPUT->header();
// The initial request just shows the confirmation page; we don't do anything further unless
// they confirm.
echo $OUTPUT->confirm(get_string('clearfaildelay_confirm', 'tool_task', $task->get_name()),
new single_button(new moodle_url('/admin/tool/task/clear_fail_delay.php',
['task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey()]),
get_string('clear')),
new single_button($returnurl, get_string('cancel'), false));
echo $OUTPUT->footer();
+142
View File
@@ -0,0 +1,142 @@
<?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/>.
/**
* Strings for component 'tool_task', language 'en'
*
* @package tool_task
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['adhoc'] = 'Ad hoc';
$string['adhoctaskid'] = 'Ad hoc task ID: {$a}';
$string['adhoctaskrun'] = 'Ad hoc task run initiated';
$string['adhoctasks'] = 'Ad hoc tasks';
$string['adhoctasksdue'] = 'Ad hoc tasks due';
$string['adhoctasksfailed'] = 'Ad hoc tasks failed';
$string['adhoctasksfuture'] = 'Future ad hoc tasks';
$string['adhoctasksrunning'] = 'Ad hoc tasks running';
$string['asap'] = 'ASAP';
$string['adhocempty'] = 'Ad hoc task queue is empty';
$string['adhocqueuesize'] = 'Ad hoc task queue has {$a} tasks';
$string['adhocqueueold'] = 'Oldest unprocessed task is {$a->age}, which is more than {$a->max}';
$string['backtoadhoctasks'] = 'Back to ad hoc tasks';
$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['checkadhocqueue'] = 'Ad hoc task queue';
$string['checkcronrunning'] = 'Cron running';
$string['checkmaxfaildelay'] = 'Tasks max fail delay';
$string['classname'] = 'Class name';
$string['checklongrunningtasks'] = 'Long running tasks';
$string['checklongrunningtaskcount'] = 'Long running tasks: {$a}';
$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';
$string['crondisabled'] = 'Cron is disabled. No new tasks will be started. The system will not operate properly until it is enabled again.';
$string['cronok'] = 'Cron is running frequently';
$string['default'] = 'Default';
$string['defaultx'] = 'Default: {$a}';
$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 feature requires \'Path to PHP CLI\' (pathtophp) to be set in System paths. The task runs on the web server, so you may wish to disable this feature to avoid potential performance issues.';
$string['faildelay'] = 'Fail delay';
$string['failed'] = 'Failed';
$string['fromcomponent'] = 'From component: {$a}';
$string['hostname'] = 'Host name';
$string['lastcronstart'] = 'Time since last cron run: {$a}';
$string['lastruntime'] = 'Last run';
$string['lastupdated'] = 'Last updated {$a}.';
$string['nextruntime'] = 'Next run';
$string['noclassname'] = 'Class name not specified';
$string['notasks'] = 'No tasks to run';
$string['payload'] = 'Payload';
$string['pid'] = 'PID';
$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['run_adhoctasks'] = 'Run ad hoc tasks';
$string['runningalltasks'] = 'Running all tasks';
$string['runningfailedtasks'] = 'Running failed tasks';
$string['runningtasks'] = 'Tasks running now';
$string['runnow'] = 'Run now';
$string['runagain'] = 'Run again';
$string['runadhoc_confirm'] = 'Tasks will run on the web server and may take some time to complete.';
$string['runadhoc'] = 'Run ad hoc tasks now?';
$string['runadhoctask'] = 'Run \'{$a->task}\' task ID {$a->taskid}';
$string['runadhoctasks'] = 'Run all \'{$a}\' tasks';
$string['runadhoctasksfailed'] = 'Run failed \'{$a}\' tasks';
$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['runclassname'] = 'Run all';
$string['runclassnamefailedonly'] = 'Run all failed';
$string['runpattern'] = 'Run pattern';
$string['scheduled'] = 'Scheduled';
$string['scheduledtasks'] = 'Scheduled tasks';
$string['scheduledtaskchangesdisabled'] = 'Modifications to the list of scheduled tasks have been prevented in Moodle configuration';
$string['slowtask'] = 'Task has run for longer than {$a}';
$string['showall'] = 'Show all';
$string['showfailedonly'] = 'Show failed only';
$string['showsummary'] = 'Show ad hoc tasks summary';
$string['started'] = 'Started';
$string['taskage'] = 'Run time';
$string['taskdetails'] = 'Tasks running for more than {$a->time} (max {$a->maxtime}): {$a->count}';
$string['taskdisabled'] = 'Task disabled';
$string['taskfailures'] = '{$a} task(s) failing';
$string['taskid'] = 'Task ID';
$string['tasklogs'] = 'Task logs';
$string['tasknofailures'] = 'There are no tasks failing';
$string['taskrunningtime'] = 'Task has run for {$a}';
$string['taskscheduleday'] = 'Day';
$string['taskscheduleday_help'] = 'Day of month field for task schedule. The field uses the same format as unix cron. Some examples are:
* <strong>*</strong> Every day
* <strong>*/2</strong> Every 2nd day
* <strong>1</strong> The first of every month
* <strong>1,15</strong> The first and fifteenth of every month';
$string['taskscheduledayofweek'] = 'Day of week';
$string['taskscheduledayofweek_help'] = 'Day of week field for task schedule. The field uses the same format as unix cron. Some examples are:
* <strong>*</strong> Every day
* <strong>0</strong> Every Sunday
* <strong>6</strong> Every Saturday
* <strong>1,5</strong> Every Monday and Friday';
$string['taskschedulehour'] = 'Hour';
$string['taskschedulehour_help'] = 'Hour field for task schedule. The field uses the same format as unix cron. Some examples are:
* <strong>*</strong> Every hour
* <strong>*/2</strong> Every 2 hours
* <strong>2-10</strong> Every hour from 2am until 10am (inclusive)
* <strong>2,6,9</strong> 2am, 6am and 9am';
$string['taskscheduleminute'] = 'Minute';
$string['taskscheduleminute_help'] = 'Minute field for task schedule. The field uses the same format as unix cron. Some examples are:
* <strong>*</strong> Every minute
* <strong>*/5</strong> Every 5 minutes
* <strong>2-10</strong> Every minute between 2 and 10 past the hour (inclusive)
* <strong>2,6,9</strong> 2, 6 and 9 minutes past the hour';
$string['taskschedulemonth'] = 'Month';
$string['taskschedulemonth_help'] = 'Month field for task schedule. The field uses the same format as unix cron. Some examples are:
* <strong>*</strong> Every month
* <strong>*/2</strong> Every second month
* <strong>1</strong> Every January
* <strong>1,5</strong> Every January and May';
$string['privacy:metadata'] = 'The Scheduled task configuration plugin does not store any personal data.';
$string['viewlogs'] = 'View logs for {$a}';
+65
View File
@@ -0,0 +1,65 @@
<?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/>.
/**
* Task API status checks
*
* @package tool_task
* @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Add cron related service status checks
*
* @return array of check objects
*/
function tool_task_status_checks(): array {
return [
new \tool_task\check\cronrunning(),
new \tool_task\check\maxfaildelay(),
new \tool_task\check\adhocqueue(),
new \tool_task\check\longrunningtasks(),
];
}
/**
* 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(string $message, string $eol): void {
$message = s($message);
// We autolink urls and emails here but can't use format_text as it does
// more than we need and has side effects which are not useful in this context.
$urlpattern = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
$message = preg_replace_callback($urlpattern, function($matches) {
$url = $matches[0];
return html_writer::link($url, $url, ['target' => '_blank']);
}, $message);
$emailpattern = '/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/';
$message = preg_replace_callback($emailpattern, function($matches) {
$email = $matches[0];
return html_writer::link('mailto:' . $email, $email);
}, $message);
echo $message . $eol;
}
+571
View File
@@ -0,0 +1,571 @@
<?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/>.
/**
* Output rendering for the plugin.
*
* @package tool_task
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core\task\scheduled_task;
/**
* Implements the plugin renderer
*
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_task_renderer extends plugin_renderer_base {
/**
* This function will render a table with the summary of all adhoc tasks.
*
* @param array $summary
* @return string HTML to output.
*/
public function adhoc_tasks_summary_table(array $summary): string {
$adhocurl = '/admin/tool/task/adhoctasks.php';
$adhocrunurl = '/admin/tool/task/run_adhoctasks.php';
// Main tasks table.
$table = new html_table();
$table->caption = get_string('adhoctasks', 'tool_task');
$table->head = [
get_string('component', 'tool_task') . ' / ' . get_string('classname', 'tool_task'),
get_string('adhoctasksrunning', 'tool_task'),
get_string('adhoctasksdue', 'tool_task'),
get_string('adhoctasksfuture', 'tool_task'),
get_string('adhoctasksfailed', 'tool_task'),
get_string('nextruntime', 'tool_task'),
];
$table->attributes['class'] = 'admintable generaltable';
$table->colclasses = [];
// For each task entry (row) show action buttons/logs link depending on the user permissions.
$data = [];
$canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow');
foreach ($summary as $component => $classes) {
// Component cell.
$componentcell = new html_table_cell($component);
$componentcell->header = true;
$componentcell->id = "tasks-$component";
$componentcell->colspan = 6;
$data[] = new html_table_row([$componentcell]);
foreach ($classes as $classname => $stats) {
// Task class cell.
$classbits = explode('\\', $classname);
$classcontent = html_writer::link(
new moodle_url($adhocurl, ['classname' => $classname]),
end($classbits)
);
$classcell = new html_table_cell($classcontent);
$classcell->header = true;
$classcell->attributes['class'] = "task-class-summary text-ltr";
$duecontent = $stats['due'];
if ($canruntasks && ($stats['due'] > 0 || $stats['failed'] > 0)) {
$duecontent .= html_writer::div(
html_writer::link(
new moodle_url(
$adhocrunurl,
['classname' => $classname]
),
get_string('runclassname', 'tool_task')
),
'task-runnow'
);
}
// Mark cell if has failed tasks.
$failed = $stats['failed'];
if ($canruntasks && $failed > 0) {
$failed .= html_writer::div(
html_writer::link(
new moodle_url(
$adhocrunurl,
['classname' => $classname, 'failedonly' => 1]
),
get_string('runclassnamefailedonly', 'tool_task')
),
'task-runnow'
);
}
$failedcell = new html_table_cell($failed);
if ($failed > 0) {
$failedcell->attributes['class'] = 'table-danger';
}
// Prepares the next run time cell contents.
$nextrun = '';
if ($stats['stop']) {
$nextrun = get_string('never', 'admin');
} else if ($stats['due'] > 0) {
$nextrun = get_string('asap', 'tool_task');
} else if ($stats['nextruntime']) {
$nextrun = userdate($stats['nextruntime']);
}
$data[] = new html_table_row([
$classcell,
new html_table_cell($stats['running']),
new html_table_cell($duecontent),
new html_table_cell($stats['count'] - $stats['running'] - $stats['due']),
$failedcell,
new html_table_cell($nextrun),
]);
}
}
$table->data = $data;
return html_writer::table($table);
}
/**
* This function will render a table with all the adhoc tasks for the class.
*
* @param string $classname
* @param array $tasks - list of all adhoc tasks.
* @param array|null $params
* @return string HTML to output.
*/
public function adhoc_tasks_class_table(string $classname, array $tasks, ?array $params = []): string {
$adhocurl = '/admin/tool/task/adhoctasks.php';
$adhocrunurl = '/admin/tool/task/run_adhoctasks.php';
$showloglink = \core\task\logmanager::has_log_report();
$failedonly = !empty($params['failedonly']);
$canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow');
// Depending on the currently set parameters, set up toggle buttons.
$failedorall = html_writer::link(
new moodle_url(
$adhocurl,
array_merge($params, ['classname' => $classname, 'failedonly' => !$failedonly])
),
get_string($failedonly ? 'showall' : 'showfailedonly', 'tool_task')
);
// Main tasks table.
$table = $this->generate_adhoc_tasks_simple_table($tasks, $canruntasks);
$table->caption = s($classname) . " "
. get_string($failedonly ? 'adhoctasksfailed' : 'adhoctasks', 'tool_task');
$table->head[3] .= " $failedorall"; // Spice up faildelay heading.
if ($showloglink) {
// Insert logs as the second col.
array_splice($table->head, 1, 0, [get_string('logs')]);
array_walk($table->data, function ($row, $idx) use ($classname) {
$loglink = '';
$faildelaycell = $row->cells[3];
if ($faildelaycell->attributes['class'] == 'table-danger') {
// Failed task.
$loglink = $this->output->action_icon(
\core\task\logmanager::get_url_for_task_class($classname),
new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $classname)
));
}
array_splice($row->cells, 1, 0, [new html_table_cell($loglink)]);
});
}
return html_writer::table($table)
. html_writer::div(
html_writer::link(
new moodle_url(
$adhocrunurl,
array_merge($params, ['classname' => $classname])
),
get_string('runclassname', 'tool_task')
),
'task-runnow'
)
. html_writer::div(
html_writer::link(
new moodle_url(
$adhocurl
),
get_string('showsummary', 'tool_task')
),
'task-show-summary'
);
}
/**
* This function will render a plain adhoc tasks table.
*
* @param array $tasks - list of adhoc tasks.
* @return string HTML to output.
*/
public function adhoc_tasks_simple_table(array $tasks): string {
$table = $this->generate_adhoc_tasks_simple_table($tasks);
return html_writer::table($table);
}
/**
* This function will render a plain adhoc tasks table.
*
* @param array $tasks - list of adhoc tasks.
* @param bool $wantruntasks add 'Run now' link
* @return html_table
*/
private function generate_adhoc_tasks_simple_table(array $tasks, bool $wantruntasks = false): html_table {
$adhocrunurl = '/admin/tool/task/run_adhoctasks.php';
$now = time();
$failedstr = get_string('failed', 'tool_task');
// Main tasks table.
$table = new html_table();
$table->caption = get_string('adhoctasks', 'tool_task');
$table->head = [
get_string('taskid', 'tool_task'),
get_string('nextruntime', 'tool_task'),
get_string('payload', 'tool_task'),
$failedstr
];
$table->attributes['class'] = 'generaltable';
$table->colclasses = [];
// For each task entry (row) show action buttons/logs link depending on the user permissions.
$data = [];
foreach ($tasks as $task) {
$taskid = $task->get_id();
$started = $task->get_timestarted();
// Task id cell.
$taskidcellcontent = html_writer::span($taskid, 'task-id');
$taskidcell = new html_table_cell($taskidcellcontent);
$taskidcell->header = true;
$taskidcell->id = "task-$taskid";
// Mark cell if task has failed.
$faildelay = $task->get_fail_delay();
$faildelaycell = new html_table_cell($faildelay ? $failedstr : '');
if ($faildelay) {
$faildelaycell->attributes['class'] = 'table-danger';
}
// Prepares the next run time cell contents.
$nextrun = get_string('started', 'tool_task');
if (!$started) {
$nextruntime = $task->get_next_run_time();
$due = $nextruntime < $now;
if ($task->get_attempts_available() > 0) {
$nextrun = $due ? userdate($nextruntime) : get_string('asap', 'tool_task');
} else {
$nextrun = get_string('never', 'admin');
}
if ($wantruntasks && ($faildelay || $due)) {
$nextrun .= ' '.html_writer::div(
html_writer::link(
new moodle_url(
$adhocrunurl,
['id' => $taskid]
),
get_string('runnow', 'tool_task')
),
'task-runnow'
);
}
}
$data[] = new html_table_row([
$taskidcell,
new html_table_cell($nextrun),
new html_table_cell($task->get_custom_data_as_string()),
$faildelaycell,
]);
}
$table->data = $data;
return $table;
}
/**
* Displays a notification on ad hoc task run request.
*
* @return string HTML notification block for task initiated message
*/
public function adhoc_task_run(): string {
return $this->output->notification(get_string('adhoctaskrun', 'tool_task'), 'info');
}
/**
* This function will render one beautiful table with all the scheduled tasks.
*
* @param \core\task\scheduled_task[] $tasks - list of all scheduled tasks.
* @param string $lastchanged (optional) the last task edited. Gets highlighted in teh table.
* @return string HTML to output.
*/
public function scheduled_tasks_table($tasks, $lastchanged = '') {
global $CFG;
$showloglink = \core\task\logmanager::has_log_report();
$table = new html_table();
$table->caption = get_string('scheduledtasks', 'tool_task');
$table->head = [
get_string('name'),
get_string('component', 'tool_task'),
get_string('edit'),
get_string('logs'),
get_string('lastruntime', 'tool_task'),
get_string('nextruntime', 'tool_task'),
get_string('taskscheduleminute', 'tool_task'),
get_string('taskschedulehour', 'tool_task'),
get_string('taskscheduleday', 'tool_task'),
get_string('taskscheduledayofweek', 'tool_task'),
get_string('taskschedulemonth', 'tool_task'),
get_string('faildelay', 'tool_task'),
get_string('default', 'tool_task'),
];
$table->attributes['class'] = 'admintable generaltable';
$table->colclasses = [];
if (!$showloglink) {
// Hide the log links.
$table->colclasses['3'] = 'hidden';
}
$data = [];
$yes = get_string('yes');
$no = get_string('no');
$canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow');
foreach ($tasks as $task) {
$classname = get_class($task);
$defaulttask = \core\task\manager::get_default_scheduled_task($classname, false);
$customised = $task->is_customised() ? $no : $yes;
if (empty($CFG->preventscheduledtaskchanges) && !$task->is_overridden()) {
$configureurl = new moodle_url('/admin/tool/task/scheduledtasks.php',
['action' => 'edit', 'task' => $classname]);
$editlink = $this->output->action_icon($configureurl, new pix_icon('t/edit',
get_string('edittaskschedule', 'tool_task', $task->get_name())));
} else {
$editlink = $this->render(new pix_icon('t/locked',
get_string('scheduledtaskchangesdisabled', 'tool_task')));
}
$loglink = '';
if ($showloglink) {
$loglink = $this->output->action_icon(
\core\task\logmanager::get_url_for_task_class($classname),
new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $task->get_name())
));
}
$namecellcontent = $task->get_name() . "\n" .
html_writer::span('\\' . $classname, 'task-class text-ltr');
if ($task->is_overridden()) {
// Let the user know the scheduled task is defined in config.
$namecellcontent .= "\n" . html_writer::div(get_string('configoverride', 'admin'), 'alert-info');
}
$namecell = new html_table_cell($namecellcontent);
$namecell->header = true;
$namecell->id = scheduled_task::get_html_id($classname);
$runnow = '';
$canrunthistask = $canruntasks && $task->can_run();
if ($canrunthistask) {
$runnow = html_writer::div(html_writer::link(
new moodle_url('/admin/tool/task/schedule_task.php',
['task' => $classname]),
get_string('runnow', 'tool_task')), 'task-runnow');
}
$faildelaycell = new html_table_cell($task->get_fail_delay());
if ($task->get_fail_delay()) {
$faildelaycell->text .= html_writer::div(
$this->output->single_button(
new moodle_url('/admin/tool/task/clear_fail_delay.php',
['task' => $classname]),
get_string('clear')
),
'task-runnow'
);
$faildelaycell->attributes['class'] = 'table-danger';
}
$row = new html_table_row([
$namecell,
new html_table_cell($this->component_name($task->get_component())),
new html_table_cell($editlink),
new html_table_cell($loglink),
new html_table_cell($this->last_run_time($task) . $runnow),
new html_table_cell($this->next_run_time($task)),
$this->time_cell($task->get_minute(), $defaulttask->get_minute()),
$this->time_cell($task->get_hour(), $defaulttask->get_hour()),
$this->time_cell($task->get_day(), $defaulttask->get_day()),
$this->time_cell($task->get_day_of_week(), $defaulttask->get_day_of_week()),
$this->time_cell($task->get_month(), $defaulttask->get_month()),
$faildelaycell,
new html_table_cell($customised)]);
$classes = [];
if (!$task->is_enabled()) {
$classes[] = 'disabled';
}
if (get_class($task) == $lastchanged) {
$classes[] = 'table-primary';
}
$row->attributes['class'] = implode(' ', $classes);
$data[] = $row;
}
$table->data = $data;
if ($lastchanged) {
// IE does not support this, and the ancient version of Firefox we use for Behat
// has the method, but then errors on 'centre'. So, just try to scroll, and if it fails, don't care.
$this->page->requires->js_init_code(
'try{document.querySelector("tr.table-primary").scrollIntoView({block: "center"});}catch(e){}');
}
return html_writer::table($table);
}
/**
* Nicely display the name of a component, with its disabled status and internal name.
*
* @param string $component component name, e.g. 'core' or 'mod_forum'.
* @return string HTML.
*/
public function component_name(string $component): string {
list($type) = core_component::normalize_component($component);
if ($type === 'core') {
return get_string('corecomponent', 'tool_task');
}
$plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
if (!$plugininfo) {
return $component;
}
$plugininfo->init_display_name();
$componentname = $plugininfo->displayname;
if ($plugininfo->is_enabled() === false) {
$componentname .= ' ' . html_writer::span(
get_string('disabled', 'tool_task'), 'badge bg-secondary text-dark');
}
$componentname .= "\n" . html_writer::span($plugininfo->component, 'task-class text-ltr');
return $componentname;
}
/**
* Standard display of a tasks last run time.
*
* @param scheduled_task $task
* @return string HTML.
*/
public function last_run_time(scheduled_task $task): string {
if ($task->get_last_run_time()) {
return userdate($task->get_last_run_time());
} else {
return get_string('never');
}
}
/**
* Standard display of a tasks next run time.
*
* @param scheduled_task $task
* @return string HTML.
*/
public function next_run_time(scheduled_task $task): string {
$nextrun = $task->get_next_run_time();
if (!$task->is_component_enabled() && !$task->get_run_if_component_disabled()) {
$nextrun = get_string('plugindisabled', 'tool_task');
} else if ($task->get_disabled()) {
$nextrun = get_string('taskdisabled', 'tool_task');
} else if ($nextrun > time()) {
$nextrun = userdate($nextrun);
} else {
$nextrun = get_string('asap', 'tool_task');
}
return $nextrun;
}
/**
* Get a table cell to show one time, comparing it to the default.
*
* @param string $current the current setting.
* @param string $default the default setting from the db/tasks.php file.
* @return html_table_cell for use in the table.
*/
protected function time_cell(string $current, string $default): html_table_cell {
$cell = new html_table_cell($current);
// Cron-style values must always be LTR.
$cell->attributes['class'] = 'text-ltr';
// If the current value is default, that is all we want to do.
if ($default === '*') {
if ($current === '*') {
return $cell;
}
} else if ($default === 'R' ) {
if (is_numeric($current)) {
return $cell;
}
} else {
if ($default === $current) {
return $cell;
}
}
// Otherwise, highlight and show the default.
$cell->attributes['class'] .= ' table-warning';
$cell->text .= ' ' . html_writer::span(
get_string('defaultx', 'tool_task', $default), 'task-class');
return $cell;
}
/**
* Displays a warning on the page if cron is disabled.
*
* @return string HTML code for information about cron being disabled
* @throws moodle_exception
*/
public function cron_disabled(): string {
return $this->output->notification(get_string('crondisabled', 'tool_task'), 'warning');
}
/**
* Renders a link back to the scheduled tasks page (used from the 'run now' screen).
*
* @param string $taskclassname if specified, the list of tasks will scroll to show this task.
* @return string HTML code
*/
public function link_back($taskclassname = '') {
$url = new moodle_url('/admin/tool/task/scheduledtasks.php');
if ($taskclassname) {
$url->param('lastchanged', $taskclassname);
}
return $this->render_from_template('tool_task/link_back', ['url' => $url]);
}
}
+165
View File
@@ -0,0 +1,165 @@
<?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 run ad hoc task(s)
*
* This script runs a group or a single ad hoc task from the web UI.
*
* @package tool_task
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('adhoctasks');
$runurl = '/admin/tool/task/run_adhoctasks.php';
$tasksurl = '/admin/tool/task/adhoctasks.php';
// Allow execution of single task. This requires login and has different rules.
$classname = optional_param('classname', null, PARAM_RAW);
$failedonly = optional_param('failedonly', false, PARAM_BOOL);
$taskid = optional_param('id', null, PARAM_INT);
$confirmed = optional_param('confirm', 0, PARAM_INT);
if (!\core\task\manager::is_runnable()) {
$redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
}
$params = ['classname' => $classname, 'failedonly' => $failedonly, 'id' => $taskid];
// Check input parameter id against all existing tasks.
if ($taskid) {
$record = $DB->get_record('task_adhoc', ['id' => $taskid]);
if (!$record) {
throw new \moodle_exception('invalidtaskid');
}
$classname = $record->classname;
$heading = get_string('runadhoctask', 'tool_task', ['task' => $classname, 'taskid' => $taskid]);
$tasks = [core\task\manager::adhoc_task_from_record($record)];
} else {
if (!$classname) {
throw new \moodle_exception('noclassname', 'tool_task');
}
$heading = get_string(
$failedonly ? 'runadhoctasksfailed' : 'runadhoctasks',
'tool_task',
s($classname),
);
$now = time();
$tasks = array_filter(
core\task\manager::get_adhoc_tasks($classname, $failedonly, true),
function ($t) use ($now) {
return $t->get_fail_delay() || $t->get_next_run_time() <= $now;
}
);
}
// Start output.
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_heading($SITE->fullname);
$PAGE->set_title($classname);
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
if (!$tasks) {
echo $OUTPUT->single_button($tasksurl,
get_string('notasks', 'tool_task'),
'get');
echo $OUTPUT->footer();
exit;
}
$renderer = $PAGE->get_renderer('tool_task');
if (!get_config('core', 'cron_enabled')) {
echo $renderer->cron_disabled();
}
echo $renderer->adhoc_tasks_simple_table($tasks);
// The initial request just shows the confirmation page; we don't do anything further unless
// they confirm.
if (!$confirmed) {
echo $OUTPUT->confirm(get_string('runadhoc_confirm', 'tool_task'),
new single_button(new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
get_string('runadhoc', 'tool_task')),
new single_button(new moodle_url($tasksurl, $params),
get_string('cancel'), false));
echo $OUTPUT->footer();
exit;
}
// Action requires session key.
require_sesskey();
\core\session\manager::write_close();
echo $OUTPUT->footer();
echo $OUTPUT->select_element_for_append();
// Prepare to handle output via mtrace.
require_once("{$CFG->dirroot}/{$CFG->admin}/tool/task/lib.php");
$CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
// Run the specified tasks.
if ($taskid) {
$repeat = $DB->get_record('task_adhoc', ['id' => $taskid]);
echo html_writer::start_tag('pre', ['class' => 'task-output']);
\core\task\manager::run_adhoc_from_cli($taskid);
echo html_writer::end_tag('pre');
} else {
$repeat = core\task\manager::get_adhoc_tasks($classname, $failedonly, true);
// Run failed first (if any). We have to run them separately anyway,
// because faildelay is observed if failed flag is not true.
echo html_writer::tag('p', get_string('runningfailedtasks', 'tool_task'), ['class' => 'lead']);
echo html_writer::start_tag('pre', ['class' => 'task-output']);
\core\task\manager::run_all_adhoc_from_cli(true, $classname);
echo html_writer::end_tag('pre');
if (!$failedonly) {
echo html_writer::tag('p', get_string('runningalltasks', 'tool_task'), ['class' => 'lead']);
echo html_writer::start_tag('pre', ['class' => 'task-output']);
\core\task\manager::run_all_adhoc_from_cli(false, $classname);
echo html_writer::end_tag('pre');
}
}
if ($repeat) {
echo html_writer::div(
$OUTPUT->single_button(
new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
get_string('runagain', 'tool_task')
)
);
}
echo html_writer::div(
html_writer::link(
new moodle_url($tasksurl, $taskid ? ['classname' => $classname] : []),
get_string('backtoadhoctasks', 'tool_task')
)
);
+51
View File
@@ -0,0 +1,51 @@
<?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/>.
/**
* Running task admin page.
*
* @package tool_task
* @copyright 2019 The Open University
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/tablelib.php');
$pageurl = new \moodle_url('/admin/tool/task/runningtasks.php');
$heading = get_string('runningtasks', 'tool_task');
$PAGE->set_url($pageurl);
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('admin');
$PAGE->set_title($heading);
$PAGE->set_heading($heading);
admin_externalpage_setup('runningtasks');
echo $OUTPUT->header();
if (!get_config('core', 'cron_enabled')) {
$renderer = $PAGE->get_renderer('tool_task');
echo $renderer->cron_disabled();
}
$table = new \tool_task\running_tasks_table();
$table->baseurl = $pageurl;
$table->out(100, false);
echo $OUTPUT->footer();
+106
View File
@@ -0,0 +1,106 @@
<?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
*/
define('NO_OUTPUT_BUFFERING', true);
require('../../../config.php');
// Allow execution of single task. This requires login and has different rules.
$taskname = required_param('task', PARAM_RAW_TRIMMED);
// Basic security checks.
require_admin();
$context = context_system::instance();
// 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) {
throw new moodle_exception('cannotfindinfo', 'error', new moodle_url('/admin/tool/task/scheduledtasks.php'), $taskname);
}
if (!\core\task\manager::is_runnable()) {
$redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
}
if (!get_config('tool_task', 'enablerunnow') || !$task->can_run()) {
throw new moodle_exception('nopermissions', 'error', new moodle_url('/admin/tool/task/scheduledtasks.php'),
get_string('runnow', 'tool_task'), $task->get_name());
}
// Start output.
$PAGE->set_url(new moodle_url('/admin/tool/task/schedule_task.php', ['task' => $taskname]));
$PAGE->set_context($context);
$PAGE->set_heading($SITE->fullname);
$PAGE->set_title($task->get_name());
navigation_node::override_active_url(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',
['task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey()]),
get_string('runnow', 'tool_task')),
new single_button(new moodle_url('/admin/tool/task/scheduledtasks.php',
['lastchanged' => get_class($task)]),
get_string('cancel'), false));
echo $OUTPUT->footer();
exit;
}
// Action requires session key.
require_sesskey();
\core\session\manager::write_close();
// Prepare for streamed output.
echo $OUTPUT->footer();
echo $OUTPUT->select_element_for_append();
// Prepare to handle output via mtrace.
require_once("{$CFG->dirroot}/{$CFG->admin}/tool/task/lib.php");
echo html_writer::start_tag('pre', ['class' => 'task-output', 'style' => 'min-height: 24lh']);
$CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
// Run the specified task (this will output an error if it doesn't exist).
\core\task\manager::run_from_cli($task);
echo html_writer::end_tag('pre');
$output = $PAGE->get_renderer('tool_task');
// Re-run the specified task (this will output an error if it doesn't exist).
echo $OUTPUT->single_button(new moodle_url('/admin/tool/task/schedule_task.php',
array('task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey())),
get_string('runagain', 'tool_task'));
echo $output->link_back(get_class($task));
+108
View File
@@ -0,0 +1,108 @@
<?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/>.
/**
* Scheduled task admin pages.
*
* @package tool_task
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/tablelib.php');
admin_externalpage_setup('scheduledtasks');
$action = optional_param('action', '', PARAM_ALPHAEXT);
$taskname = optional_param('task', '', PARAM_RAW);
$lastchanged = optional_param('lastchanged', '', PARAM_RAW);
$task = null;
$mform = null;
if ($taskname) {
$task = \core\task\manager::get_scheduled_task($taskname);
if (!$task) {
throw new \moodle_exception('invaliddata');
}
}
$PAGE->navbar->add(get_string('scheduledtasks', 'tool_task'), $PAGE->url);
if ($action == 'edit') {
$PAGE->navbar->add(get_string('edittaskschedule', 'tool_task', $task->get_name()));
}
if ($task) {
$mform = new tool_task_edit_scheduled_task_form(null, $task);
$nexturl = new moodle_url($PAGE->url, ['lastchanged' => $taskname]);
}
$PAGE->set_primary_active_tab('siteadminnode');
$renderer = $PAGE->get_renderer('tool_task');
if ($mform && ($mform->is_cancelled() || !empty($CFG->preventscheduledtaskchanges) || $task->is_overridden())) {
redirect($nexturl);
} else if ($action == 'edit' && empty($CFG->preventscheduledtaskchanges)) {
if ($data = $mform->get_data()) {
if ($data->resettodefaults) {
$defaulttask = \core\task\manager::get_default_scheduled_task($taskname);
$task->set_minute($defaulttask->get_minute());
$task->set_hour($defaulttask->get_hour());
$task->set_month($defaulttask->get_month());
$task->set_day_of_week($defaulttask->get_day_of_week());
$task->set_day($defaulttask->get_day());
$task->set_disabled($defaulttask->get_disabled());
$task->set_customised(false);
} else {
$task->set_minute($data->minute);
$task->set_hour($data->hour);
$task->set_month($data->month);
$task->set_day_of_week($data->dayofweek);
$task->set_day($data->day);
$task->set_disabled($data->disabled);
$task->set_customised(true);
}
try {
\core\task\manager::configure_scheduled_task($task);
redirect($nexturl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
} catch (Exception $e) {
redirect($nexturl, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
}
} else {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('edittaskschedule', 'tool_task', $task->get_name()));
echo html_writer::div('\\' . get_class($task), 'task-class text-ltr');
echo html_writer::div(get_string('fromcomponent', 'tool_task',
$renderer->component_name($task->get_component())));
$mform->display();
echo $OUTPUT->footer();
}
} else {
echo $OUTPUT->header();
if (!get_config('core', 'cron_enabled')) {
echo $renderer->cron_disabled();
}
$tasks = core\task\manager::get_all_scheduled_tasks();
echo $renderer->scheduled_tasks_table($tasks, $lastchanged);
echo $OUTPUT->footer();
}
+54
View File
@@ -0,0 +1,54 @@
<?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/>.
/**
* Scheduled tasks.
*
* @package tool_task
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
if ($hassiteconfig) {
$ADMIN->add(
'taskconfig',
new admin_externalpage(
'scheduledtasks',
new lang_string('scheduledtasks', 'tool_task'),
"$CFG->wwwroot/$CFG->admin/tool/task/scheduledtasks.php"
)
);
$ADMIN->add(
'taskconfig',
new admin_externalpage(
'adhoctasks',
new lang_string('adhoctasks', 'tool_task'),
"$CFG->wwwroot/$CFG->admin/tool/task/adhoctasks.php"
)
);
$ADMIN->add(
'taskconfig',
new admin_externalpage(
'runningtasks',
new lang_string('runningtasks', 'tool_task'),
"$CFG->wwwroot/$CFG->admin/tool/task/runningtasks.php"
)
);
}
+28
View File
@@ -0,0 +1,28 @@
#page-admin-tool-task-scheduledtasks .task-class,
#page-admin-tool-task-runningtasks .task-class {
display: block;
padding: 0 0.5em;
color: #888;
font-size: 0.75em;
}
#page-admin-tool-task-scheduledtasks input[type=text] {
/*rtl:ignore*/
direction: ltr;
}
#page-admin-tool-task-scheduledtasks .task-runnow,
#page-admin-tool-task-scheduledtasks .task-clearfaildelay {
font-size: 0.75em;
}
.path-admin-tool-task .task-output {
color: #fff;
background: #333;
padding: 1em;
a {
color: #fff;
text-decoration: underline;
}
}
@@ -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>
@@ -0,0 +1,53 @@
<?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/>.
/**
* Behat step definitions for scheduled task administration.
*
* @package tool_task
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
/**
* Behat step definitions for scheduled task administration.
*
* @package tool_task
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_tool_task extends behat_base {
/**
* Set a fake fail delay for a scheduled task.
*
* @Given /^the scheduled task "(?P<task_name>[^"]+)" has a fail delay of "(?P<seconds_number>\d+)" seconds$/
* @param string $task Task classname
* @param int $seconds Fail delay time in seconds
*/
public function scheduled_task_has_fail_delay_seconds($task, $seconds) {
global $DB;
$id = $DB->get_field('task_scheduled', 'id', ['classname' => $task], IGNORE_MISSING);
if (!$id) {
throw new Exception('Unknown scheduled task: ' . $task);
}
$DB->set_field('task_scheduled', 'faildelay', $seconds, ['id' => $id]);
}
}
@@ -0,0 +1,32 @@
@tool @tool_task
Feature: Clear scheduled task fail delay
In order to stop failures from delaying a scheduled task run
As an admin
I need to be able to clear the fail delay on a task
Background:
Given the scheduled task "\core\task\send_new_user_passwords_task" has a fail delay of "60" seconds
And I log in as "admin"
And I navigate to "Server > Tasks > Scheduled tasks" in site administration
Scenario: Any fail delay is highlighted
Then I should see "60" in the "Send new user passwords" "table_row"
And I should see "Clear" in the "Send new user passwords" "table_row"
And I should see "60" in the "td.table-danger" "css_element"
Scenario: Clear fail delay
When I click on "Clear" "text" in the "Send new user passwords" "table_row"
And I should see "Are you sure you want to clear the fail delay"
And I press "Clear"
Then I should not see "60" in the "Send new user passwords" "table_row"
And I should not see "Clear" in the "Send new user passwords" "table_row"
And I should see "Send new user passwords" in the "tr.table-primary" "css_element"
Scenario: Cancel clearing the fail delay
When I click on "Clear" "text" in the "Send new user passwords" "table_row"
And I press "Cancel"
Then I should see "60" in the "Send new user passwords" "table_row"
And I should see "Clear" in the "Send new user passwords" "table_row"
And I should see "Send new user passwords" in the "tr.table-primary" "css_element"
@@ -0,0 +1,20 @@
@tool @tool_task
Feature: See warning message if cron is disabled
In order to manage scheduled tasks
As a Moodle Administrator
I need to be able to view a warning message if cron is disabled
Background:
Given I log in as "admin"
Scenario: If cron is disabled, I should see the message
When the following config values are set as admin:
| cron_enabled | 0 |
And I navigate to "Server > Tasks > Scheduled tasks" in site administration
Then I should see "Cron is disabled"
Scenario: If cron is enabled, I should not see the message
When the following config values are set as admin:
| cron_enabled | 1 |
And I navigate to "Server > Tasks > Scheduled tasks" in site administration
Then I should not see "Cron is disabled"
@@ -0,0 +1,73 @@
@tool @tool_task @javascript
Feature: Manage scheduled tasks
In order to configure scheduled tasks
As an admin
I need to be able to disable, enable, edit and reset to default scheduled tasks
Background:
Given I log in as "admin"
And I navigate to "Server > Tasks > Scheduled tasks" in site administration
Scenario: Disable scheduled task
When I click on "Edit task schedule: Log table cleanup" "link" in the "Log table cleanup" "table_row"
Then I should see "Edit task schedule: Log table cleanup"
And I set the following fields to these values:
| disabled | 1 |
And I press "Save changes"
Then I should see "Changes saved"
And I should see "Task disabled" in the "Log table cleanup" "table_row"
And I should see "Log table cleanup" in the "tr.table-primary" "css_element"
Scenario: Enable scheduled task
When I click on "Edit task schedule: Log table cleanup" "link" in the "Log table cleanup" "table_row"
Then I should see "Edit task schedule: Log table cleanup"
And I set the following fields to these values:
| disabled | 0 |
And I press "Save changes"
Then I should see "Changes saved"
And I should not see "Task disabled" in the "Log table cleanup" "table_row"
And I should see "Log table cleanup" in the "tr.table-primary" "css_element"
Scenario: Edit scheduled task
When I click on "Edit task schedule: Log table cleanup" "link" in the "Log table cleanup" "table_row"
Then I should see "Edit task schedule: Log table cleanup"
And I should see "\logstore_standard\task\cleanup_task"
And I should see "From component: Standard log"
And I should see "logstore_standard"
And I should see "Default: R" in the "Minute" "fieldset"
And I should see "Default: *" in the "Day" "fieldset"
And I set the following fields to these values:
| minute | frog |
And I press "Save changes"
And I should see "Data submitted is invalid"
And I set the following fields to these values:
| minute | */5 |
| hour | 1 |
| day | 2 |
| month | 3 |
| dayofweek | 4 |
And I press "Save changes"
And I should see "Changes saved"
And the following should exist in the "admintable" table:
| Component | Minute | Hour | Day | Day of week | Month |
| Standard log logstore_standard | */5 Default: R | 1 Default: 4 | 2 Default: * | 4 Default: * | 3 Default: * |
And I should see "Log table cleanup" in the "tr.table-primary" "css_element"
And I should see "*/5 Default: R" in the "td.table-warning" "css_element"
Scenario: Reset scheduled task to default
When I click on "Edit task schedule: Log table cleanup" "link" in the "Log table cleanup" "table_row"
Then I should see "Edit task schedule: Log table cleanup"
And I set the following fields to these values:
| resettodefaults | 1 |
And I press "Save changes"
Then I should see "Changes saved"
And the following should not exist in the "admintable" table:
| Name | Minute | Hour | Day | Day of week | Month |
| Log table cleanup | */5 | 1 | 2 | 4 | 3 |
And I should see "Log table cleanup" in the "tr.table-primary" "css_element"
Scenario: Disabled plugin's tasks are labelled as disabled too
When "CAS users sync job \auth_cas\task\sync_task" row "Next run" column of "Scheduled tasks" table should contain "Plugin disabled"
Then "CAS users sync job \auth_cas\task\sync_task" row "Component" column of "Scheduled tasks" table should contain "Disabled"
And "Background processing for scheduled allocation \workshopallocation_scheduled\task\cron_task" row "Next run" column of "Scheduled tasks" table should not contain "Plugin disabled"
And "Background processing for scheduled allocation \workshopallocation_scheduled\task\cron_task" row "Component" column of "Scheduled tasks" table should not contain "Disabled"
@@ -0,0 +1,40 @@
@tool @tool_task
Feature: See running scheduled tasks
In order to configure scheduled tasks
As an admin
I need to see if tasks are running
Background:
Given I log in as "admin"
Scenario: If no task is running, I should see the corresponding message
Given I navigate to "Server > Tasks > Tasks running now" in site administration
Then I should see "Nothing to display"
Scenario: If tasks are running, I should see task details
Given the following "tool_task > scheduled tasks" exist:
| classname | seconds | hostname | pid |
| \core\task\automated_backup_task | 121 | c69335460f7f | 1914 |
And the following "tool_task > adhoc tasks" exist:
| classname | seconds | hostname | pid |
| \core\task\asynchronous_backup_task | 7201 | c69335460f7f | 1915 |
| \core\task\asynchronous_restore_task | 172800 | c69335460f7f | 1916 |
And I navigate to "Server > Tasks > Tasks running now" in site administration
# Check the scheduled task details.
Then I should see "Scheduled" in the "\core\task\automated_backup_task" "table_row"
And I should see "2 mins" in the "Automated backups" "table_row"
And I should see "c69335460f7f" in the "Automated backups" "table_row"
And I should see "1914" in the "Automated backups" "table_row"
# Check the "asynchronous_backup_task" adhoc task details.
And I should see "Ad hoc" in the "\core\task\asynchronous_backup_task" "table_row"
And I should see "2 hours" in the "core\task\asynchronous_backup_task" "table_row"
And I should see "c69335460f7f" in the "core\task\asynchronous_backup_task" "table_row"
And I should see "1915" in the "core\task\asynchronous_backup_task" "table_row"
# Check the "asynchronous_restore_task" adhoc task details.
And I should see "Ad hoc" in the "\core\task\asynchronous_restore_task" "table_row"
And I should see "2 days" in the "core\task\asynchronous_restore_task" "table_row"
And I should see "c69335460f7f" in the "core\task\asynchronous_restore_task" "table_row"
And I should see "1916" in the "core\task\asynchronous_restore_task" "table_row"
+280
View File
@@ -0,0 +1,280 @@
<?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/>.
namespace tool_task;
/**
* Test for the task mform class.
*
* @package tool_task
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
*/
class form_test extends \advanced_testcase {
/**
* Test validations for minute field.
*/
public function test_validate_fields_minute(): void {
$checker = new \tool_task\scheduled_checker_task();
$checker->set_minute('*');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('1');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('20');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('65');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('*/');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('*/1');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('*/20');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('*/60');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('1,2');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('2,20');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('20,30,45');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('65,20,30');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('25,75');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('1-2');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('2-20');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('20-30');
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('65-20');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
$checker->set_minute('25-75');
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
}
/**
* Test validations for minute hour.
*/
public function test_validate_fields_hour(): void {
$checker = new \tool_task\scheduled_checker_task();
$checker->set_hour('*');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('1');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('20');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('60');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('65');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('*/');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('*/1');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('*/20');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('*/60');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('*/65');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('1,2');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('2,20');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('20,30,45');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('65,20,30');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('25,75');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('1-2');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('2-20');
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('20-30');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('65-20');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
$checker->set_hour('24-75');
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
}
/**
* Test validations for day field.
*/
public function test_validate_fields_day(): void {
$checker = new \tool_task\scheduled_checker_task();
$checker->set_day('*');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('1');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('20');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('65');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('*/');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('*/1');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('*/20');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('*/65');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('*/35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('1,2');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('2,20');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('20,30,25');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('65,20,30');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('25,35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('1-2');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('2-20');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('20-30');
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('65-20');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
$checker->set_day('25-35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
}
/**
* Test validations for month field.
*/
public function test_validate_fields_month(): void {
$checker = new \tool_task\scheduled_checker_task();
$checker->set_month('*');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('1');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('10');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('13');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('35');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('*/');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('*/1');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('*/12');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('*/13');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('*/35');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('1,2');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('2,11');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('2,10,12');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('65,2,13');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('25,35');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('1-2');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('2-12');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('3-6');
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('65-2');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
$checker->set_month('25-26');
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
}
/**
* Test validations for dayofweek field.
*/
public function test_validate_fields_dayofweek(): void {
$checker = new \tool_task\scheduled_checker_task();
$checker->set_day_of_week('*');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('0');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('1');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('6');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('7');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('8');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('20');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/1');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/6');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/7');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/13');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('*/35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('1,2');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('2,6');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('2,6,3');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('65,2,13');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('25,35');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('1-2');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('2-6');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('65-2');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('3-7');
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
$checker->set_day_of_week('3-8');
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
}
}
@@ -0,0 +1,57 @@
<?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/>.
/**
* Behat data generator for tool_task.
*
* @package tool_task
* @category test
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Behat data generator for tool_task.
*
* @package tool_task
* @category test
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_tool_task_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created.
* @return array entity name => information about how to generate.
*/
protected function get_creatable_entities(): array {
return [
'scheduled tasks' => [
'singular' => 'scheduled task',
'datagenerator' => 'scheduled_tasks',
'required' => ['classname', 'seconds', 'hostname', 'pid'],
],
'adhoc tasks' => [
'singular' => 'adhoc task',
'datagenerator' => 'adhoc_tasks',
'required' => ['classname', 'seconds', 'hostname', 'pid'],
],
];
}
}
+69
View File
@@ -0,0 +1,69 @@
<?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/>.
/**
* Tool task test data generator class
*
* @package tool_task
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tool task test data generator class
*
* @package tool_task
* @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_task_generator extends testing_module_generator {
/**
* Mark a scheduled task as running.
*
* @param array $data Scheduled task properties
* @throws dml_exception
*/
public function create_scheduled_tasks($data) {
global $DB;
$conditions = ['classname' => $data['classname']];
$record = $DB->get_record('task_scheduled', $conditions, '*', MUST_EXIST);
$record->timestarted = time() - $data['seconds'];
$record->hostname = $data['hostname'];
$record->pid = $data['pid'];
$DB->update_record('task_scheduled', $record);
}
/**
* Mark an adhoc task as running.
*
* @param array $data Adhoc task properties
* @throws dml_exception
*/
public function create_adhoc_tasks($data) {
global $DB;
$adhoctask = (object)[
'classname' => $data['classname'],
'nextruntime' => 0,
'timestarted' => time() - $data['seconds'],
'hostname' => $data['hostname'],
'pid' => $data['pid'],
];
$DB->insert_record('task_adhoc', $adhoctask);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Plugin version info
*
* @package tool_task
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'tool_task'; // Full name of the plugin (used for diagnostics)