|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Runtime\Bref\Timeout; |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper class to trigger an exception just before the Lambda times out. This |
| 7 | + * will give the application a chance to shut down. |
| 8 | + * |
| 9 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 10 | + */ |
| 11 | +final class Timeout |
| 12 | +{ |
| 13 | + /** @var bool */ |
| 14 | + private static $initialized = false; |
| 15 | + |
| 16 | + /** @var string|null */ |
| 17 | + private static $stackTrace = null; |
| 18 | + |
| 19 | + /** |
| 20 | + * @internal |
| 21 | + */ |
| 22 | + public static function enable(int $remainingTimeInMillis): void |
| 23 | + { |
| 24 | + self::init(); |
| 25 | + |
| 26 | + if (!self::$initialized) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + $remainingTimeInSeconds = (int) floor($remainingTimeInMillis / 1000); |
| 31 | + |
| 32 | + // The script will timeout 2 seconds before the remaining time |
| 33 | + // to allow some time for Bref/our app to recover and cleanup |
| 34 | + $margin = 2; |
| 35 | + |
| 36 | + $timeoutDelayInSeconds = max(1, $remainingTimeInSeconds - $margin); |
| 37 | + |
| 38 | + // Trigger SIGALRM in X seconds |
| 39 | + pcntl_alarm($timeoutDelayInSeconds); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Setup custom handler for SIGALRM. |
| 44 | + */ |
| 45 | + private static function init(): void |
| 46 | + { |
| 47 | + self::$stackTrace = null; |
| 48 | + |
| 49 | + if (self::$initialized) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (! function_exists('pcntl_async_signals')) { |
| 54 | + trigger_error('Could not enable timeout exceptions because pcntl extension is not enabled.'); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + pcntl_async_signals(true); |
| 59 | + // Setup a handler for SIGALRM that throws an exception |
| 60 | + // This will interrupt any running PHP code, including `sleep()` or code stuck waiting for I/O. |
| 61 | + pcntl_signal(SIGALRM, function (): void { |
| 62 | + if (Timeout::$stackTrace !== null) { |
| 63 | + // we have already thrown an exception, do a harder exit. |
| 64 | + error_log('Lambda timed out'); |
| 65 | + error_log((new LambdaTimeoutException)->getTraceAsString()); |
| 66 | + error_log('Original stack trace'); |
| 67 | + error_log(Timeout::$stackTrace); |
| 68 | + |
| 69 | + exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + $exception = new LambdaTimeoutException('Maximum AWS Lambda execution time reached'); |
| 73 | + Timeout::$stackTrace = $exception->getTraceAsString(); |
| 74 | + |
| 75 | + // Trigger another alarm after 1 second to do a hard exit. |
| 76 | + pcntl_alarm(1); |
| 77 | + |
| 78 | + throw $exception; |
| 79 | + }); |
| 80 | + |
| 81 | + self::$initialized = true; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Cancel all current timeouts. |
| 86 | + * |
| 87 | + * @internal |
| 88 | + */ |
| 89 | + public static function reset(): void |
| 90 | + { |
| 91 | + if (self::$initialized) { |
| 92 | + pcntl_alarm(0); |
| 93 | + self::$stackTrace = null; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments