diff --git a/src/Module.php b/src/Module.php index 2de4f45..2e702c5 100644 --- a/src/Module.php +++ b/src/Module.php @@ -107,6 +107,11 @@ public function getConfig(): Config protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void { + ini_set( + 'max_execution_time', + (string) $this->getMaxExecutionSeconds(), + ); + // Set Craft memory limit to just below PHP's limit $this->setMemoryLimit( ini_get('memory_limit'), @@ -221,4 +226,14 @@ private function setMemoryLimit(int|string $limit, int|string $offset = 0): int| return $memoryLimit; } + + private function getMaxExecutionSeconds(): int + { + if (Craft::$app->getRequest()->getIsWebRequest()) { + return 60; + } + + // Include a buffer so PHP's limit is always hit before Lambda's + return 900 - 3; + } } diff --git a/src/runtime/event/EventHandler.php b/src/runtime/event/EventHandler.php index 20f7dce..050e114 100644 --- a/src/runtime/event/EventHandler.php +++ b/src/runtime/event/EventHandler.php @@ -9,8 +9,6 @@ class EventHandler implements Handler { private FpmHandler $fpmHandler; - private const MAX_SECONDS = 900 - 3; - private const MAX_HTTP_SECONDS = 60; public function __construct(FpmHandler $fpmHandler) { @@ -19,8 +17,6 @@ public function __construct(FpmHandler $fpmHandler) public function handle(mixed $event, Context $context) { - ini_set('max_execution_time', self::MAX_SECONDS); - // is this a sqs event? if (isset($event['Records'])) { return (new SqsHandler())->handle($event, $context); @@ -31,8 +27,6 @@ public function handle(mixed $event, Context $context) return (new CliHandler())->handle($event, $context); } - ini_set('max_execution_time', self::MAX_HTTP_SECONDS); - return $this->fpmHandler->handle($event, $context); } }