From ecbc9aecb3fb807e6ccb7e6a99884db9a6f3d3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 2 May 2023 13:21:08 +0200 Subject: [PATCH] Migrated cache buster to a script --- Dockerfile | 1 + app/controllers/api/account.php | 6 -- app/controllers/shared/api.php | 36 ----------- bin/clear-card-cache | 3 + src/Appwrite/Platform/Services/Tasks.php | 2 + .../Platform/Tasks/ClearCardCache.php | 62 +++++++++++++++++++ 6 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 bin/clear-card-cache create mode 100644 src/Appwrite/Platform/Tasks/ClearCardCache.php diff --git a/Dockerfile b/Dockerfile index 37a65d6054..bf3f4f1634 100755 --- a/Dockerfile +++ b/Dockerfile @@ -118,6 +118,7 @@ RUN mkdir -p /storage/uploads && \ # Executables RUN chmod +x /usr/local/bin/doctor && \ chmod +x /usr/local/bin/patch-delete-schedule-updated-at-attribute && \ + chmod +x /usr/local/bin/clear-card-cache && \ chmod +x /usr/local/bin/maintenance && \ chmod +x /usr/local/bin/volume-sync && \ chmod +x /usr/local/bin/usage && \ diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index c3c75dd7ae..d5d9539a5f 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -473,8 +473,6 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect') ->label('docs', false) ->label('usage.metric', 'sessions.{scope}.requests.create') ->label('usage.params', ['provider:{request.provider}']) - ->label('cacheBuster', true) - ->label('cacheBuster.resource', ['card-cloud/{user.$id}', 'card-cloud-back/{user.$id}', 'card-cloud-og/{user.$id}']) ->param('provider', '', new WhiteList(\array_keys(Config::getParam('providers')), true), 'OAuth2 provider.') ->param('code', '', new Text(2048), 'OAuth2 code.') ->param('state', '', new Text(2048), 'OAuth2 state params.', true) @@ -1570,8 +1568,6 @@ App::patch('/v1/account/name') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_ACCOUNT) - ->label('cacheBuster', true) - ->label('cacheBuster.resource', ['card-cloud/{user.$id}', 'card-cloud-back/{user.$id}', 'card-cloud-og/{user.$id}']) ->param('name', '', new Text(128), 'User name. Max length: 128 chars.') ->inject('response') ->inject('user') @@ -1643,8 +1639,6 @@ App::patch('/v1/account/email') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_ACCOUNT) - ->label('cacheBuster', true) - ->label('cacheBuster.resource', ['card-cloud/{user.$id}', 'card-cloud-back/{user.$id}', 'card-cloud-og/{user.$id}']) ->param('email', '', new Email(), 'User email.') ->param('password', '', new Password(), 'User password. Must be at least 8 chars.') ->inject('response') diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index de641328c6..8a2dde5963 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -438,42 +438,6 @@ App::shutdown() $database->trigger(); } - /** - * Cache Buster - */ - $bustCache = $route->getLabel('cacheBuster', false); - if ($bustCache) { - $payload = $response->getPayload(); - - if (!empty($payload)) { - $resources = []; - - $patterns = $route->getLabel('cacheBuster.resource', []); - if (!empty($patterns)) { - foreach ($patterns as $pattern) { - $resources[] = $parseLabel($pattern, $responsePayload, $requestParams, $user); - } - } - - $caches = Authorization::skip(fn () => $dbForProject->find('cache', [ - Query::equal('resource', $resources), - Query::limit(100) - ])); - - foreach ($caches as $cache) { - $key = $cache->getId(); - - $cache = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) - ); - - $cache->purge($key); - - Authorization::skip(fn () => $dbForProject->deleteDocument('cache', $cache->getId())); - } - } - } - /** * Cache label */ diff --git a/bin/clear-card-cache b/bin/clear-card-cache new file mode 100644 index 0000000000..b39bc13651 --- /dev/null +++ b/bin/clear-card-cache @@ -0,0 +1,3 @@ +#!/bin/sh + +php /usr/src/code/app/cli.php clear-card-cache $@ \ No newline at end of file diff --git a/src/Appwrite/Platform/Services/Tasks.php b/src/Appwrite/Platform/Services/Tasks.php index a0c5d3a547..b82dca422f 100644 --- a/src/Appwrite/Platform/Services/Tasks.php +++ b/src/Appwrite/Platform/Services/Tasks.php @@ -14,6 +14,7 @@ use Appwrite\Platform\Tasks\Specs; use Appwrite\Platform\Tasks\SSL; use Appwrite\Platform\Tasks\Hamster; use Appwrite\Platform\Tasks\PatchDeleteScheduleUpdatedAtAttribute; +use Appwrite\Platform\Tasks\ClearCardCache; use Appwrite\Platform\Tasks\Usage; use Appwrite\Platform\Tasks\Vars; use Appwrite\Platform\Tasks\Version; @@ -34,6 +35,7 @@ class Tasks extends Service ->addAction(Install::getName(), new Install()) ->addAction(Maintenance::getName(), new Maintenance()) ->addAction(PatchCreateMissingSchedules::getName(), new PatchCreateMissingSchedules()) + ->addAction(ClearCardCache::getName(), new ClearCardCache()) ->addAction(PatchDeleteScheduleUpdatedAtAttribute::getName(), new PatchDeleteScheduleUpdatedAtAttribute()) ->addAction(Schedule::getName(), new Schedule()) ->addAction(Migrate::getName(), new Migrate()) diff --git a/src/Appwrite/Platform/Tasks/ClearCardCache.php b/src/Appwrite/Platform/Tasks/ClearCardCache.php new file mode 100644 index 0000000000..7bee0c1a98 --- /dev/null +++ b/src/Appwrite/Platform/Tasks/ClearCardCache.php @@ -0,0 +1,62 @@ +desc('Deletes card cache for specific user') + ->param('userId', '', new UID(), 'User UID.', false) + ->inject('dbForConsole') + ->callback(fn (string $userId, Database $dbForConsole) => $this->action($userId, $dbForConsole)); + } + + public function action(string $userId, Database $dbForConsole): void + { + Authorization::disable(); + Authorization::setDefaultStatus(false); + + Console::title('ClearCardCache V1'); + Console::success(APP_NAME . ' ClearCardCache v1 has started'); + $resources = ['card-cloud/' . $userId, 'card-cloud-back/' . $userId, 'card-cloud-og/' . $userId]; + + $caches = Authorization::skip(fn () => $dbForConsole->find('cache', [ + Query::equal('resource', $resources), + Query::limit(100) + ])); + + $count = \count($caches); + Console::info("Going to delete {$count} cache records in 5 seconds..."); + \sleep(5); + + foreach ($caches as $cache) { + $key = $cache->getId(); + + $cacheFolder = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-console') + ); + + $cacheFolder->purge($key); + + Authorization::skip(fn () => $dbForConsole->deleteDocument('cache', $cache->getId())); + } + + Console::success(APP_NAME . ' ClearCardCache v1 has finished'); + } +}