feat: add a flag to know if a repo has been reseted

This commit is contained in:
tdurieux
2023-04-03 10:21:56 +02:00
parent ef1a2bfa4a
commit 344ecf2a33
6 changed files with 19 additions and 6 deletions

View File

@@ -318,6 +318,8 @@ export default class Repository {
* @returns
*/
async removeCache() {
this.model.isReseted = true;
await this.model.save();
if (await storage.exists(this._model.repoId + "/")) {
return storage.rm(this._model.repoId + "/");
}

View File

@@ -63,6 +63,10 @@ const AnonymizedRepositorySchema = new Schema({
default: 0,
},
},
isReseted: {
type: Boolean,
default: false,
},
});
export default AnonymizedRepositorySchema;

View File

@@ -40,6 +40,7 @@ export interface IAnonymizedRepository {
storage: number;
file: number;
};
isReseted: boolean;
}
export interface IAnonymizedRepositoryDocument

View File

@@ -27,7 +27,10 @@ export function repositoryStatusCheck() {
const job = schedule.scheduleJob("0 */6 * * *", async () => {
console.log("[schedule] Check repository status and unused repositories");
(
await AnonymizedRepositoryModel.find({ status: { $eq: "ready" } })
await AnonymizedRepositoryModel.find({
status: { $eq: "ready" },
isReseted: { $eq: false },
})
).forEach((data) => {
const repo = new Repository(data);
try {
@@ -35,13 +38,13 @@ export function repositoryStatusCheck() {
} catch (error) {
console.log(`Repository ${repo.repoId} is expired`);
}
const sixMonthAgo = new Date();
sixMonthAgo.setMonth(sixMonthAgo.getMonth() - 6);
const fourMonthAgo = new Date();
fourMonthAgo.setMonth(fourMonthAgo.getMonth() - 4);
if (repo.model.lastView < sixMonthAgo) {
if (repo.model.lastView < fourMonthAgo) {
repo.removeCache().then(() => {
console.log(
`Repository ${repo.repoId} not visited for 6 months remove the cached files`
`Repository ${repo.repoId} not visited for 4 months remove the cached files`
);
});
}

View File

@@ -122,6 +122,7 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
clearTimeout(progressTimeout);
}
this.repository.model.isReseted = false;
await this.repository.updateStatus(RepositoryStatus.READY);
}

View File

@@ -59,9 +59,11 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
} else {
content = Buffer.from("");
}
await storage.write(file.originalCachePath, content, file, this);
this.repository.model.isReseted = false;
await this.repository.model.save();
if (this.repository.status !== RepositoryStatus.READY)
await this.repository.updateStatus(RepositoryStatus.READY);
await storage.write(file.originalCachePath, content, file, this);
return stream.Readable.from(content);
} catch (error) {
if ((error as any).status === 404 || (error as any).httpStatus === 404) {