Skip to content

Commit

Permalink
Allow CliHandler to throw to trigger markAsFailed
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Nov 20, 2023
1 parent 33b6fbe commit 095f43e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
32 changes: 23 additions & 9 deletions src/runtime/event/CliHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Bref\Context\Context;
use Bref\Event\Handler;
use Exception;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
Expand All @@ -14,12 +13,15 @@ class CliHandler implements Handler
public const EXIT_CODE_TIMEOUT = 187;
protected string $scriptPath = '/var/task/craft';

public function handle(mixed $event, Context $context): array
/**
* @inheritDoc
*/
public function handle(mixed $event, Context $context, $throw = false): array
{
$commandArgs = $event['command'] ?? null;

if (!$commandArgs) {
throw new Exception('No command found.');
throw new \Exception('No command found.');
}

$php = PHP_BINARY;
Expand All @@ -28,6 +30,7 @@ public function handle(mixed $event, Context $context): array
$process = Process::fromShellCommandline($command, null, [
'LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR),
], null, $timeout);
$exitCode = null;

try {
echo "Running command: $command";
Expand All @@ -37,18 +40,29 @@ public function handle(mixed $event, Context $context): array
echo $buffer;
});

echo "Finished command: $command";
} catch (ProcessTimedOutException $e) {
$exitCode = self::EXIT_CODE_TIMEOUT;
echo "Process timed out for command: $command.\n";
echo "Command succeeded after {$this->getRunningTime($process)} seconds: $command\n";
} catch (\Throwable $e) {
echo $e->getMessage();
echo "Command failed after {$this->getRunningTime($process)} seconds: $command\n";
echo "{$e->getMessage()}\n";

$exitCode = $e instanceof ProcessTimedOutException
? self::EXIT_CODE_TIMEOUT
: $exitCode;

if ($throw) {
throw $e;
}
}

// 'output' is equivalent to stdout/stderr
return [
'exitCode' => $exitCode ?? $process->getExitCode(),
'output' => $process->getErrorOutput() . $process->getOutput(),
'runningTime' => $this->getRunningTime($process),
];
}

public static function getRunningTime(Process $process): float
{
return $process->getLastOutputTime() - $process->getStartTime();
}
}
7 changes: 6 additions & 1 deletion src/runtime/event/SqsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public function handleSqs(SqsEvent $event, Context $context): void

(new CliHandler())->handle([
'command' => "cloud/queue/exec {$jobId}",
] , $context);
], $context, true);
} catch (Throwable $e) {
echo "Marking SQS record as failed:\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);
}
}
Expand Down

0 comments on commit 095f43e

Please sign in to comment.