Skip to content

Commit

Permalink
Add a maximum runtime and improve messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottVerbeek committed Nov 6, 2023
1 parent f53bbf5 commit d8ccc8a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cli/resendfailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
--datefrom=<integer> Epoch from date to resend, default: null.
--dateto=<integer> Epoch to date to resend, default: null.
--batch=<integer> The batch size of each move iteration, default: 12500.
--runtime=<integer> The maximum amount of time this program can run in seconds, default: 300.
--dryrun=<integer> Runs the program without executing any write queries, default: 1.
-h --help Print this help.
";
Expand All @@ -54,6 +55,7 @@
'datefrom' => null,
'dateto' => null,
'dryrun' => true,
'runtime' => 300,
'batch' => 12500,
], [
'h' => 'help'
Expand Down Expand Up @@ -81,6 +83,10 @@
cli_write('To disable dryrun mode, add option --dryrun=0' . PHP_EOL);
}

$options['runtime'] = intval($options['runtime']);
cli_writeln("Program will stop after {$options['runtime']} seconds have passed, started at " .date('Y-m-d H:i:s T') . "...");
$starttime = microtime(true);

$options['batch'] = intval($options['batch']);
cli_writeln("Program will run in batches of {$options['batch']} records ...");

Expand Down Expand Up @@ -141,6 +147,11 @@
$countfail = 0;

do {
if (microtime(true) - $starttime >= $options['runtime']) {
cli_writeln("Stopping the program, the maximum runtime has been exceeded ({$options['runtime']} seconds).");
break; // Exit the loop after the specified runtime
}

cli_write("Reading at offset {$limitfrom} ...");
$records = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
$count = count($records);
Expand All @@ -162,11 +173,11 @@

if ($mover->execute()) {
$countsucc += $count;
cli_writeln("$count events successfully sent for reprocessing.");
cli_writeln("$count events successfully sent for reprocessing. Not increasing the offset (records were moved).");
} else {
$limitfrom += $count; // Increase the offset, when failed to move.
$countfail += $count;
cli_writeln("$count events failed to send for reprocessing.");
cli_writeln("$count events failed to send for reprocessing. Increasing the offset by {$count} (records were not moved).");
}
} while ($count > 0);

Expand Down

0 comments on commit d8ccc8a

Please sign in to comment.