Merge branch 'feat-functions-refactor' of github.com:appwrite/appwrite into feat-add-builds-worker

This commit is contained in:
Christy Jacob
2022-01-25 21:11:29 +04:00
23 changed files with 1931 additions and 1710 deletions
+45 -48
View File
@@ -433,7 +433,7 @@ App::delete('/v1/functions/:functionId')
// Request executor to delete tag containers
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/cleanup/function");
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/cleanup/function");
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'functionId' => $functionId
@@ -496,14 +496,14 @@ App::post('/v1/functions/:functionId/tags')
->param('functionId', '', new UID(), 'Function ID.')
->param('entrypoint', '', new Text('1028'), 'Entrypoint File.')
->param('code', [], new File(), 'Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. Use a path that is within the current directory.', false)
->param('automaticDeploy', false, new Boolean(true), 'Automatically deploy the function when it is finished building.', false)
->param('deploy', false, new Boolean(true), 'Automatically deploy the function when it is finished building.', false)
->inject('request')
->inject('response')
->inject('dbForProject')
->inject('usage')
->inject('user')
->inject('project')
->action(function ($functionId, $entrypoint, $file, $automaticDeploy, $request, $response, $dbForProject, $usage, $user, $project) {
->action(function ($functionId, $entrypoint, $file, $deploy, $request, $response, $dbForProject, $usage, $user, $project) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
@@ -552,15 +552,15 @@ App::post('/v1/functions/:functionId/tags')
throw new Exception('Failed moving file', 500);
}
if ((bool) $automaticDeploy) {
// Remove automaticDeploy for all other tags.
if ((bool) $deploy) {
// Remove deploy for all other tags.
$tags = $dbForProject->find('tags', [
new Query('automaticDeploy', Query::TYPE_EQUAL, [true]),
new Query('deploy', Query::TYPE_EQUAL, [true]),
new Query('functionId', Query::TYPE_EQUAL, [$functionId])
]);
foreach ($tags as $tag) {
$tag->setAttribute('automaticDeploy', false);
$tag->setAttribute('deploy', false);
$dbForProject->updateDocument('tags', $tag->getId(), $tag);
}
}
@@ -581,7 +581,7 @@ App::post('/v1/functions/:functionId/tags')
'status' => 'processing',
'buildStdout' => '',
'buildStderr' => '',
'automaticDeploy' => ($automaticDeploy === 'true'),
'deploy' => ($deploy === 'true'),
]));
$usage
@@ -743,7 +743,7 @@ App::delete('/v1/functions/:functionId/tags/:tagId')
// Request executor to delete tag containers
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/cleanup/tag");
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/cleanup/tag");
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'tagId' => $tagId
@@ -901,7 +901,7 @@ App::post('/v1/functions/:functionId/executions')
// Directly execute function.
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/execute");
\curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/execute");
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'trigger' => 'http',
@@ -1029,50 +1029,47 @@ App::get('/v1/functions/:functionId/executions/:executionId')
});
App::get('/v1/builds')
->groups(['api', 'functions'])
->desc('Get Builds')
->label('scope', 'execution.read')
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'listBuilds')
->label('sdk.description', '/docs/references/functions/list-builds.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_BUILD_LIST)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('cursor', '', new UID(), 'ID of the build used as the starting point for the query, excluding the build itself. Should be used for efficient pagination when working with large sets of data.', true)
->param('cursorDirection', Database::CURSOR_AFTER, new WhiteList([Database::CURSOR_AFTER, Database::CURSOR_BEFORE]), 'Direction of the cursor.', true)
->inject('response')
->inject('dbForProject')
->action(function ($limit, $offset, $search, $cursor, $cursorDirection, $response, $dbForProject) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->groups(['api', 'functions'])
->desc('List Builds')
->label('scope', 'functions.read')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'builds')
->label('sdk.description', '/docs/references/functions/list-builds.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_BUILD_LIST)
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Maximum number of builds to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, APP_LIMIT_COUNT), 'Offset value. The default value is 0. Use this value to manage pagination. [learn more about pagination](https://appwrite.io/docs/pagination)', true)
->param('cursor', '', new UID(), 'ID of the function used as the starting point for the query, excluding the function itself. Should be used for efficient pagination when working with large sets of data. [learn more about pagination](https://appwrite.io/docs/pagination)', true)
->param('cursorDirection', Database::CURSOR_AFTER, new WhiteList([Database::CURSOR_AFTER, Database::CURSOR_BEFORE]), 'Direction of the cursor.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForProject')
->action(function ($search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForProject) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
if (!empty($cursor)) {
$cursorExecution = $dbForProject->getDocument('builds', $cursor);
if (!empty($cursor)) {
$cursorFunction = $dbForProject->getDocument('builds', $cursor);
if ($cursorExecution->isEmpty()) {
throw new Exception("Execution '{$cursor}' for the 'cursor' value not found.", 400);
}
if ($cursorFunction->isEmpty()) {
throw new Exception("Build '{$cursor}' for the 'cursor' value not found.", 400);
}
}
$queries = [];
$queries = [];
if (!empty($search)) {
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
if (!empty($search)) {
$queries[] = new Query('search', Query::TYPE_SEARCH, [$search]);
}
$results = $dbForProject->find('builds', $queries, $limit, $offset, [], [Database::ORDER_DESC], $cursorExecution ?? null, $cursorDirection);
$sum = $dbForProject->count('builds', $queries, APP_LIMIT_COUNT);
$response->dynamic(new Document([
'builds' => $results,
'sum' => $sum,
]), Response::MODEL_BUILD_LIST);
});
$response->dynamic(new Document([
'builds' => $dbForProject->find('builds', $queries, $limit, $offset, [], [$orderType], $cursorFunction ?? null, $cursorDirection),
'sum' => $dbForProject->count('builds', $queries, APP_LIMIT_COUNT),
]), Response::MODEL_BUILD_LIST);
});
App::get('/v1/builds/:buildId')
->groups(['api', 'functions'])