1
0
Fork 0
mirror of synced 2024-07-08 07:55:48 +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) {
try {
/** @var Orchestration $orchestration */ /** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get(); $orchestration = $orchestrationPool->get();
try {
// Get function document // Get function document
$function = $dbForProject->getDocument('functions', $functionId); $function = $dbForProject->getDocument('functions', $functionId);
@ -967,12 +958,12 @@ 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'));
@ -986,19 +977,16 @@ App::post('/v1/cleanup/function')
$orchestration->remove('appwrite-function-' . $deployment['$id'], true); $orchestration->remove('appwrite-function-' . $deployment['$id'], true);
Console::info('Removed container for deployment ' . $deployment['$id']); Console::info('Removed container for deployment ' . $deployment['$id']);
} catch (Exception $e) {
// Do nothing, we don't care that much if it fails
}
} }
$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) {
try {
/** @var Orchestration $orchestration */ /** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get(); $orchestration = $orchestrationPool->get();
try {
// Get deployment document // Get deployment document
$deployment = $dbForProject->getDocument('deployments', $deploymentId); $deployment = $dbForProject->getDocument('deployments', $deploymentId);
@ -1019,7 +1006,6 @@ 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'));
@ -1034,14 +1020,9 @@ App::post('/v1/cleanup/deployment')
// Remove the container of the deployment // Remove the container of the deployment
$orchestration->remove('appwrite-function-' . $deployment['$id'], true); $orchestration->remove('appwrite-function-' . $deployment['$id'], true);
Console::info('Removed container for deployment ' . $deployment['$id']); Console::info('Removed container for deployment ' . $deployment['$id']);
} catch (Exception $e) { } catch (Throwable $th) {
// Do nothing, we don't care that much if it fails
}
} catch (Exception $e) {
logError($e, "cleanupFunction");
$orchestrationPool->put($orchestration); $orchestrationPool->put($orchestration);
throw $th;
return $response->json(['error' => $e->getMessage()]);
} }
$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,7 +1064,6 @@ 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] : [],
@ -1110,10 +1090,6 @@ App::post('/v1/deployment')
$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,7 +1167,6 @@ 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);
@ -1214,15 +1192,6 @@ App::post('/v1/build/:buildId') // Start a Build
// return success // return success
return $response->json(['success' => true]); 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()]);
}
}); });
App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode