Address TODOs

This commit is contained in:
Matej Baco
2023-03-14 11:13:03 +00:00
parent e7351f4eec
commit ed610fcdd4
14 changed files with 152 additions and 22 deletions
+1 -1
View File
@@ -551,7 +551,7 @@ return [
],
Exception::RULE_VERIFICATION_FAILED => [
'name' => Exception::RULE_VERIFICATION_FAILED,
'description' => 'Domain verification for failed. Please check your DNS records.',
'description' => 'Domain verification failed. Please check your DNS records.',
'code' => 401,
],
Exception::WEBHOOK_NOT_FOUND => [
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+27 -6
View File
@@ -47,8 +47,6 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
include_once __DIR__ . '/../shared/api.php';
// TODO: @Meldiron Changes to variables must delete runtime on executor
App::post('/v1/functions')
->groups(['api', 'functions'])
->desc('Create Function')
@@ -131,7 +129,6 @@ App::post('/v1/functions')
]))
);
// TODO: @Meldiron this doesnt seem to work. Same for certificate.php worker. When working, implement with Console (instead of interval)
/** Trigger Webhook */
$ruleModel = new Rule();
$ruleCreate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME);
@@ -1504,9 +1501,10 @@ App::post('/v1/functions/:functionId/variables')
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('key', null, new Text(Database::LENGTH_KEY), 'Variable key. Max length: ' . Database::LENGTH_KEY . ' chars.', false)
->param('value', null, new Text(8192), 'Variable value. Max length: 8192 chars.', false)
->inject('project')
->inject('response')
->inject('dbForProject')
->action(function (string $functionId, string $key, string $value, Response $response, Database $dbForProject) {
->action(function (string $functionId, string $key, string $value, Document $project, Response $response, Database $dbForProject) {
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
@@ -1538,6 +1536,13 @@ App::post('/v1/functions/:functionId/variables')
$dbForProject->deleteCachedDocument('functions', $function->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->setFunction($function)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($variable, Response::MODEL_VARIABLE);
@@ -1622,9 +1627,10 @@ App::put('/v1/functions/:functionId/variables/:variableId')
->param('variableId', '', new UID(), 'Variable unique ID.', false)
->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', false)
->param('value', null, new Text(8192), 'Variable value. Max length: 8192 chars.', true)
->inject('project')
->inject('response')
->inject('dbForProject')
->action(function (string $functionId, string $variableId, string $key, ?string $value, Response $response, Database $dbForProject) {
->action(function (string $functionId, string $variableId, string $key, ?string $value, Document $project, Response $response, Database $dbForProject) {
$function = $dbForProject->getDocument('functions', $functionId);
@@ -1656,6 +1662,13 @@ App::put('/v1/functions/:functionId/variables/:variableId')
$dbForProject->deleteCachedDocument('functions', $function->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->setFunction($function)
->trigger();
$response->dynamic($variable, Response::MODEL_VARIABLE);
});
@@ -1673,9 +1686,10 @@ App::delete('/v1/functions/:functionId/variables/:variableId')
->label('sdk.response.model', Response::MODEL_NONE)
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('variableId', '', new UID(), 'Variable unique ID.', false)
->inject('project')
->inject('response')
->inject('dbForProject')
->action(function (string $functionId, string $variableId, Response $response, Database $dbForProject) {
->action(function (string $functionId, string $variableId, Document $project, Response $response, Database $dbForProject) {
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
@@ -1695,5 +1709,12 @@ App::delete('/v1/functions/:functionId/variables/:variableId')
$dbForProject->deleteDocument('variables', $variable->getId());
$dbForProject->deleteCachedDocument('functions', $function->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->setFunction($function)
->trigger();
$response->noContent();
});
+19 -2
View File
@@ -1,5 +1,6 @@
<?php
use Appwrite\Event\Delete;
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Response;
use Utopia\App;
@@ -16,8 +17,6 @@ use Utopia\Database\Validator\UID;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
// TODO: @Meldiron Changes to variables must delete runtime on executor
App::get('/v1/project/usage')
->desc('Get usage stats for a project')
->groups(['api'])
@@ -166,6 +165,12 @@ App::post('/v1/project/variables')
$dbForProject->deleteCachedDocument('projects', $project->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($variable, Response::MODEL_VARIABLE);
@@ -264,6 +269,12 @@ App::put('/v1/project/variables/:variableId')
$dbForProject->deleteCachedDocument('projects', $project->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->trigger();
$response->dynamic($variable, Response::MODEL_VARIABLE);
});
@@ -294,5 +305,11 @@ App::delete('/v1/project/variables/:variableId')
$dbForProject->deleteDocument('variables', $variable->getId());
$dbForProject->deleteCachedDocument('projects', $project->getId());
// Stop all running runtimes with this variable
(new Delete())
->setType(DELETE_TYPE_RUNTIMES)
->setProject($project)
->trigger();
$response->noContent();
});
+1 -3
View File
@@ -19,8 +19,6 @@ use Utopia\Domains\Domain;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
// TODO: @Meldiron Realtime and events QA
App::post('/v1/proxy/rules')
->groups(['api', 'proxy'])
->desc('Create Rule')
@@ -101,7 +99,7 @@ App::post('/v1/proxy/rules')
'resourceInternalId' => $resourceInternalId,
'status' => $status,
'certificateId' => '',
'search' => implode(' ', [ $domain->get(), $ruleId, $resourceId, $resourceType, $redirect ]),
'search' => implode(' ', [ $domain->get(), $ruleId, $resourceId, $resourceType ]),
]));
$events->setParam('ruleId', $rule->getId());
+1
View File
@@ -157,6 +157,7 @@ const DELETE_TYPE_SESSIONS = 'sessions';
const DELETE_TYPE_CACHE_BY_TIMESTAMP = 'cacheByTimeStamp';
const DELETE_TYPE_CACHE_BY_RESOURCE = 'cacheByResource';
const DELETE_TYPE_SCHEDULES = 'schedules';
const DELETE_TYPE_RUNTIMES = 'runtimes';
// Compression type
const COMPRESSION_TYPE_NONE = 'none';
const COMPRESSION_TYPE_GZIP = 'gzip';
+59 -2
View File
@@ -116,6 +116,10 @@ class DeletesV1 extends Worker
case DELETE_TYPE_SCHEDULES:
$this->deleteSchedules($this->args['datetime']);
break;
case DELETE_TYPE_RUNTIMES:
$function = $this->args['function'] == null ? null : new Document($this->args['function']);
$this->deleteRuntimes($function, $project);
break;
default:
Console::error('No delete operation for type: ' . $type);
break;
@@ -513,7 +517,11 @@ class DeletesV1 extends Worker
Query::equal('functionId', [$functionId])
], $dbForProject);
// TODO: @Meldiron Request executor to delete runtime
/**
* Request executor to delete all deployment containers
*/
Console::info("Requesting executor to delete all deployment containers for function " . $functionId);
$this->deleteRuntimes($document, $project);
}
/**
@@ -553,7 +561,12 @@ class DeletesV1 extends Worker
}
});
// TODO: @Meldiron Request executor to delete runtime
/**
* Request executor to delete all deployment containers
*/
Console::info("Requesting executor to delete deployment container for deployment " . $deploymentId);
$this->deleteRuntimes($document, $project);
}
@@ -723,4 +736,48 @@ class DeletesV1 extends Worker
$device->deletePath($document->getId());
}
protected function deleteRuntimes(?Document $function, Document $project) {
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
$deleteByFunction = function(Document $function) use ($project, $executor) {
$this->listByGroup(
'deployments',
[
Query::equal('resourceInternalId', [$function->getInternalId()]),
Query::equal('resourceType', ['functions']),
],
$this->getProjectDB($project),
function (Document $deployment) use ($project, $executor) {
$deploymentId = $deployment->getId();
try {
$executor->deleteRuntime($project->getId(), $deploymentId);
Console::info("Runtime for deployment {$deploymentId} deleted.");
} catch (Throwable $th) {
Console::warning("Runtime for deployment {$deploymentId} skipped:");
Console::error('[Error] Type: ' . get_class($th));
Console::error('[Error] Message: ' . $th->getMessage());
Console::error('[Error] File: ' . $th->getFile());
Console::error('[Error] Line: ' . $th->getLine());
}
}
);
};
if($function !== null) {
// Delete function runtimes
$deleteByFunction($function);
} else {
// Delete all project runtimes
$this->listByGroup(
'functions',
[],
$this->getProjectDB($project),
function (Document $function) use ($deleteByFunction) {
$deleteByFunction($function);
}
);
}
}
}
+14
View File
@@ -12,6 +12,7 @@ class Delete extends Event
protected ?string $resource = null;
protected ?string $datetime = null;
protected ?string $hourlyUsageRetentionDatetime = null;
protected ?Document $function = null;
public function __construct()
@@ -54,6 +55,18 @@ class Delete extends Event
return $this;
}
/**
* set Function
*
* @param Document $function
* @return self
*/
public function setFunction(Document $function): self
{
$this->function = $function;
return $this;
}
/**
* Sets datetime for 1h interval.
*
@@ -128,6 +141,7 @@ class Delete extends Event
'resource' => $this->resource,
'datetime' => $this->datetime,
'hourlyUsageRetentionDatetime' => $this->hourlyUsageRetentionDatetime,
'function' => $this->function
]);
}
}
+2 -2
View File
@@ -261,8 +261,8 @@ class Realtime extends Adapter
$roles = [Role::user(ID::custom($parts[1]))->toString()];
break;
case 'rules':
$channels[] = 'rules';
$channels[] = 'rules.' . $parts[1];
$channels[] = 'console';
$projectId = 'console';
$roles = [Role::team($project->getAttribute('teamId'))->toString()];
break;
case 'teams':
@@ -108,7 +108,6 @@ class Maintenance extends Action
function notifyDeleteCache($interval)
{
(new Delete())
->setType(DELETE_TYPE_CACHE_BY_TIMESTAMP)
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
@@ -117,7 +116,6 @@ class Maintenance extends Action
function notifyDeleteSchedules($interval)
{
(new Delete())
->setType(DELETE_TYPE_SCHEDULES)
->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval))
+24
View File
@@ -102,6 +102,30 @@ class Executor
return $response['body'];
}
/**
* Delete Runtime
*
* Deletes a runtime and cleans up any containers remaining.
*
* @param string $projectId
* @param string $deploymentId
*/
public function deleteRuntime(string $projectId, string $deploymentId)
{
$runtimeId = "$projectId-$deploymentId";
$route = "/runtimes/$runtimeId";
$response = $this->call(self::METHOD_DELETE, $route, [], [], true, 30);
$status = $response['headers']['status-code'];
if ($status >= 400) {
$message = \is_string($response['body']) ? $response['body'] : $response['body']['message'];
throw new \Exception($message, $status);
}
return $response['body'];
}
/**
* Create an execution
*