1
0
Fork 0
mirror of synced 2024-06-14 08:44:49 +12:00

Migrated cache buster to a script

This commit is contained in:
Matej Bačo 2023-05-02 13:21:08 +02:00
parent 1409ea4dbf
commit ecbc9aecb3
6 changed files with 68 additions and 42 deletions

View file

@ -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 && \

View file

@ -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')

View file

@ -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
*/

3
bin/clear-card-cache Normal file
View file

@ -0,0 +1,3 @@
#!/bin/sh
php /usr/src/code/app/cli.php clear-card-cache $@

View file

@ -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())

View file

@ -0,0 +1,62 @@
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
use Utopia\Platform\Action;
use Utopia\CLI\Console;
use Utopia\Database\Query;
use Utopia\Database\Database;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\UID;
class ClearCardCache extends Action
{
public static function getName(): string
{
return 'clear-card-cache';
}
public function __construct()
{
$this
->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');
}
}