Compare commits

...
6 changed files with 40 additions and 24 deletions
+1 -1
View File
@@ -2574,7 +2574,7 @@ $collections = [
],
[
'$id' => ID::custom('duration'),
'type' => Database::VAR_INTEGER,
'type' => Database::VAR_FLOAT,
'format' => '',
'size' => 0,
'signed' => true,
+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());
+10 -9
View File
@@ -98,7 +98,7 @@ class BuildsV1 extends Worker
'$permissions' => [],
'startTime' => $startTime,
'deploymentId' => $deployment->getId(),
'status' => 'processing',
'status' => 'building',
'outputPath' => '',
'runtime' => $function->getAttribute('runtime'),
'source' => $deployment->getAttribute('path'),
@@ -112,11 +112,10 @@ class BuildsV1 extends Worker
$deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
} else {
$build = $dbForProject->getDocument('builds', $buildId);
}
/** Request the executor to build the code... */
$build->setAttribute('status', 'building');
$build = $dbForProject->updateDocument('builds', $buildId, $build);
$build->setAttribute('status', 'building');
$build = $dbForProject->updateDocument('builds', $buildId, $build);
}
/** Trigger Webhook */
$deploymentModel = new Deployment();
@@ -168,6 +167,8 @@ class BuildsV1 extends Worker
return $carry;
}, []);
/** Build deployment */
$durationStart = \microtime(true);
try {
$response = $this->executor->createRuntime(
projectId: $project->getId(),
@@ -191,7 +192,7 @@ class BuildsV1 extends Worker
/** Update the build document */
$build->setAttribute('endTime', DateTime::format($endTime));
$build->setAttribute('duration', \intval($response['duration']));
$build->setAttribute('duration', $response['duration']);
$build->setAttribute('status', $response['status']);
$build->setAttribute('outputPath', $response['outputPath']);
$build->setAttribute('stderr', $response['stderr']);
@@ -222,10 +223,10 @@ class BuildsV1 extends Worker
Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule));
} catch (\Throwable $th) {
$endTime = DateTime::now();
$interval = (new \DateTime($endTime))->diff(new \DateTime($startTime));
$durationEnd = \microtime(true);
$duration = ($durationEnd - $durationStart);
$build->setAttribute('endTime', $endTime);
$build->setAttribute('duration', $interval->format('%s') + 0);
$build->setAttribute('duration', $duration);
$build->setAttribute('status', 'failed');
$build->setAttribute('stderr', $th->getMessage());
Console::error($th->getMessage());
+10 -7
View File
@@ -84,7 +84,7 @@ Server::setResource('execute', function () {
'functionId' => $functionId,
'deploymentId' => $deploymentId,
'trigger' => $trigger,
'status' => 'waiting',
'status' => 'processing',
'statusCode' => 0,
'response' => '',
'stderr' => '',
@@ -97,11 +97,11 @@ Server::setResource('execute', function () {
if ($execution->isEmpty()) {
throw new Exception('Failed to create or read execution');
}
} else {
$execution->setAttribute('status', 'processing');
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
}
$execution->setAttribute('status', 'processing');
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
$vars = array_reduce($function->getAttribute('vars', []), function (array $carry, Document $var) {
$carry[$var->getAttribute('key')] = $var->getAttribute('value');
return $carry;
@@ -124,6 +124,8 @@ 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(
@@ -131,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', ''),
@@ -146,9 +148,10 @@ Server::setResource('execute', function () {
->setAttribute('stderr', $executionResponse['stderr'])
->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());
+11 -2
View File
@@ -56,9 +56,18 @@ class V17 extends Migration
} catch (\Throwable $th) {
Console::warning("'mimeType' from {$id}: {$th->getMessage()}");
}
break;
case 'builds':
try {
/**
* Update 'duration' attribute type (int->float)
*/
$this->projectDB->updateAttribute($id, 'duration', Database::VAR_FLOAT, 0, true, false);
$this->projectDB->deleteCachedCollection($id);
} catch (\Throwable $th) {
Console::warning("'duration' from {$id}: {$th->getMessage()}");
}
break;
default:
break;
}
+2 -2
View File
@@ -88,7 +88,7 @@ class Executor
'memory' => $this->memory,
];
$timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900);
$timeout = ((int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900)) + 2; // 2 secs network lattelcy allowed
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $timeout);
@@ -141,7 +141,7 @@ class Executor
'memory' => $this->memory,
];
$timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900);
$timeout = ((int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900)) + 2; // 2 secs network lattelcy allowed
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $timeout);