From 6cab4365b044e871e5b9f3e40c69f530710ffa80 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 24 Oct 2022 15:12:18 +0200 Subject: [PATCH] feat: remove cache of old repositories --- src/Repository.ts | 13 +++++++++---- src/schedule.ts | 11 +++++++++++ src/source/GitHubDownload.ts | 3 +++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Repository.ts b/src/Repository.ts index 2728a3d..6110e45 100644 --- a/src/Repository.ts +++ b/src/Repository.ts @@ -253,10 +253,15 @@ export default class Repository { this._model.size = { storage: 0, file: 0 }; this._model.originalFiles = null; // remove cache - return Promise.all([ - this._model.save(), - storage.rm(this._model.repoId + "/"), - ]); + return Promise.all([this._model.save(), this.removeCache()]); + } + + /** + * Remove the cached files + * @returns + */ + async removeCache() { + return storage.rm(this._model.repoId + "/"); } /** diff --git a/src/schedule.ts b/src/schedule.ts index 8c2f3b1..595e8bb 100644 --- a/src/schedule.ts +++ b/src/schedule.ts @@ -25,6 +25,7 @@ export function conferenceStatusCheck() { export function repositoryStatusCheck() { // check every 6 hours the status of the repositories const job = schedule.scheduleJob("0 */6 * * *", async () => { + console.log("[schedule] Check repository status and unused repositories"); ( await AnonymizedRepositoryModel.find({ status: { $eq: "ready" } }) ).forEach((data) => { @@ -34,6 +35,16 @@ export function repositoryStatusCheck() { } catch (error) { console.log(`Repository ${repo.repoId} is expired`); } + const sixMonthAgo = new Date(); + sixMonthAgo.setMonth(sixMonthAgo.getMonth() - 6); + + if (repo.model.lastView < sixMonthAgo) { + repo.removeCache().then(() => { + console.log( + `Repository ${repo.repoId} not visited for 6 months remove the cached files` + ); + }); + } }); }); } diff --git a/src/source/GitHubDownload.ts b/src/source/GitHubDownload.ts index 4395c38..773403b 100644 --- a/src/source/GitHubDownload.ts +++ b/src/source/GitHubDownload.ts @@ -114,6 +114,9 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { } async getFileContent(file: AnonymizedFile): Promise { + if (await storage.exists(file.originalCachePath)) { + return storage.read(file.originalCachePath); + } await this.download(); // update the file list await this.repository.files({ force: true });