Skip to content

Commit

Permalink
Don't retry long jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Nov 22, 2023
1 parent 21c99bf commit 56bdd3d
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/runtime/event/SqsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Bref\Context\Context;
use Bref\Event\Sqs\SqsEvent;
use craft\cloud\runtime\Runtime;
use RuntimeException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
use Throwable;

class SqsHandler extends \Bref\Event\Sqs\SqsHandler
{
public const MAX_EXECUTION_BUFFER_SECONDS = 5;

public function handleSqs(SqsEvent $event, Context $context): void
{
foreach ($event->getRecords() as $record) {
Expand All @@ -33,17 +38,32 @@ public function handleSqs(SqsEvent $event, Context $context): void
'command' => "cloud/queue/exec {$jobId}",
], $context, true);
} catch (Throwable $e) {
echo "Marking SQS record as failed:\n";
if ($e instanceof ProcessTimedOutException) {
$process = $e->getProcess();

if (!$this->shouldRetry($process)) {
$runningTime = CliHandler::getRunningTime($process);
echo "Job ran for {$runningTime} seconds and will not be retried:\n";
echo "Message: #{$record->getMessageId()}\n";
echo "Job: " . ($jobId ? "#$jobId" : 'unknown');

throw $e;
}
}

echo "Marking SQS record as failed for retry:\n";
echo "Message: #{$record->getMessageId()}\n";
echo "Job: " . ($jobId ? "#$jobId" : 'unknown');

// TODO: if process has already run for 15(ish) minutes, don't retry it.
// if ($e instanceof ProcessTimedOutException) {
// $diff = Runtime::MAX_EXECUTION_SECONDS - CliHandler::getRunningTime($e->getProcess();
// }

$this->markAsFailed($record);
}
}
}

protected function shouldRetry(Process $process): bool
{
$diff = Runtime::MAX_EXECUTION_SECONDS - CliHandler::getRunningTime($process);

return $diff > static::MAX_EXECUTION_BUFFER_SECONDS;
}
}

0 comments on commit 56bdd3d

Please sign in to comment.