1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/src/Appwrite/Platform/Workers/Deletes.php

1199 lines
44 KiB
PHP
Raw Normal View History

<?php
2023-06-06 04:13:00 +12:00
namespace Appwrite\Platform\Workers;
2022-11-22 03:24:52 +13:00
use Appwrite\Auth\Auth;
use Appwrite\Extend\Exception;
2023-08-24 08:19:22 +12:00
use Executor\Executor;
2023-10-01 21:04:12 +13:00
use Throwable;
2023-06-06 04:13:00 +12:00
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Audit\Audit;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
2021-07-14 07:24:52 +12:00
use Utopia\Database\Database;
2023-06-06 04:13:00 +12:00
use Utopia\App;
use Utopia\CLI\Console;
2022-11-22 03:24:52 +13:00
use Utopia\Database\DateTime;
2023-06-06 04:13:00 +12:00
use Utopia\Database\Document;
use Utopia\Database\Exception\Authorization;
2023-10-01 21:04:12 +13:00
use Utopia\Database\Exception\Conflict;
use Utopia\Database\Exception\Restricted;
use Utopia\Database\Exception\Structure;
2024-01-15 18:26:30 +13:00
use Utopia\Database\Exception as DatabaseException;
2023-06-06 04:13:00 +12:00
use Utopia\Database\Query;
use Utopia\Logger\Log;
2023-06-06 04:13:00 +12:00
use Utopia\Platform\Action;
use Utopia\Queue\Message;
use Utopia\Storage\Device;
2023-06-06 04:13:00 +12:00
class Deletes extends Action
{
2023-06-06 04:13:00 +12:00
public static function getName(): string
2022-05-16 21:58:17 +12:00
{
2023-06-06 04:13:00 +12:00
return 'deletes';
}
2023-06-06 04:13:00 +12:00
/**
* @throws Exception
*/
public function __construct()
{
2023-06-06 04:13:00 +12:00
$this
->desc('Deletes worker')
->inject('message')
->inject('dbForConsole')
->inject('getProjectDB')
2024-02-21 03:10:51 +13:00
->inject('deviceForFiles')
->inject('deviceForFunctions')
->inject('deviceForBuilds')
->inject('deviceForCache')
2023-12-13 00:26:44 +13:00
->inject('abuseRetention')
->inject('executionRetention')
->inject('auditRetention')
->inject('log')
2024-02-21 03:10:51 +13:00
->callback(fn ($message, $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, string $abuseRetention, string $executionRetention, string $auditRetention, Log $log) => $this->action($message, $dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $abuseRetention, $executionRetention, $auditRetention, $log));
}
2023-06-06 04:13:00 +12:00
/**
* @throws Exception
2023-10-01 21:04:12 +13:00
* @throws Throwable
2023-06-06 04:13:00 +12:00
*/
2024-02-21 03:10:51 +13:00
public function action(Message $message, Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, string $abuseRetention, string $executionRetention, string $auditRetention, Log $log): void
{
2023-06-06 04:13:00 +12:00
$payload = $message->getPayload() ?? [];
if (empty($payload)) {
throw new Exception('Missing payload');
}
$type = $payload['type'] ?? '';
$datetime = $payload['datetime'] ?? null;
$hourlyUsageRetentionDatetime = $payload['hourlyUsageRetentionDatetime'] ?? null;
$resource = $payload['resource'] ?? null;
$document = new Document($payload['document'] ?? []);
2023-06-12 02:08:48 +12:00
$project = new Document($payload['project'] ?? []);
2023-06-06 04:13:00 +12:00
$log->addTag('projectId', $project->getId());
$log->addTag('type', $type);
2024-01-03 02:02:11 +13:00
switch (\strval($type)) {
2020-12-19 03:05:15 +13:00
case DELETE_TYPE_DOCUMENT:
2021-06-12 22:07:26 +12:00
switch ($document->getCollection()) {
case DELETE_TYPE_DATABASES:
$this->deleteDatabase($getProjectDB, $document, $project);
break;
case DELETE_TYPE_COLLECTIONS:
$this->deleteCollection($getProjectDB, $document, $project);
break;
2021-10-26 13:14:55 +13:00
case DELETE_TYPE_PROJECTS:
2024-02-21 03:10:51 +13:00
$this->deleteProject($dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $document);
break;
2021-10-26 13:14:55 +13:00
case DELETE_TYPE_FUNCTIONS:
2024-02-21 03:10:51 +13:00
$this->deleteFunction($dbForConsole, $getProjectDB, $deviceForFunctions, $deviceForBuilds, $document, $project);
break;
case DELETE_TYPE_DEPLOYMENTS:
2024-02-21 03:10:51 +13:00
$this->deleteDeployment($getProjectDB, $deviceForFunctions, $deviceForBuilds, $document, $project);
break;
2021-10-26 13:14:55 +13:00
case DELETE_TYPE_USERS:
2023-06-06 04:13:00 +12:00
$this->deleteUser($getProjectDB, $document, $project);
break;
2021-10-26 13:14:55 +13:00
case DELETE_TYPE_TEAMS:
2023-10-03 21:40:34 +13:00
$this->deleteMemberships($getProjectDB, $document, $project);
if ($project->getId() === 'console') {
2024-02-21 03:10:51 +13:00
$this->deleteProjectsByTeam($dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $document);
}
break;
2021-11-07 18:54:28 +13:00
case DELETE_TYPE_BUCKETS:
2024-02-21 03:10:51 +13:00
$this->deleteBucket($getProjectDB, $deviceForFiles, $document, $project);
break;
case DELETE_TYPE_INSTALLATIONS:
2023-10-03 21:40:34 +13:00
$this->deleteInstallation($dbForConsole, $getProjectDB, $document, $project);
break;
2023-03-10 20:42:52 +13:00
case DELETE_TYPE_RULES:
2023-10-18 07:32:38 +13:00
$this->deleteRule($dbForConsole, $document);
2021-07-28 22:09:29 +12:00
break;
default:
if (\str_starts_with($document->getCollection(), 'database_')) {
$this->deleteCollection($getProjectDB, $document, $project);
break;
}
2021-09-01 21:13:23 +12:00
Console::error('No lazy delete operation available for document of type: ' . $document->getCollection());
break;
}
2020-12-15 10:26:37 +13:00
break;
2020-12-28 06:57:35 +13:00
case DELETE_TYPE_EXECUTIONS:
2023-12-13 00:26:44 +13:00
$this->deleteExecutionLogs($project, $getProjectDB, $executionRetention);
2020-12-22 07:15:52 +13:00
break;
2020-12-19 03:05:15 +13:00
case DELETE_TYPE_AUDIT:
2023-12-13 00:26:44 +13:00
if (!$project->isEmpty()) {
$this->deleteAuditLogs($project, $getProjectDB, $auditRetention);
}
if (!$document->isEmpty()) {
2023-06-06 04:13:00 +12:00
$this->deleteAuditLogsByResource($getProjectDB, 'document/' . $document->getId(), $project);
}
break;
2020-12-19 03:05:15 +13:00
case DELETE_TYPE_ABUSE:
2023-12-13 00:26:44 +13:00
$this->deleteAbuseLogs($project, $getProjectDB, $abuseRetention);
break;
case DELETE_TYPE_REALTIME:
2023-10-18 07:32:38 +13:00
$this->deleteRealtimeUsage($dbForConsole, $datetime);
break;
case DELETE_TYPE_SESSIONS:
2023-12-13 00:26:44 +13:00
$this->deleteExpiredSessions($project, $getProjectDB);
break;
case DELETE_TYPE_USAGE:
2023-12-13 00:26:44 +13:00
$this->deleteUsageStats($project, $getProjectDB, $hourlyUsageRetentionDatetime);
2021-09-06 21:39:36 +12:00
break;
2022-08-15 21:05:41 +12:00
case DELETE_TYPE_CACHE_BY_RESOURCE:
$this->deleteCacheByResource($project, $getProjectDB, $resource);
2022-08-15 21:05:41 +12:00
break;
case DELETE_TYPE_CACHE_BY_TIMESTAMP:
2023-10-01 21:04:12 +13:00
$this->deleteCacheByDate($project, $getProjectDB, $datetime);
2022-07-03 21:36:59 +12:00
break;
2022-11-17 01:51:43 +13:00
case DELETE_TYPE_SCHEDULES:
2024-01-26 00:35:04 +13:00
$this->deleteSchedules($dbForConsole, $getProjectDB, $datetime);
2022-11-17 01:51:43 +13:00
break;
2023-10-26 06:33:23 +13:00
case DELETE_TYPE_TOPIC:
$this->deleteTopic($project, $getProjectDB, $document);
2023-10-18 06:23:26 +13:00
break;
case DELETE_TYPE_TARGET:
$this->deleteTargetSubscribers($project, $getProjectDB, $document);
break;
2024-01-19 17:23:44 +13:00
case DELETE_TYPE_EXPIRED_TARGETS:
$this->deleteExpiredTargets($project, $getProjectDB);
break;
case DELETE_TYPE_SESSION_TARGETS:
$this->deleteSessionTargets($project, $getProjectDB, $document);
break;
2020-12-19 21:08:03 +13:00
default:
2024-01-03 02:02:11 +13:00
throw new \Exception('No delete operation for type: ' . \strval($type));
2021-09-01 21:13:23 +12:00
}
}
2022-11-17 01:51:43 +13:00
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
* @param callable $getProjectDB
* @param string $datetime
2024-01-15 18:26:30 +13:00
* @param Document|null $document
2023-06-06 04:13:00 +12:00
* @return void
* @throws Authorization
2024-01-15 18:26:30 +13:00
* @throws Conflict
* @throws Restricted
* @throws Structure
* @throws DatabaseException
2022-11-17 01:51:43 +13:00
*/
2024-01-26 00:35:04 +13:00
private function deleteSchedules(Database $dbForConsole, callable $getProjectDB, string $datetime): void
2022-11-17 01:51:43 +13:00
{
2022-12-14 21:44:40 +13:00
$this->listByGroup(
2022-11-17 03:31:49 +13:00
'schedules',
[
2022-11-17 01:51:43 +13:00
Query::equal('region', [App::getEnv('_APP_REGION', 'default')]),
Query::lessThanEqual('resourceUpdatedAt', $datetime),
Query::equal('active', [false]),
2022-11-17 03:31:49 +13:00
],
2023-06-06 04:13:00 +12:00
$dbForConsole,
function (Document $document) use ($dbForConsole, $getProjectDB) {
$project = $dbForConsole->getDocument('projects', $document->getAttribute('projectId'));
2022-11-17 01:51:43 +13:00
if ($project->isEmpty()) {
$dbForConsole->deleteDocument('schedules', $document->getId());
Console::success('Deleted schedule for deleted project ' . $document->getAttribute('projectId'));
2022-11-17 03:31:49 +13:00
return;
2022-11-17 01:51:43 +13:00
}
2024-02-21 03:25:01 +13:00
$collectionId = match ($document->getAttribute('resourceType')) {
'function' => 'functions',
'message' => 'messages'
};
2024-01-15 19:00:41 +13:00
$resource = $getProjectDB($project)->getDocument(
2024-02-21 03:25:01 +13:00
$collectionId,
2024-01-15 19:00:41 +13:00
$document->getAttribute('resourceId')
);
2022-11-17 01:51:43 +13:00
2024-01-15 19:00:41 +13:00
$delete = true;
switch ($document->getAttribute('resourceType')) {
case 'function':
$delete = $resource->isEmpty();
break;
}
2022-11-17 01:51:43 +13:00
2024-01-15 19:00:41 +13:00
if ($delete) {
2023-06-06 04:13:00 +12:00
$dbForConsole->deleteDocument('schedules', $document->getId());
2024-01-15 19:00:41 +13:00
Console::success('Deleting schedule for ' . $document->getAttribute('resourceType') . ' ' . $document->getAttribute('resourceId'));
2022-11-17 01:51:43 +13:00
}
}
2022-11-17 03:31:49 +13:00
);
2022-11-17 01:51:43 +13:00
}
2023-10-18 06:23:26 +13:00
/**
* @param Document $project
* @param callable $getProjectDB
2023-10-18 06:23:26 +13:00
* @param Document $topic
* @throws Exception
*/
private function deleteTopic(Document $project, callable $getProjectDB, Document $topic)
2023-10-18 06:23:26 +13:00
{
if ($topic->isEmpty()) {
Console::error('Failed to delete subscribers. Topic not found');
return;
}
$this->deleteByGroup(
'subscribers',
[
Query::equal('topicInternalId', [$topic->getInternalId()])
],
$getProjectDB($project)
);
2023-10-18 06:23:26 +13:00
}
/**
* @param Document $project
* @param callable $getProjectDB
* @param Document $target
* @throws Exception
*/
private function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target): void
{
/** @var Database */
$dbForProject = $getProjectDB($project);
// Delete subscribers and decrement topic counts
$this->deleteByGroup(
'subscribers',
[
Query::equal('targetInternalId', [$target->getInternalId()])
],
$dbForProject,
function (Document $subscriber) use ($dbForProject, $target) {
$topicId = $subscriber->getAttribute('topicId');
$topicInternalId = $subscriber->getAttribute('topicInternalId');
$topic = $dbForProject->getDocument('topics', $topicId);
if (!$topic->isEmpty() && $topic->getInternalId() === $topicInternalId) {
$totalAttribute = match ($target->getAttribute('providerType')) {
MESSAGE_TYPE_EMAIL => 'emailTotal',
MESSAGE_TYPE_SMS => 'smsTotal',
MESSAGE_TYPE_PUSH => 'pushTotal',
default => throw new Exception('Invalid target provider type'),
};
$dbForProject->decreaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
min: 0
);
}
}
);
}
2024-01-19 17:23:44 +13:00
/**
* @param Document $project
* @param callable $getProjectDB
* @param Document $target
* @return void
* @throws Exception
*/
private function deleteExpiredTargets(Document $project, callable $getProjectDB): void
2024-01-19 17:23:44 +13:00
{
$this->deleteByGroup(
'targets',
[
Query::equal('expired', [true])
],
$getProjectDB($project),
function (Document $target) use ($getProjectDB, $project) {
$this->deleteTargetSubscribers($project, $getProjectDB, $target);
2024-01-19 17:23:44 +13:00
}
);
}
private function deleteSessionTargets(Document $project, callable $getProjectDB, Document $session): void
{
$this->deleteByGroup(
'targets',
[
Query::equal('sessionInternalId', [$session->getInternalId()])
],
$getProjectDB($project),
function (Document $target) use ($getProjectDB, $project) {
$this->deleteTargetSubscribers($project, $getProjectDB, $target);
}
);
}
2022-07-03 21:36:59 +12:00
/**
2023-06-13 20:15:38 +12:00
* @param Document $project
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
* @param string $resource
2023-10-02 06:39:26 +13:00
* @return void
2023-10-01 21:04:12 +13:00
* @throws Authorization
2022-07-03 21:36:59 +12:00
*/
private function deleteCacheByResource(Document $project, callable $getProjectDB, string $resource): void
2022-08-15 21:05:41 +12:00
{
2023-06-13 20:15:38 +12:00
$projectId = $project->getId();
2023-10-01 21:04:12 +13:00
$dbForProject = $getProjectDB($project);
$document = $dbForProject->findOne('cache', [Query::equal('resource', [$resource])]);
if ($document) {
$cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId)
);
$this->deleteById(
$document,
$dbForProject,
function ($document) use ($cache, $projectId) {
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId();
if ($cache->purge($document->getId())) {
Console::success('Deleting cache file: ' . $path);
} else {
Console::error('Failed to delete cache file: ' . $path);
}
}
);
}
2022-08-15 21:05:41 +12:00
}
2023-06-13 20:15:38 +12:00
/**
2023-10-01 21:04:12 +13:00
* Document $project
* @param Document $project
* @param callable $getProjectDB
2023-06-13 20:15:38 +12:00
* @param string $datetime
2023-10-01 21:04:12 +13:00
* @return void
2023-06-13 20:15:38 +12:00
* @throws Exception
*/
2023-10-18 07:32:38 +13:00
private function deleteCacheByDate(Document $project, callable $getProjectDB, string $datetime): void
2022-07-03 21:36:59 +12:00
{
2023-10-01 21:04:12 +13:00
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
2022-07-03 21:36:59 +12:00
2023-10-01 21:04:12 +13:00
$cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId)
);
2023-06-13 20:15:38 +12:00
2023-10-01 21:04:12 +13:00
$query = [
Query::lessThan('accessedAt', $datetime),
];
2023-10-01 21:04:12 +13:00
$this->deleteByGroup(
'cache',
$query,
$dbForProject,
function (Document $document) use ($cache, $projectId) {
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId();
if ($cache->purge($document->getId())) {
Console::success('Deleting cache file: ' . $path);
} else {
Console::error('Failed to delete cache file: ' . $path);
2022-07-05 19:37:46 +12:00
}
2023-10-01 21:04:12 +13:00
}
);
2022-07-03 21:36:59 +12:00
}
/**
* @param callable $getProjectDB
* @param Document $document
* @param Document $project
* @return void
* @throws Exception
*/
private function deleteDatabase(callable $getProjectDB, Document $document, Document $project): void
{
$databaseId = $document->getId();
$dbForProject = $getProjectDB($project);
$this->deleteByGroup('database_' . $document->getInternalId(), [], $dbForProject, function ($document) use ($getProjectDB, $project) {
$this->deleteCollection($getProjectDB, $document, $project);
});
$dbForProject->deleteCollection('database_' . $document->getInternalId());
$this->deleteAuditLogsByResource($getProjectDB, 'database/' . $databaseId, $project);
}
/**
* @param callable $getProjectDB
* @param Document $document teams document
* @param Document $project
* @return void
* @throws Exception
*/
private function deleteCollection(callable $getProjectDB, Document $document, Document $project): void
{
$collectionId = $document->getId();
$collectionInternalId = $document->getInternalId();
$databaseId = $document->getAttribute('databaseId');
$databaseInternalId = $document->getAttribute('databaseInternalId');
$dbForProject = $getProjectDB($project);
$relationships = \array_filter(
$document->getAttribute('attributes'),
fn ($attribute) => $attribute['type'] === Database::VAR_RELATIONSHIP
);
foreach ($relationships as $relationship) {
if (!$relationship['twoWay']) {
continue;
}
$relatedCollection = $dbForProject->getDocument('database_' . $databaseInternalId, $relationship['relatedCollection']);
$dbForProject->deleteDocument('attributes', $databaseInternalId . '_' . $relatedCollection->getInternalId() . '_' . $relationship['twoWayKey']);
$dbForProject->deleteCachedDocument('database_' . $databaseInternalId, $relatedCollection->getId());
$dbForProject->deleteCachedCollection('database_' . $databaseInternalId . '_collection_' . $relatedCollection->getInternalId());
}
$dbForProject->deleteCollection('database_' . $databaseInternalId . '_collection_' . $document->getInternalId());
$this->deleteByGroup('attributes', [
Query::equal('databaseInternalId', [$databaseInternalId]),
Query::equal('collectionInternalId', [$collectionInternalId])
], $dbForProject);
$this->deleteByGroup('indexes', [
Query::equal('databaseInternalId', [$databaseInternalId]),
Query::equal('collectionInternalId', [$collectionInternalId])
], $dbForProject);
$this->deleteAuditLogsByResource($getProjectDB, 'database/' . $databaseId . '/collection/' . $collectionId, $project);
}
2021-08-27 23:37:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
* @param callable $getProjectDB
2022-10-28 21:40:04 +13:00
* @param string $hourlyUsageRetentionDatetime
2023-10-02 06:39:26 +13:00
* @return void
* @throws Exception
2021-08-27 23:37:52 +12:00
*/
2023-12-13 00:26:44 +13:00
private function deleteUsageStats(Document $project, callable $getProjectDB, string $hourlyUsageRetentionDatetime): void
2021-09-01 21:13:23 +12:00
{
2023-12-13 00:26:44 +13:00
$dbForProject = $getProjectDB($project);
// Delete Usage stats
$this->deleteByGroup('stats_v2', [
2023-12-13 00:26:44 +13:00
Query::lessThan('time', $hourlyUsageRetentionDatetime),
Query::equal('period', ['1h']),
], $dbForProject);
2021-08-27 23:37:52 +12:00
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
2021-07-14 07:24:52 +12:00
* @param Document $document teams document
2022-08-13 19:57:04 +12:00
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2023-10-18 07:32:38 +13:00
private function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
2021-08-27 16:37:15 +12:00
{
2023-10-01 21:04:12 +13:00
$dbForProject = $getProjectDB($project);
2023-06-23 23:52:19 +12:00
$teamInternalId = $document->getInternalId();
// Delete Memberships
$this->deleteByGroup(
'memberships',
[
Query::equal('teamInternalId', [$teamInternalId])
],
$dbForProject,
function (Document $membership) use ($dbForProject) {
$userId = $membership->getAttribute('userId');
2023-12-15 02:32:06 +13:00
$dbForProject->purgeCachedDocument('users', $userId);
}
);
}
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
2023-10-01 21:04:12 +13:00
* @param Document $document
* @return void
2023-06-06 04:13:00 +12:00
* @throws Authorization
2023-10-01 21:04:12 +13:00
* @throws \Utopia\Database\Exception
* @throws Conflict
* @throws Restricted
* @throws Structure
*/
2024-02-21 03:10:51 +13:00
private function deleteProjectsByTeam(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
{
2023-10-25 20:39:59 +13:00
$projects = $dbForConsole->find('projects', [
Query::equal('teamInternalId', [$document->getInternalId()])
]);
foreach ($projects as $project) {
2024-02-21 03:10:51 +13:00
$this->deleteProject($dbForConsole, $getProjectDB, $deviceForFiles, $deviceForFunctions, $deviceForBuilds, $deviceForCache, $project);
$dbForConsole->deleteDocument('projects', $project->getId());
}
}
2021-07-14 07:24:52 +12:00
/**
2023-10-01 21:04:12 +13:00
* @param Database $dbForConsole
* @param callable $getProjectDB
2024-02-21 03:10:51 +13:00
* @param Device $deviceForFiles
* @param Device $deviceForFunctions
* @param Device $deviceForBuilds
* @param Device $deviceForCache
2023-10-01 21:04:12 +13:00
* @param Document $document
2023-10-02 06:39:26 +13:00
* @return void
* @throws Exception
2023-10-02 06:39:26 +13:00
* @throws Authorization
* @throws \Utopia\Database\Exception
2021-07-14 07:24:52 +12:00
*/
2024-02-21 03:10:51 +13:00
private function deleteProject(Database $dbForConsole, callable $getProjectDB, Device $deviceForFiles, Device $deviceForFunctions, Device $deviceForBuilds, Device $deviceForCache, Document $document): void
{
2021-07-14 06:44:45 +12:00
$projectId = $document->getId();
2023-04-28 03:37:14 +12:00
$projectInternalId = $document->getInternalId();
// Delete project tables
2023-06-11 22:29:04 +12:00
$dbForProject = $getProjectDB($document);
while (true) {
2023-01-17 17:31:14 +13:00
$collections = $dbForProject->listCollections();
if (empty($collections)) {
break;
}
foreach ($collections as $collection) {
$dbForProject->deleteCollection($collection->getId());
}
}
2023-04-28 03:37:14 +12:00
// Delete Platforms
$this->deleteByGroup('platforms', [
Query::equal('projectInternalId', [$projectInternalId])
], $dbForConsole);
// Delete project and function rules
$this->deleteByGroup('rules', [
2023-04-28 03:37:14 +12:00
Query::equal('projectInternalId', [$projectInternalId])
2023-10-18 07:32:38 +13:00
], $dbForConsole, function (Document $document) use ($dbForConsole) {
$this->deleteRule($dbForConsole, $document);
});
2023-04-28 03:37:14 +12:00
// Delete Keys
$this->deleteByGroup('keys', [
Query::equal('projectInternalId', [$projectInternalId])
], $dbForConsole);
// Delete Webhooks
$this->deleteByGroup('webhooks', [
Query::equal('projectInternalId', [$projectInternalId])
2023-04-28 03:37:14 +12:00
], $dbForConsole);
2023-11-03 22:24:51 +13:00
// Delete VCS Installations
$this->deleteByGroup('installations', [
Query::equal('projectInternalId', [$projectInternalId])
2023-11-03 22:24:51 +13:00
], $dbForConsole);
2023-11-09 10:59:29 +13:00
// Delete VCS Repositories
2023-11-03 22:24:51 +13:00
$this->deleteByGroup('repositories', [
2023-11-03 23:02:50 +13:00
Query::equal('projectInternalId', [$projectInternalId]),
2023-11-09 10:59:29 +13:00
], $dbForConsole);
// Delete VCS commments
$this->deleteByGroup('vcsComments', [
Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole);
// Delete metadata tables
try {
$dbForProject->deleteCollection('_metadata');
} catch (\Throwable) {
// Ignore: deleteCollection tries to delete a metadata entry after the collection is deleted,
// which will throw an exception here because the metadata collection is already deleted.
}
2021-07-14 06:44:45 +12:00
// Delete all storage directories
2024-02-21 03:10:51 +13:00
$deviceForFiles->delete($deviceForFiles->getRoot(), true);
$deviceForFunctions->delete($deviceForFunctions->getRoot(), true);
$deviceForBuilds->delete($deviceForBuilds->getRoot(), true);
$deviceForCache->delete($deviceForCache->getRoot(), true);
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
2021-07-14 07:24:52 +12:00
* @param Document $document user document
2022-08-13 19:57:04 +12:00
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2023-10-18 07:32:38 +13:00
private function deleteUser(callable $getProjectDB, Document $document, Document $project): void
{
2021-07-13 09:57:37 +12:00
$userId = $document->getId();
2023-06-23 23:52:19 +12:00
$userInternalId = $document->getInternalId();
2023-06-06 04:13:00 +12:00
$dbForProject = $getProjectDB($project);
2022-11-17 08:39:35 +13:00
// Delete all sessions of this user from the sessions table and update the sessions field of the user record
$this->deleteByGroup('sessions', [
2023-06-23 23:52:19 +12:00
Query::equal('userInternalId', [$userInternalId])
2022-11-17 08:39:35 +13:00
], $dbForProject);
2023-12-15 02:32:06 +13:00
$dbForProject->purgeCachedDocument('users', $userId);
// Delete Memberships and decrement team membership counts
2021-07-14 07:24:52 +12:00
$this->deleteByGroup('memberships', [
2023-06-23 23:52:19 +12:00
Query::equal('userInternalId', [$userInternalId])
2022-11-17 08:39:35 +13:00
], $dbForProject, function (Document $document) use ($dbForProject) {
if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId');
2022-11-17 08:39:35 +13:00
$team = $dbForProject->getDocument('teams', $teamId);
2021-09-01 21:13:23 +12:00
if (!$team->isEmpty()) {
2024-02-12 14:18:19 +13:00
$dbForProject->decreaseDocumentAttribute('teams', $teamId, 'total', 1, 0);
}
}
});
2022-04-27 23:06:53 +12:00
// Delete tokens
$this->deleteByGroup('tokens', [
2023-06-23 23:52:19 +12:00
Query::equal('userInternalId', [$userInternalId])
2022-11-17 08:39:35 +13:00
], $dbForProject);
// Delete identities
$this->deleteByGroup('identities', [
Query::equal('userInternalId', [$userInternalId])
], $dbForProject);
// Delete targets
$this->deleteByGroup(
'targets',
[
Query::equal('userInternalId', [$userInternalId])
],
$dbForProject,
function (Document $target) use ($getProjectDB, $project) {
$this->deleteTargetSubscribers($project, $getProjectDB, $target);
}
);
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param database $dbForConsole
* @param callable $getProjectDB
2022-07-12 03:12:41 +12:00
* @param string $datetime
2023-10-02 06:39:26 +13:00
* @return void
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2023-12-21 00:26:57 +13:00
private function deleteExecutionLogs(Document $project, callable $getProjectDB, string $datetime): void
2020-12-15 10:26:37 +13:00
{
2023-12-13 00:26:44 +13:00
$dbForProject = $getProjectDB($project);
// Delete Executions
$this->deleteByGroup('executions', [
Query::lessThan('$createdAt', $datetime)
], $dbForProject);
2020-12-15 10:26:37 +13:00
}
2023-06-06 04:13:00 +12:00
/**
* @param Database $dbForConsole
* @param callable $getProjectDB
* @return void
2023-10-01 21:04:12 +13:00
* @throws Exception|Throwable
2023-06-06 04:13:00 +12:00
*/
2023-12-13 00:26:44 +13:00
private function deleteExpiredSessions(Document $project, callable $getProjectDB): void
{
2023-12-13 00:26:44 +13:00
$dbForProject = $getProjectDB($project);
$duration = $project->getAttribute('auths', [])['duration'] ?? Auth::TOKEN_EXPIRATION_LOGIN_LONG;
$expired = DateTime::addSeconds(new \DateTime(), -1 * $duration);
2022-12-14 21:44:40 +13:00
2023-12-13 00:26:44 +13:00
// Delete Sessions
$this->deleteByGroup('sessions', [
Query::lessThan('$createdAt', $expired)
], $dbForProject);
}
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
2022-07-12 03:12:41 +12:00
* @param string $datetime
2023-10-02 06:39:26 +13:00
* @return void
* @throws Exception
*/
2023-10-18 07:32:38 +13:00
private function deleteRealtimeUsage(Database $dbForConsole, string $datetime): void
{
2023-10-18 07:32:38 +13:00
// Delete Dead Realtime Logs
$this->deleteByGroup('realtime', [
Query::lessThan('timestamp', $datetime)
], $dbForConsole);
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
* @param callable $getProjectDB
2022-07-12 03:12:41 +12:00
* @param string $datetime
2023-10-02 06:39:26 +13:00
* @return void
2022-07-12 03:12:41 +12:00
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2023-12-13 00:26:44 +13:00
private function deleteAbuseLogs(Document $project, callable $getProjectDB, string $abuseRetention): void
{
2023-12-13 00:26:44 +13:00
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
$timeLimit = new TimeLimit("", 0, 1, $dbForProject);
$abuse = new Abuse($timeLimit);
$status = $abuse->cleanup($abuseRetention);
if (!$status) {
throw new Exception('Failed to delete Abuse logs for project ' . $projectId);
2020-12-19 03:05:15 +13:00
}
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
* @param callable $getProjectDB
2022-07-12 03:12:41 +12:00
* @param string $datetime
2023-10-02 06:39:26 +13:00
* @return void
2022-07-12 03:12:41 +12:00
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2023-12-13 00:26:44 +13:00
private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
{
2023-12-13 00:26:44 +13:00
$projectId = $project->getId();
$dbForProject = $getProjectDB($project);
$audit = new Audit($dbForProject);
$status = $audit->cleanup($auditRetention);
if (!$status) {
throw new Exception('Failed to delete Audit logs for project' . $projectId);
2020-12-19 03:05:15 +13:00
}
}
/**
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
2022-07-12 03:12:41 +12:00
* @param string $resource
2022-08-13 19:57:04 +12:00
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
*/
2023-10-18 07:32:38 +13:00
private function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void
{
2023-06-06 04:13:00 +12:00
$dbForProject = $getProjectDB($project);
$this->deleteByGroup(Audit::COLLECTION, [
2022-08-12 11:53:52 +12:00
Query::equal('resource', [$resource])
], $dbForProject);
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
2024-02-21 03:10:51 +13:00
* @param Device $deviceForFunctions
* @param Device $deviceForBuilds
2021-07-14 07:24:52 +12:00
* @param Document $document function document
2022-08-13 19:57:04 +12:00
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
2021-07-14 07:24:52 +12:00
*/
2024-02-21 03:10:51 +13:00
private function deleteFunction(Database $dbForConsole, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
{
2022-08-13 19:57:04 +12:00
$projectId = $project->getId();
2023-06-06 04:13:00 +12:00
$dbForProject = $getProjectDB($project);
2022-02-13 11:34:16 +13:00
$functionId = $document->getId();
2023-06-23 23:52:19 +12:00
$functionInternalId = $document->getInternalId();
2022-01-28 13:25:28 +13:00
2023-02-23 04:07:34 +13:00
/**
2023-07-31 18:47:47 +12:00
* Delete rules
2023-02-23 04:07:34 +13:00
*/
2023-07-31 18:47:47 +12:00
Console::info("Deleting rules for function " . $functionId);
2023-03-09 07:30:01 +13:00
$this->deleteByGroup('rules', [
2023-02-23 04:07:34 +13:00
Query::equal('resourceType', ['function']),
2023-06-23 23:52:19 +12:00
Query::equal('resourceInternalId', [$functionInternalId]),
2023-02-23 04:07:34 +13:00
Query::equal('projectInternalId', [$project->getInternalId()])
2023-10-01 21:04:12 +13:00
], $dbForConsole, function (Document $document) use ($project, $dbForConsole) {
2023-10-18 07:32:38 +13:00
$this->deleteRule($dbForConsole, $document);
2023-03-11 01:20:24 +13:00
});
2023-02-23 04:07:34 +13:00
2022-09-19 23:53:34 +12:00
/**
* Delete Variables
*/
Console::info("Deleting variables for function " . $functionId);
$this->deleteByGroup('variables', [
2023-03-12 05:06:02 +13:00
Query::equal('resourceType', ['function']),
2023-06-23 23:52:19 +12:00
Query::equal('resourceInternalId', [$functionInternalId])
2022-09-19 23:53:34 +12:00
], $dbForProject);
/**
* Delete Deployments
*/
2022-02-13 11:34:16 +13:00
Console::info("Deleting deployments for function " . $functionId);
2024-02-21 00:40:55 +13:00
2023-06-23 23:52:19 +12:00
$deploymentInternalIds = [];
$this->deleteByGroup('deployments', [
2023-06-23 23:52:19 +12:00
Query::equal('resourceInternalId', [$functionInternalId])
2024-02-21 03:10:51 +13:00
], $dbForProject, function (Document $document) use ($deviceForFunctions, &$deploymentInternalIds) {
2023-06-23 23:52:19 +12:00
$deploymentInternalIds[] = $document->getInternalId();
2024-02-21 03:10:51 +13:00
$this->deleteDeploymentFiles($deviceForFunctions, $document);
});
/**
* Delete builds
*/
2022-02-13 11:34:16 +13:00
Console::info("Deleting builds for function " . $functionId);
2024-02-21 00:40:55 +13:00
2023-06-23 23:52:19 +12:00
foreach ($deploymentInternalIds as $deploymentInternalId) {
2022-01-29 13:52:06 +13:00
$this->deleteByGroup('builds', [
2023-06-23 23:52:19 +12:00
Query::equal('deploymentInternalId', [$deploymentInternalId])
2024-02-21 03:10:51 +13:00
], $dbForProject, function (Document $document) use ($deviceForBuilds) {
$this->deleteBuildFiles($deviceForBuilds, $document);
2022-01-29 13:52:06 +13:00
});
}
2022-05-24 02:54:50 +12:00
/**
2022-02-13 11:34:16 +13:00
* Delete Executions
2022-05-16 21:58:17 +12:00
*/
2022-02-13 11:34:16 +13:00
Console::info("Deleting executions for function " . $functionId);
$this->deleteByGroup('executions', [
2023-06-23 23:52:19 +12:00
Query::equal('functionInternalId', [$functionInternalId])
], $dbForProject);
/**
* Delete VCS Repositories and VCS Comments
*/
Console::info("Deleting VCS repositories and comments linked to function " . $functionId);
$this->deleteByGroup('repositories', [
2023-11-15 00:38:09 +13:00
Query::equal('projectInternalId', [$project->getInternalId()]),
Query::equal('resourceInternalId', [$functionInternalId]),
Query::equal('resourceType', ['function']),
], $dbForConsole, function (Document $document) use ($dbForConsole) {
$providerRepositoryId = $document->getAttribute('providerRepositoryId', '');
2023-11-15 02:11:54 +13:00
$projectInternalId = $document->getAttribute('projectInternalId', '');
$this->deleteByGroup('vcsComments', [
Query::equal('providerRepositoryId', [$providerRepositoryId]),
2023-11-15 02:11:54 +13:00
Query::equal('projectInternalId', [$projectInternalId]),
], $dbForConsole);
});
2023-03-15 00:13:03 +13:00
/**
* Request executor to delete all deployment containers
*/
Console::info("Requesting executor to delete all deployment containers for function " . $functionId);
2023-10-03 03:02:48 +13:00
$this->deleteRuntimes($getProjectDB, $document, $project);
}
2023-10-18 07:32:38 +13:00
/**
* @param Device $device
* @param Document $deployment
* @return void
*/
private function deleteDeploymentFiles(Device $device, Document $deployment): void
{
$deploymentId = $deployment->getId();
$deploymentPath = $deployment->getAttribute('path', '');
if (empty($deploymentPath)) {
Console::info("No deployment files for deployment " . $deploymentId);
return;
}
Console::info("Deleting deployment files for deployment " . $deploymentId);
try {
if ($device->delete($deploymentPath, true)) {
Console::success('Deleted deployment files: ' . $deploymentPath);
} else {
Console::error('Failed to delete deployment files: ' . $deploymentPath);
}
} catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $deploymentPath);
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());
}
}
2023-10-18 07:32:38 +13:00
/**
* @param Device $device
* @param Document $build
* @return void
*/
private function deleteBuildFiles(Device $device, Document $build): void
{
$buildId = $build->getId();
$buildPath = $build->getAttribute('path', '');
if (empty($buildPath)) {
Console::info("No build files for build " . $buildId);
return;
}
try {
if ($device->delete($buildPath, true)) {
Console::success('Deleted build files: ' . $buildPath);
} else {
Console::error('Failed to delete build files: ' . $buildPath);
}
} catch (\Throwable $th) {
Console::error('Failed to delete deployment files: ' . $buildPath);
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());
}
}
/**
2023-06-06 04:13:00 +12:00
* @param callable $getProjectDB
2024-02-21 03:10:51 +13:00
* @param Device $deviceForFunctions
* @param Device $deviceForBuilds
2023-10-01 21:04:12 +13:00
* @param Document $document
2022-08-13 19:57:04 +12:00
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
*/
2024-02-21 03:10:51 +13:00
private function deleteDeployment(callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, Document $document, Document $project): void
{
2022-08-13 19:57:04 +12:00
$projectId = $project->getId();
2023-06-06 04:13:00 +12:00
$dbForProject = $getProjectDB($project);
$deploymentId = $document->getId();
2023-06-23 23:52:19 +12:00
$deploymentInternalId = $document->getInternalId();
/**
2022-01-29 13:00:25 +13:00
* Delete deployment files
*/
2024-02-21 03:10:51 +13:00
$this->deleteDeploymentFiles($deviceForFunctions, $document);
/**
* Delete builds
*/
2022-02-13 11:34:16 +13:00
Console::info("Deleting builds for deployment " . $deploymentId);
2024-02-21 00:40:55 +13:00
$this->deleteByGroup('builds', [
2023-06-23 23:52:19 +12:00
Query::equal('deploymentInternalId', [$deploymentInternalId])
2024-02-21 03:10:51 +13:00
], $dbForProject, function (Document $document) use ($deviceForBuilds) {
$this->deleteBuildFiles($deviceForBuilds, $document);
});
2023-03-15 00:13:03 +13:00
/**
* Request executor to delete all deployment containers
*/
Console::info("Requesting executor to delete deployment container for deployment " . $deploymentId);
2023-10-02 06:39:26 +13:00
$this->deleteRuntimes($getProjectDB, $document, $project);
}
2021-07-14 07:24:52 +12:00
/**
* @param Document $document to be deleted
* @param Database $database to delete it from
* @param callable|null $callback to perform after document is deleted
2023-10-18 07:32:38 +13:00
* @return void
2021-07-14 07:24:52 +12:00
*/
2023-10-18 07:32:38 +13:00
private function deleteById(Document $document, Database $database, callable $callback = null): void
{
if ($database->deleteDocument($document->getCollection(), $document->getId())) {
2021-09-01 21:13:23 +12:00
Console::success('Deleted document "' . $document->getId() . '" successfully');
2021-09-01 21:13:23 +12:00
if (is_callable($callback)) {
$callback($document);
}
2021-09-01 21:13:23 +12:00
} else {
Console::error('Failed to delete document: ' . $document->getId());
}
}
/**
* @param string $collection collectionID
* @param array $queries
* @param Database $database
* @param callable|null $callback
2023-10-02 06:39:26 +13:00
* @return void
* @throws Exception
*/
2023-10-18 07:32:38 +13:00
private function deleteByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
{
$count = 0;
$chunk = 0;
$limit = 50;
$sum = $limit;
$executionStart = \microtime(true);
2023-06-06 04:13:00 +12:00
while ($sum === $limit) {
$chunk++;
2023-06-06 04:13:00 +12:00
$results = $database->find($collection, \array_merge([Query::limit($limit)], $queries));
2023-06-06 04:13:00 +12:00
$sum = count($results);
2023-06-06 04:13:00 +12:00
Console::info('Deleting chunk #' . $chunk . '. Found ' . $sum . ' documents');
2023-06-06 04:13:00 +12:00
foreach ($results as $document) {
$this->deleteById($document, $database, $callback);
$count++;
}
}
$executionEnd = \microtime(true);
Console::info("Deleted {$count} document by group in " . ($executionEnd - $executionStart) . " seconds");
}
2021-07-13 09:57:37 +12:00
/**
* @param string $collection collectionID
* @param Query[] $queries
2021-07-14 07:24:52 +12:00
* @param Database $database
2023-06-06 04:13:00 +12:00
* @param callable|null $callback
2023-10-02 06:39:26 +13:00
* @return void
2023-06-06 04:13:00 +12:00
* @throws Exception
2021-07-13 09:57:37 +12:00
*/
2023-10-18 07:32:38 +13:00
private function listByGroup(string $collection, array $queries, Database $database, callable $callback = null): void
{
$count = 0;
$chunk = 0;
$limit = 50;
$results = [];
$sum = $limit;
$cursor = null;
$executionStart = \microtime(true);
2021-09-01 21:13:23 +12:00
while ($sum === $limit) {
$chunk++;
$mergedQueries = \array_merge([Query::limit($limit)], $queries);
if ($cursor instanceof Document) {
$mergedQueries[] = Query::cursorAfter($cursor);
}
$results = $database->find($collection, $mergedQueries);
2022-01-29 13:52:06 +13:00
$sum = count($results);
if ($sum > 0) {
$cursor = $results[$sum - 1];
}
foreach ($results as $document) {
2022-12-14 21:44:40 +13:00
if (is_callable($callback)) {
$callback($document);
}
$count++;
}
}
$executionEnd = \microtime(true);
2022-12-14 21:44:40 +13:00
Console::info("Listed {$count} document by group in " . ($executionEnd - $executionStart) . " seconds");
}
2021-07-14 07:24:52 +12:00
/**
2023-06-06 04:13:00 +12:00
* @param Database $dbForConsole
2023-03-10 20:42:52 +13:00
* @param Document $document rule document
2023-10-02 06:39:26 +13:00
* @return void
2021-07-14 07:24:52 +12:00
*/
2023-10-18 07:32:38 +13:00
private function deleteRule(Database $dbForConsole, Document $document): void
2021-02-05 22:05:26 +13:00
{
2021-02-05 23:57:43 +13:00
$domain = $document->getAttribute('domain');
2021-02-05 22:05:26 +13:00
$directory = APP_STORAGE_CERTIFICATES . '/' . $domain;
2021-02-06 00:18:12 +13:00
$checkTraversal = realpath($directory) === $directory;
2021-02-05 22:05:26 +13:00
2023-03-10 20:42:52 +13:00
if ($checkTraversal && is_dir($directory)) {
// Delete files, so Traefik is aware of change
2021-09-01 21:13:23 +12:00
array_map('unlink', glob($directory . '/*.*'));
2021-02-05 22:05:26 +13:00
rmdir($directory);
2021-02-05 23:57:43 +13:00
Console::info("Deleted certificate files for {$domain}");
} else {
Console::info("No certificate files found for {$domain}");
2021-02-05 22:05:26 +13:00
}
2023-03-10 20:42:52 +13:00
// Delete certificate document, so Appwrite is aware of change
if (isset($document['certificateId'])) {
2023-10-01 21:04:12 +13:00
$dbForConsole->deleteDocument('certificates', $document['certificateId']);
2023-03-10 20:42:52 +13:00
}
2021-02-05 22:05:26 +13:00
}
2021-07-18 19:03:48 +12:00
2023-06-06 04:13:00 +12:00
/**
* @param callable $getProjectDB
2024-02-21 03:10:51 +13:00
* @param Device $deviceForFiles
2023-06-06 04:13:00 +12:00
* @param Document $document
* @param Document $project
* @return void
*/
2024-02-21 03:10:51 +13:00
private function deleteBucket(callable $getProjectDB, Device $deviceForFiles, Document $document, Document $project): void
2021-07-18 19:03:48 +12:00
{
2023-06-06 04:13:00 +12:00
$dbForProject = $getProjectDB($project);
2022-02-18 14:36:25 +13:00
$dbForProject->deleteCollection('bucket_' . $document->getInternalId());
2021-07-28 22:14:47 +12:00
2024-02-21 03:10:51 +13:00
$deviceForFiles->deletePath($document->getId());
2021-07-18 19:03:48 +12:00
}
2023-03-15 00:13:03 +13:00
2023-10-01 21:04:12 +13:00
/**
* @param Database $dbForConsole
* @param callable $getProjectDB
* @param Document $document
* @param Document $project
* @return void
2023-10-02 06:39:26 +13:00
* @throws Exception
2023-10-01 21:04:12 +13:00
*/
2023-10-18 07:32:38 +13:00
private function deleteInstallation(Database $dbForConsole, callable $getProjectDB, Document $document, Document $project): void
{
2023-10-01 21:04:12 +13:00
$dbForProject = $getProjectDB($project);
$this->listByGroup('functions', [
Query::equal('installationInternalId', [$document->getInternalId()])
], $dbForProject, function ($function) use ($dbForProject, $dbForConsole) {
2023-07-31 07:10:25 +12:00
$dbForConsole->deleteDocument('repositories', $function->getAttribute('repositoryId'));
$function = $function
->setAttribute('installationId', '')
->setAttribute('installationInternalId', '')
->setAttribute('providerRepositoryId', '')
->setAttribute('providerBranch', '')
->setAttribute('providerSilentMode', false)
->setAttribute('providerRootDirectory', '')
->setAttribute('repositoryId', '')
->setAttribute('repositoryInternalId', '');
$dbForProject->updateDocument('functions', $function->getId(), $function);
});
}
2023-10-01 21:04:12 +13:00
/**
* @param callable $getProjectDB
* @param ?Document $function
* @param Document $project
2023-10-02 06:39:26 +13:00
* @return void
2023-10-01 21:04:12 +13:00
* @throws Exception
*/
2023-10-18 07:32:38 +13:00
private function deleteRuntimes(callable $getProjectDB, ?Document $function, Document $project): void
2023-03-15 08:31:23 +13:00
{
2023-03-15 00:13:03 +13:00
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
2023-10-01 21:04:12 +13:00
$deleteByFunction = function (Document $function) use ($getProjectDB, $project, $executor) {
2023-03-15 00:13:03 +13:00
$this->listByGroup(
'deployments',
[
Query::equal('resourceInternalId', [$function->getInternalId()]),
Query::equal('resourceType', ['functions']),
],
2023-10-01 21:04:12 +13:00
$getProjectDB($project),
2023-03-15 00:13:03 +13:00
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());
}
}
);
};
2023-03-15 08:31:23 +13:00
if ($function !== null) {
2023-03-15 00:13:03 +13:00
// Delete function runtimes
$deleteByFunction($function);
} else {
// Delete all project runtimes
$this->listByGroup(
'functions',
[],
2023-10-01 21:04:12 +13:00
$getProjectDB($project),
2023-03-15 00:13:03 +13:00
function (Document $function) use ($deleteByFunction) {
$deleteByFunction($function);
}
);
}
}
2021-09-01 21:13:23 +12:00
}