1
0
Fork 0
mirror of synced 2024-07-05 14:40:42 +12:00

Remove Try Catch's and use global handler

This commit is contained in:
Bradley Schofield 2022-01-27 09:18:01 +00:00
parent a3519ec912
commit c94b28555d

View file

@ -915,32 +915,24 @@ function runBuildStage(string $buildId, string $projectID): Document
App::post('/v1/execute') // Define Route App::post('/v1/execute') // Define Route
->desc('Execute a function') ->desc('Execute a function')
->param('trigger', '', new Text(1024)) ->param('trigger', '', new Text(1024), 'What triggered this execution, can be http / schedule / event')
->param('projectId', '', new Text(1024)) ->param('projectId', '', new Text(1024), 'The ProjectID this execution belongs to')
->param('executionId', '', new Text(1024), '', true) ->param('executionId', '', new Text(1024), 'An optional execution ID, If not specified a new execution document is created.', true)
->param('functionId', '', new Text(1024)) ->param('functionId', '', new Text(1024), 'The FunctionID to execute')
->param('event', '', new Text(1024), '', true) ->param('event', '', new Text(1024), 'The event that triggered this execution', true)
->param('eventData', '', new Text(10240), '', true) ->param('eventData', '', new Text(0), 'Extra Data for the event', true)
->param('data', '', new Text(1024), '', true) ->param('data', '', new Text(1024), 'Data to be forwarded to the function, this is user specified.', true)
->param('webhooks', [], new ArrayList(new JSON()), '', true) ->param('webhooks', [], new ArrayList(new JSON()), 'Any webhooks that need to be triggered after this execution', true)
->param('userId', '', new Text(1024), '', 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), '', 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('response')
->inject('dbForProject') ->inject('dbForProject')
->action( ->action(
function (string $trigger, string $projectId, string $executionId, string $functionId, string $event, string $eventData, string $data, array $webhooks, string $userId, string $jwt, Response $response, Database $dbForProject) { function (string $trigger, string $projectId, string $executionId, string $functionId, string $event, string $eventData, string $data, array $webhooks, string $userId, string $jwt, Response $response, Database $dbForProject) {
try { $data = execute($trigger, $projectId, $executionId, $functionId, $dbForProject, $event, $eventData, $data, $webhooks, $userId, $jwt);
$data = execute($trigger, $projectId, $executionId, $functionId, $dbForProject, $event, $eventData, $data, $webhooks, $userId, $jwt);
$response->json($data);
} catch (Exception $e) {
logError($e, 'executeEndpoint');
$response $response->setStatusCode(Response::STATUS_CODE_OK);
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') $response->json($data);
->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache')
->json(['error' => $e->getMessage()]);
}
} }
); );
@ -951,10 +943,9 @@ App::post('/v1/cleanup/function')
->inject('dbForProject') ->inject('dbForProject')
->action( ->action(
function (string $functionId, Response $response, Database $dbForProject) use ($orchestrationPool) { function (string $functionId, Response $response, Database $dbForProject) use ($orchestrationPool) {
/** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get();
try { try {
/** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get();
// Get function document // Get function document
$function = $dbForProject->getDocument('functions', $functionId); $function = $dbForProject->getDocument('functions', $functionId);
@ -967,38 +958,35 @@ App::post('/v1/cleanup/function')
// If amount is 0 then we simply return true // If amount is 0 then we simply return true
if (count($results) === 0) { if (count($results) === 0) {
$response->setStatusCode(Response::STATUS_CODE_OK);
return $response->json(['success' => true]); return $response->json(['success' => true]);
} }
// Delete the containers of all deployments // Delete the containers of all deployments
foreach ($results as $deployment) { foreach ($results as $deployment) {
try { // Remove any ongoing builds
// Remove any ongoing builds if ($deployment->getAttribute('buildId')) {
if ($deployment->getAttribute('buildId')) { $build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
if ($build->getAttribute('status') === 'building') { if ($build->getAttribute('status') === 'building') {
// Remove the build // Remove the build
$orchestration->remove('build-stage-' . $deployment->getAttribute('buildId'), true); $orchestration->remove('build-stage-' . $deployment->getAttribute('buildId'), true);
Console::info('Removed build for deployment ' . $deployment['$id']); Console::info('Removed build for deployment ' . $deployment['$id']);
}
} }
$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
} }
$orchestration->remove('appwrite-function-' . $deployment['$id'], true);
Console::info('Removed container for deployment ' . $deployment['$id']);
} }
$response->setStatusCode(Response::STATUS_CODE_OK);
return $response->json(['success' => true]); return $response->json(['success' => true]);
} catch (Exception $e) { } catch (Throwable $th) {
logError($e, "cleanupFunction"); $orchestrationPool->put($orchestration);
throw $th;
} finally {
$orchestrationPool->put($orchestration); $orchestrationPool->put($orchestration);
return $response->json(['error' => $e->getMessage()]);
} }
$orchestrationPool->put($orchestration);
} }
); );
@ -1007,10 +995,9 @@ App::post('/v1/cleanup/deployment')
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $deploymentId, Response $response, Database $dbForProject) use ($orchestrationPool) { ->action(function (string $deploymentId, Response $response, Database $dbForProject) use ($orchestrationPool) {
/** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get();
try { try {
/** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get();
// Get deployment document // Get deployment document
$deployment = $dbForProject->getDocument('deployments', $deploymentId); $deployment = $dbForProject->getDocument('deployments', $deploymentId);
@ -1019,29 +1006,23 @@ App::post('/v1/cleanup/deployment')
throw new Exception('Deployment not found', 404); throw new Exception('Deployment not found', 404);
} }
try { // Remove any ongoing builds
// Remove any ongoing builds if ($deployment->getAttribute('buildId')) {
if ($deployment->getAttribute('buildId')) { $build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId'));
if ($build->getAttribute('status') == 'building') { if ($build->getAttribute('status') == 'building') {
// Remove the build // Remove the build
$orchestration->remove('build-stage-' . $deployment->getAttribute('buildId'), true); $orchestration->remove('build-stage-' . $deployment->getAttribute('buildId'), true);
Console::info('Removed build for deployment ' . $deployment['$id']); Console::info('Removed build for deployment ' . $deployment['$id']);
}
} }
// 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");
$orchestrationPool->put($orchestration);
return $response->json(['error' => $e->getMessage()]); // Remove the container of the deployment
$orchestration->remove('appwrite-function-' . $deployment['$id'], true);
Console::info('Removed container for deployment ' . $deployment['$id']);
} catch (Throwable $th) {
$orchestrationPool->put($orchestration);
throw $th;
} }
$orchestrationPool->put($orchestration); $orchestrationPool->put($orchestration);
@ -1074,7 +1055,7 @@ App::post('/v1/deployment')
$runtime = $runtimes[$function->getAttribute('runtime')] ?? null; $runtime = $runtimes[$function->getAttribute('runtime')] ?? null;
if (\is_null($runtime)) { if (\is_null($runtime)) {
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported'); throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported', 404);
} }
// Create a new build entry // Create a new build entry
@ -1083,37 +1064,32 @@ App::post('/v1/deployment')
if ($deployment->getAttribute('buildId')) { if ($deployment->getAttribute('buildId')) {
$buildId = $deployment->getAttribute('buildId'); $buildId = $deployment->getAttribute('buildId');
} else { } else {
try { $dbForProject->createDocument('builds', new Document([
$dbForProject->createDocument('builds', new Document([ '$id' => $buildId,
'$id' => $buildId, '$read' => (!empty($userId)) ? ['user:' . $userId] : [],
'$read' => (!empty($userId)) ? ['user:' . $userId] : [], '$write' => ['role:all'],
'$write' => ['role:all'], 'dateCreated' => time(),
'dateCreated' => time(), 'status' => 'processing',
'status' => 'processing', 'runtime' => $function->getAttribute('runtime'),
'runtime' => $function->getAttribute('runtime'), 'outputPath' => '',
'outputPath' => '', 'source' => $deployment->getAttribute('path'),
'source' => $deployment->getAttribute('path'), 'sourceType' => Storage::DEVICE_LOCAL,
'sourceType' => Storage::DEVICE_LOCAL, 'stdout' => '',
'stdout' => '', 'stderr' => '',
'stderr' => '', 'time' => 0,
'time' => 0, 'vars' => [
'vars' => [ 'ENTRYPOINT_NAME' => $deployment->getAttribute('entrypoint'),
'ENTRYPOINT_NAME' => $deployment->getAttribute('entrypoint'), 'APPWRITE_FUNCTION_ID' => $function->getId(),
'APPWRITE_FUNCTION_ID' => $function->getId(), 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''), 'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'],
'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'], 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'],
'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'], 'APPWRITE_FUNCTION_PROJECT_ID' => $projectID,
'APPWRITE_FUNCTION_PROJECT_ID' => $projectID, ]
] ]));
]));
$deployment->setAttribute('buildId', $buildId); $deployment->setAttribute('buildId', $buildId);
$dbForProject->updateDocument('deployments', $deployment->getId(), $deployment); $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
} catch (\Throwable $th) {
var_dump($deployment->getArrayCopy());
throw $th;
}
} }
// Build Code // Build Code
@ -1156,6 +1132,9 @@ App::post('/v1/deployment')
// Deploy Runtime Server // Deploy Runtime Server
createRuntimeServer($functionId, $projectID, $deploymentId, $dbForProject); createRuntimeServer($functionId, $projectID, $deploymentId, $dbForProject);
} catch (\Throwable $th) { } catch (\Throwable $th) {
$register->get('dbPool')->put($db);
$register->get('redisPool')->put($redis);
throw $th;
} finally { } finally {
$register->get('dbPool')->put($db); $register->get('dbPool')->put($db);
$register->get('redisPool')->put($redis); $register->get('redisPool')->put($redis);
@ -1188,41 +1167,31 @@ App::post('/v1/build/:buildId') // Start a Build
->inject('dbForProject') ->inject('dbForProject')
->inject('projectID') ->inject('projectID')
->action(function (string $buildId, Response $response, Database $dbForProject, string $projectID) { ->action(function (string $buildId, Response $response, Database $dbForProject, string $projectID) {
try { // Get build document
// Get build document $build = $dbForProject->getDocument('builds', $buildId);
$build = $dbForProject->getDocument('builds', $buildId);
// Check if build exists // Check if build exists
if ($build->isEmpty()) { if ($build->isEmpty()) {
throw new Exception('Build not found', 404); throw new Exception('Build not found', 404);
}
// Check if build is already running
if ($build->getAttribute('status') === 'running') {
throw new Exception('Build is already running', 409);
}
// Check if build is already finished
if ($build->getAttribute('status') === 'finished') {
throw new Exception('Build is already finished', 409);
}
go(function () use ($buildId, $dbForProject, $projectID) {
// Build Code
runBuildStage($buildId, $projectID, $dbForProject);
});
// return success
return $response->json(['success' => true]);
} catch (Exception $e) {
logError($e, "buildEndpoint");
$response
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache')
->json(['error' => $e->getMessage()]);
} }
// Check if build is already running
if ($build->getAttribute('status') === 'running') {
throw new Exception('Build is already running', 409);
}
// Check if build is already finished
if ($build->getAttribute('status') === 'finished') {
throw new Exception('Build is already finished', 409);
}
go(function () use ($buildId, $dbForProject, $projectID) {
// Build Code
runBuildStage($buildId, $projectID, $dbForProject);
});
// return success
return $response->json(['success' => true]);
}); });
App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode