Merge branch 'MDL-68714-task-top-process-name' of https://github.com/brendanheywood/moodle

This commit is contained in:
Andrew Nicols 2020-07-02 11:20:45 +08:00
commit 710c3f0c36
2 changed files with 39 additions and 1 deletions

View File

@ -145,6 +145,21 @@ function cli_get_params(array $longoptions, array $shortmapping=null) {
return array($options, $unrecognized);
}
/**
* This sets the cli process title suffix
*
* An example is appending current Task API info so a sysadmin can immediately
* see what task a cron process is running at any given moment.
*
* @param string $suffix process suffix
*/
function cli_set_process_title_suffix(string $suffix) {
if (defined('CLI_SCRIPT') && isset($_SERVER['argv'])) {
$command = join(' ', $_SERVER['argv']);
@cli_set_process_title("php $command ($suffix)");
}
}
/**
* Print or return section separator string
* @param bool $return false means print, true return as string

View File

@ -196,10 +196,12 @@ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true)
}
$waiting = false;
cron_run_inner_adhoc_task($task);
cron_set_process_title("Waiting for next adhoc task");
$taskcount++;
unset($task);
} else {
if (time() >= $finishtime) {
$timeleft = $finishtime - time();
if ($timeleft <= 0) {
break;
}
if (!$waiting) {
@ -208,6 +210,7 @@ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true)
mtrace('.', '');
}
$waiting = true;
cron_set_process_title("Waiting {$timeleft}s for next adhoc task");
sleep(1);
}
}
@ -238,6 +241,7 @@ function cron_run_inner_scheduled_task(\core\task\task_base $task) {
$fullname = $task->get_name() . ' (' . get_class($task) . ')';
mtrace('Execute scheduled task: ' . $fullname);
cron_set_process_title('Scheduled task: ' . get_class($task));
cron_trace_time_and_memory();
$predbqueries = null;
$predbqueries = $DB->perf_get_queries();
@ -277,6 +281,7 @@ function cron_run_inner_scheduled_task(\core\task\task_base $task) {
} finally {
// Reset back to the standard admin user.
cron_setup_user();
cron_set_process_title('Waiting for next scheduled task');
cron_prepare_core_renderer(true);
}
get_mailer('close');
@ -293,6 +298,7 @@ function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) {
\core\task\logmanager::start_logging($task);
mtrace("Execute adhoc task: " . get_class($task));
cron_set_process_title('Adhoc task: ' . $task->get_id() . ' ' . get_class($task));
cron_trace_time_and_memory();
$predbqueries = null;
$predbqueries = $DB->perf_get_queries();
@ -367,6 +373,23 @@ function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) {
get_mailer('close');
}
/**
* Sets the process title
*
* This makes it very easy for a sysadmin to immediately see what task
* a cron process is running at any given moment.
*
* @param string $title process status title
*/
function cron_set_process_title(string $title) {
global $CFG;
if (defined('CLI_SCRIPT')) {
require_once($CFG->libdir . '/clilib.php');
$datetime = userdate(time(), '%b %d, %H:%M:%S');
cli_set_process_title_suffix("$datetime $title");
}
}
/**
* Output some standard information during cron runs. Specifically current time
* and memory usage. This method also does gc_collect_cycles() (before displaying