Skip to content

Commit

Permalink
[#136] Deduplicate adhoc tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Oct 3, 2023
1 parent 49f4e08 commit 86c445b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion croncheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@
$delay .= "SCHEDULED TASK: " . get_class($task) . ' (' .$task->get_name() . ") Delay: $faildelay\n";
}

// Find any failed adhoc tasks.
$records = $DB->get_records_sql('SELECT * from {task_adhoc} WHERE faildelay > 0');
$adhoctaskdelays = [];

foreach ($records as $record) {
$task = \core\task\manager::adhoc_task_from_record($record);
if (!$task) {
Expand All @@ -248,9 +251,27 @@
if ($faildelay > $maxdelay) {
$maxdelay = $faildelay;
}
$delay .= "ADHOC TASK: " .get_class($task) . " Delay: $faildelay\n";
$adhoctaskdelays[] = "ADHOC TASK: " .get_class($task) . " Delay: $faildelay";
}

// Deduplicate these, but record the count in the string, by doing the following:
// 1. Add the count.
// 2. Filter by unique.
// 3. Merge together into single string.

// Get the count and add it to each string (do not add if count is 1).
$delaycounts = array_count_values($adhoctaskdelays);
$delayswithcounts = array_map(function($msg) use ($delaycounts) {
$count = $delaycounts[$msg];
return $count == 1 ? $msg : $msg . ' (' . $count . ' duplicates!!!)';
}, $adhoctaskdelays);

// Filter by unique - now only 1 line per task with count added to it.
$delayswithcounts = array_unique($delayswithcounts);

// Combine together.
$delay = implode("\n", $delayswithcounts) . "\n";

$maxminsdelay = $maxdelay / 60;
if ( $maxminsdelay > $options['delayerror'] ) {
send_critical("Moodle task faildelay > {$options['delayerror']} mins\n$delay");
Expand Down

0 comments on commit 86c445b

Please sign in to comment.