diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 6fe7c67e69..d059f2921c 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1188,6 +1188,8 @@ App::post('/v1/functions/:functionId/executions') ]); /** Execute function */ + $durationStart = \microtime(true); + $timeout = $function->getAttribute('timeout', 0); $executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST')); try { $executionResponse = $executor->createExecution( @@ -1195,7 +1197,7 @@ App::post('/v1/functions/:functionId/executions') deploymentId: $deployment->getId(), payload: $data, variables: $vars, - timeout: $function->getAttribute('timeout', 0), + timeout: $timeout, image: $runtime['image'], source: $build->getAttribute('outputPath', ''), entrypoint: $deployment->getAttribute('entrypoint', ''), @@ -1209,9 +1211,10 @@ App::post('/v1/functions/:functionId/executions') $execution->setAttribute('stderr', $executionResponse['stderr']); $execution->setAttribute('duration', $executionResponse['duration']); } catch (\Throwable $th) { - $interval = (new \DateTime())->diff(new \DateTime($execution->getCreatedAt())); + $durationEnd = \microtime(true); + $duration = ($durationEnd - $durationStart); $execution - ->setAttribute('duration', (float)$interval->format('%s.%f')) + ->setAttribute('duration', \min($duration, $timeout)) ->setAttribute('status', 'failed') ->setAttribute('statusCode', $th->getCode()) ->setAttribute('stderr', $th->getMessage()); diff --git a/app/workers/functions.php b/app/workers/functions.php index 097d0d55b8..f71abb74c3 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -125,6 +125,7 @@ Server::setResource('execute', function () { /** Execute function */ $durationStart = \microtime(true); + $timeout = $function->getAttribute('timeout', 0); try { $client = new Executor(App::getEnv('_APP_EXECUTOR_HOST')); $executionResponse = $client->createExecution( @@ -132,7 +133,7 @@ Server::setResource('execute', function () { deploymentId: $deploymentId, payload: $vars['APPWRITE_FUNCTION_DATA'] ?? '', variables: $vars, - timeout: $function->getAttribute('timeout', 0), + timeout: $timeout, image: $runtime['image'], source: $build->getAttribute('outputPath', ''), entrypoint: $deployment->getAttribute('entrypoint', ''), @@ -150,7 +151,7 @@ Server::setResource('execute', function () { $durationEnd = \microtime(true); $duration = ($durationEnd - $durationStart); $execution - ->setAttribute('duration', $duration) + ->setAttribute('duration', \min($duration, $timeout)) ->setAttribute('status', 'failed') ->setAttribute('statusCode', $th->getCode()) ->setAttribute('stderr', $th->getMessage());