Merge pull request #2704 from appwrite/feat-cleanup-executor
Feat cleanup executor
This commit is contained in:
commit
db6bf37e6a
1 changed files with 53 additions and 59 deletions
|
@ -25,7 +25,6 @@ use Utopia\Database\Query;
|
|||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Logger\Log;
|
||||
use Utopia\Orchestration\Adapter\DockerAPI;
|
||||
use Utopia\Orchestration\Adapter\DockerCLI;
|
||||
use Utopia\Orchestration\Orchestration;
|
||||
use Utopia\Registry\Registry;
|
||||
|
@ -365,7 +364,7 @@ function execute(string $trigger, string $projectId, string $executionId, string
|
|||
}
|
||||
|
||||
|
||||
if ($build->getAttribute('status') == 'building') {
|
||||
if ($build->getAttribute('status') === 'building') {
|
||||
|
||||
$execution
|
||||
->setAttribute('status', 'failed')
|
||||
|
@ -634,7 +633,7 @@ function execute(string $trigger, string $projectId, string $executionId, string
|
|||
roles: $target['roles']
|
||||
);
|
||||
|
||||
if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
|
||||
if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') {
|
||||
$statsd = $register->get('statsd');
|
||||
|
||||
$usage = new Stats($statsd);
|
||||
|
@ -654,7 +653,7 @@ function execute(string $trigger, string $projectId, string $executionId, string
|
|||
|
||||
return [
|
||||
'status' => $functionStatus,
|
||||
'response' => ($functionStatus !== 'completed') ? $stderr : $stdout,
|
||||
'response' => ($functionStatus !=== 'completed') ? $stderr : $stdout,
|
||||
'time' => $executionTime
|
||||
];
|
||||
};
|
||||
|
@ -870,7 +869,7 @@ function runBuildStage(string $buildId, string $deploymentId, string $projectID)
|
|||
}
|
||||
}
|
||||
|
||||
if ($buildStdout == '') {
|
||||
if ($buildStdout === '') {
|
||||
$buildStdout = 'Build Successful!';
|
||||
}
|
||||
|
||||
|
@ -922,16 +921,16 @@ function runBuildStage(string $buildId, string $deploymentId, string $projectID)
|
|||
|
||||
App::post('/v1/functions/:functionId/executions')
|
||||
->desc('Execute a function')
|
||||
->param('trigger', '', new Text(1024))
|
||||
->param('projectId', '', new Text(1024))
|
||||
->param('executionId', '', new Text(1024), '', true)
|
||||
->param('functionId', '', new Text(1024))
|
||||
->param('event', '', new Text(1024), '', true)
|
||||
->param('eventData', '', new Text(10240), '', true)
|
||||
->param('data', '', new Text(1024), '', true)
|
||||
->param('webhooks', [], new ArrayList(new JSON()), '', true)
|
||||
->param('userId', '', new Text(1024), '', true)
|
||||
->param('jwt', '', new Text(1024), '', true)
|
||||
->param('trigger', '', new Text(1024), 'What triggered this execution, can be http / schedule / event')
|
||||
->param('projectId', '', new Text(1024), 'The ProjectID this execution belongs to')
|
||||
->param('executionId', '', new Text(1024), 'An optional execution ID, If not specified a new execution document is created.', true)
|
||||
->param('functionId', '', new Text(1024), 'The FunctionID to execute')
|
||||
->param('event', '', new Text(1024), 'The event that triggered this execution', true)
|
||||
->param('eventData', '', new Text(0), 'Extra Data for the event', true)
|
||||
->param('data', '', new Text(1024), 'Data to be forwarded to the function, this is user specified.', true)
|
||||
->param('webhooks', [], new ArrayList(new JSON()), 'Any webhooks that need to be triggered after this execution', true)
|
||||
->param('userId', '', new Text(1024), 'The UserID of the user who triggered the execution if it was called from a client SDK', true)
|
||||
->param('jwt', '', new Text(1024), 'A JWT of the user who triggered the execution if it was called from a client SDK', true)
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->action(
|
||||
|
@ -945,7 +944,7 @@ App::post('/v1/functions/:functionId/executions')
|
|||
|
||||
App::delete('/v1/functions/:functionId')
|
||||
->desc('Delete a function')
|
||||
->param('functionId', '', new UID())
|
||||
->param('functionId', '', new UID(), 'The FunctionID to delete')
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->action(
|
||||
|
@ -966,12 +965,13 @@ App::delete('/v1/functions/:functionId')
|
|||
|
||||
// If amount is 0 then we simply return true
|
||||
if (count($results) === 0) {
|
||||
return $response->json(['success' => true]);
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_OK)
|
||||
->send();
|
||||
}
|
||||
|
||||
// Delete the containers of all deployments
|
||||
foreach ($results as $deployment) {
|
||||
try {
|
||||
// Remove any ongoing builds
|
||||
if ($deployment->getAttribute('buildId')) {
|
||||
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
|
||||
|
@ -985,19 +985,17 @@ App::delete('/v1/functions/:functionId')
|
|||
|
||||
$orchestration->remove('appwrite-function-' . $deployment['$id'], true);
|
||||
Console::info('Removed container for deployment ' . $deployment['$id']);
|
||||
} catch (Exception $e) {
|
||||
// Do nothing, we don't care that much if it fails
|
||||
}
|
||||
}
|
||||
|
||||
return $response->json(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
logError($e, "cleanupFunction");
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_OK)
|
||||
->send();
|
||||
} catch (Throwable $th) {
|
||||
$orchestrationPool->put($orchestration);
|
||||
throw $th;
|
||||
} finally {
|
||||
$orchestrationPool->put($orchestration);
|
||||
|
||||
return $response->json(['error' => $e->getMessage()]);
|
||||
}
|
||||
$orchestrationPool->put($orchestration);
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -1051,12 +1049,11 @@ App::delete('/v1/deployments/:deploymentId')
|
|||
throw new Exception('Deployment not found', 404);
|
||||
}
|
||||
|
||||
try {
|
||||
// Remove any ongoing builds
|
||||
if ($deployment->getAttribute('buildId')) {
|
||||
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
|
||||
|
||||
if ($build->getAttribute('status') == 'building') {
|
||||
if ($build->getAttribute('status') === 'building') {
|
||||
// Remove the build
|
||||
$orchestration->remove('build-stage-' . $deployment->getAttribute('buildId'), true);
|
||||
Console::info('Removed build for deployment ' . $deployment['$id']);
|
||||
|
@ -1066,18 +1063,15 @@ App::delete('/v1/deployments/:deploymentId')
|
|||
// Remove the container of the deployment
|
||||
$orchestration->remove('appwrite-function-' . $deployment['$id'], true);
|
||||
Console::info('Removed container for deployment ' . $deployment['$id']);
|
||||
} catch (Exception $e) {
|
||||
// Do nothing, we don't care that much if it fails
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
logError($e, "cleanupFunction");
|
||||
} catch (Throwable $th) {
|
||||
$orchestrationPool->put($orchestration);
|
||||
|
||||
return $response->json(['error' => $e->getMessage()]);
|
||||
throw $th;
|
||||
}
|
||||
$orchestrationPool->put($orchestration);
|
||||
|
||||
return $response->json(['success' => true]);
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_OK)
|
||||
->send();
|
||||
});
|
||||
|
||||
App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId')
|
||||
|
|
Loading…
Reference in a new issue