Skip to content

Commit

Permalink
Merge branch 'master-MDL-67211_v6' of https://github.com/golenkovm/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 26, 2020
2 parents 20c84b6 + d7342dc commit 23517d9
Show file tree
Hide file tree
Showing 28 changed files with 1,018 additions and 18 deletions.
3 changes: 3 additions & 0 deletions admin/classes/task_log_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function __construct(string $filter = '', int $resultfilter = null) {
'userid' => get_string('user', 'admin'),
'timestart' => get_string('task_starttime', 'admin'),
'duration' => get_string('task_duration', 'admin'),
'hostname' => get_string('hostname', 'tool_task'),
'pid' => get_string('pid', 'tool_task'),
'db' => get_string('task_dbstats', 'admin'),
'result' => get_string('task_result', 'admin'),
'actions' => '',
Expand Down Expand Up @@ -132,6 +134,7 @@ public function query_db($pagesize, $useinitialsbar = true) {

$sql = "SELECT
tl.id, tl.type, tl.component, tl.classname, tl.userid, tl.timestart, tl.timeend,
tl.hostname, tl.pid,
tl.dbreads, tl.dbwrites, tl.result,
tl.dbreads + tl.dbwrites AS db,
tl.timeend - tl.timestart AS duration,
Expand Down
9 changes: 9 additions & 0 deletions admin/cli/adhoc_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
'showsql' => false,
'showdebugging' => false,
'ignorelimits' => false,
'force' => false,
], [
'h' => 'help',
'e' => 'execute',
'k' => 'keep-alive',
'i' => 'ignorelimits',
'f' => 'force',
]
);

Expand All @@ -61,6 +63,7 @@
-e, --execute Run all queued adhoc tasks
-k, --keep-alive=N Keep this script alive for N seconds and poll for new adhoc tasks
-i --ignorelimits Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
-f, --force Run even if cron is disabled
Example:
\$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute
Expand Down Expand Up @@ -92,6 +95,12 @@
if (empty($options['execute'])) {
exit(0);
}

if (!get_config('core', 'cron_enabled') && !$options['force']) {
mtrace('Cron is disabled. Use --force to override.');
exit(1);
}

if (empty($options['keep-alive'])) {
$options['keep-alive'] = 0;
}
Expand Down
111 changes: 105 additions & 6 deletions admin/cli/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,23 @@

// now get cli options
list($options, $unrecognized) = cli_get_params(
array(
[
'help' => false,
'stop' => false,
),
array(
'list' => false,
'force' => false,
'enable' => false,
'disable' => false,
'disable-wait' => false,
], [
'h' => 'help',
's' => 'stop',
)
'l' => 'list',
'f' => 'force',
'e' => 'enable',
'd' => 'disable',
'w' => 'disable-wait',
]
);

if ($unrecognized) {
Expand All @@ -56,8 +65,13 @@
"Execute periodic cron actions.
Options:
-h, --help Print out this help
-s, --stop Notify all other running cron processes to stop after the current task
-h, --help Print out this help
-s, --stop Notify all other running cron processes to stop after the current task
-l, --list Show the list of currently running tasks and how long they have been running
-f, --force Execute task even if cron is disabled
-e, --enable Enable cron
-d, --disable Disable cron
-w, --disable-wait=600 Disable cron and wait until all tasks finished or fail after N seconds (optional param)
Example:
\$sudo -u www-data /usr/bin/php admin/cli/cron.php
Expand All @@ -74,6 +88,91 @@
die;
}

if ($options['enable']) {
set_config('cron_enabled', 1);
mtrace('Cron has been enabled for the site.');
exit(0);
}

if ($options['disable']) {
set_config('cron_enabled', 0);
\core\task\manager::clear_static_caches();
mtrace('Cron has been disabled for the site.');
exit(0);
}

if ($options['list']) {
$tasks = \core\task\manager::get_running_tasks();
mtrace('The list of currently running tasks:');
$format = "%7s %-12s %-9s %-20s %-52s\n";
printf ($format,
'PID',
'HOST',
'TYPE',
'TIME',
'CLASSNAME'
);
foreach ($tasks as $task) {
printf ($format,
$task->pid,
substr($task->hostname, 0, 12),
$task->type,
format_time(time() - $task->timestarted),
substr($task->classname, 0, 52)
);
}
exit(0);
}

if ($wait = $options['disable-wait']) {
$started = time();
if (true === $wait) {
// Default waiting time.
$waitsec = 600;
} else {
$waitsec = $wait;
$wait = true;
}

set_config('cron_enabled', 0);
\core\task\manager::clear_static_caches();
mtrace('Cron has been disabled for the site.');
mtrace('Allocating '. format_time($waitsec) . ' for the tasks to finish.');

$lastcount = 0;
while ($wait) {
$tasks = \core\task\manager::get_running_tasks();

if (count($tasks) == 0) {
mtrace('');
mtrace('All scheduled and adhoc tasks finished.');
exit(0);
}

if (time() - $started >= $waitsec) {
mtrace('');
mtrace('Wait time ('. format_time($waitsec) . ') elapsed, but ' . count($tasks) . ' task(s) still running.');
mtrace('Exiting with code 1.');
exit(1);
}

if (count($tasks) !== $lastcount) {
mtrace('');
mtrace(count($tasks) . " tasks currently running.", '');
$lastcount = count($tasks);
} else {
mtrace('.', '');
}

sleep(1);
}
}

if (!get_config('core', 'cron_enabled') && !$options['force']) {
mtrace('Cron is disabled. Use --force to override.');
exit(1);
}

\core\local\cli\shutdown::script_supports_graceful_exit();

cron_run();
21 changes: 19 additions & 2 deletions admin/cli/scheduled_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@
require_once("$CFG->libdir/cronlib.php");

list($options, $unrecognized) = cli_get_params(
array('help' => false, 'list' => false, 'execute' => false, 'showsql' => false, 'showdebugging' => false),
array('h' => 'help')
[
'help' => false,
'list' => false,
'execute' => false,
'showsql' => false,
'showdebugging' => false,
'force' => false,
], [
'h' => 'help',
'f' => 'force',
]
);

if ($unrecognized) {
Expand All @@ -49,6 +58,7 @@
--showsql Show sql queries before they are executed
--showdebugging Show developer level debugging information
-h, --help Print out this help
-f, --force Execute task even if cron is disabled
Example:
\$sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php --execute=\\core\\task\\session_cleanup_task
Expand Down Expand Up @@ -121,6 +131,13 @@
exit(1);
}

if (!get_config('core', 'cron_enabled') && !$options['force']) {
mtrace('Cron is disabled. Use --force to override.');
exit(1);
}

\core\task\manager::scheduled_task_starting($task);

// Increase memory limit.
raise_memory_limit(MEMORY_EXTRA);

Expand Down
10 changes: 10 additions & 0 deletions admin/settings/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@

$ADMIN->add('server', new admin_category('taskconfig', new lang_string('taskadmintitle', 'admin')));
$temp = new admin_settingpage('taskprocessing', new lang_string('taskprocessing','admin'));

$setting = new admin_setting_configcheckbox(
'cron_enabled',
new lang_string('cron_enabled', 'admin'),
new lang_string('cron_enabled_desc', 'admin'),
1
);
$setting->set_updatedcallback('theme_reset_static_caches');
$temp->add($setting);

$temp->add(
new admin_setting_configtext(
'task_scheduled_concurrency_limit',
Expand Down
141 changes: 141 additions & 0 deletions admin/tool/task/classes/running_tasks_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?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 <[email protected]>
* @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');

/**
* Table to display list of running task.
*
* @copyright 2019 The Open University
* @copyright 2020 Mikhail Golenkov <[email protected]>
* @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('time'),
'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 badge-primary');
} else if ($row->type == 'adhoc') {
$output = \html_writer::span(get_string('adhoc', 'tool_task'), 'badge badge-warning');
} else {
// This shouldn't ever happen.
$output = '';
}
return $output;
}

/**
* Format the time cell.
*
* @param \stdClass $row
* @return string
*/
public function col_time($row) : string {
return format_time($row->time);
}

/**
* Format the timestarted cell.
*
* @param \stdClass $row
* @return string
*/
public function col_timestarted($row) : string {
return userdate($row->timestarted);
}
}
Loading

0 comments on commit 23517d9

Please sign in to comment.