Improve duration with timeout

This commit is contained in:
Matej Bačo
2022-12-17 12:34:20 +01:00
parent 18ab586cc6
commit 7127b8141c
2 changed files with 9 additions and 5 deletions
+6 -3
View File
@@ -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());
+3 -2
View File
@@ -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());