From 13066c3dde6ac525743bf6c166a881a91f73ef9c Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 13 Jun 2023 14:11:23 +0300 Subject: [PATCH] cache deletion bug fix --- app/controllers/api/storage.php | 10 +++--- app/workers/deletes.php | 64 ++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index d59d950d93..7d64f752f3 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -654,11 +654,6 @@ App::post('/v1/storage/buckets/:bucketId/files') ->setContext('bucket', $bucket) ; - $deletes - ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) - ->setResource('file/' . $file->getId()) - ; - $metadata = null; // was causing leaks as it was passed by reference $response @@ -1341,6 +1336,11 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId') $file = Authorization::skip(fn() => $dbForProject->updateDocument('bucket_' . $bucket->getInternalId(), $fileId, $file)); } + $deletes + ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) + ->setResource('file/' . $file->getId()) + ; + $events ->setParam('bucketId', $bucket->getId()) ->setParam('fileId', $file->getId()) diff --git a/app/workers/deletes.php b/app/workers/deletes.php index f27bc4feb9..e2a314ee98 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -114,10 +114,10 @@ class DeletesV1 extends Worker break; case DELETE_TYPE_CACHE_BY_RESOURCE: - $this->deleteCacheByResource($project->getId()); + $this->deleteCacheByResource($project, $this->args['resource']); break; case DELETE_TYPE_CACHE_BY_TIMESTAMP: - $this->deleteCacheByDate(); + $this->deleteCacheByDate($this->args['datetime']); break; default: Console::error('No delete operation for type: ' . $type); @@ -130,31 +130,55 @@ class DeletesV1 extends Worker } /** - * @param string $projectId + * @param Document $project + * @param string $resource + * @throws Exception */ - protected function deleteCacheByResource(string $projectId): void + protected function deleteCacheByResource(Document $project, string $resource): void { - $this->deleteCacheFiles([ - Query::equal('resource', [$this->args['resource']]), - ]); - } + $dbForProject = $this->getProjectDB($project); + $projectId = $project->getId(); + $document = $dbForProject->findOne('cache', [Query::equal('resource', [$resource])]); - protected function deleteCacheByDate(): void - { - $this->deleteCacheFiles([ - Query::lessThan('accessedAt', $this->args['datetime']), - ]); - } - - protected function deleteCacheFiles($query): void - { - $this->deleteForProjectIds(function (string $projectId) use ($query) { - - $dbForProject = $this->getProjectDB($projectId); + 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); + } + } + ); + } + } + + /** + * @param string $datetime + * @throws Exception + */ + protected function deleteCacheByDate(string $datetime): void + { + $this->deleteForProjectIds(function (Document $project) use ($datetime) { + + $projectId = $project->getId(); + $dbForProject = $this->getProjectDB($project); + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId) + ); + + $query = [ + Query::lessThan('accessedAt', $datetime), + ]; + $this->deleteByGroup( 'cache', $query,