Add old file deletion daily job

This commit is contained in:
Alice Gaudon 2021-03-30 16:31:23 +02:00
parent c8d7c664e2
commit 1dcbb6b414
4 changed files with 73 additions and 1 deletions

View File

@ -54,4 +54,7 @@
],
default_url_domain_for_files: 0,
default_url_domain_for_urls: 1,
// 1 hour in ms
delete_old_files_interval: 3600000,
}

View File

@ -26,4 +26,7 @@
],
default_url_domain_for_files: 0,
default_url_domain_for_urls: 0,
// 1 day in ms
delete_old_files_interval: 86400000
}

View File

@ -42,7 +42,7 @@ import MagicLinkController from "swaf/auth/magic_link/MagicLinkController";
import AddUsedToMagicLinksMigration from "swaf/auth/magic_link/AddUsedToMagicLinksMigration";
import MakeMagicLinksSessionNotUniqueMigration from "swaf/auth/magic_link/MakeMagicLinksSessionNotUniqueMigration";
import AddPasswordToUsersMigration from "swaf/auth/password/AddPasswordToUsersMigration";
import DropNameFromUsers from "swaf/auth/migrations/DropNameFromUsers";
import DeleteOldFilesJobComponent from "./DeleteOldFilesJobComponent";
import packageJson = require('./package.json');
import ReplaceTtlWithExpiresAtFilesTable from "./migrations/ReplaceTtlWithExpiresAtFilesTable";
@ -117,6 +117,10 @@ export default class App extends Application {
// WebSocket server
this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent)));
// Jobs
this.use(new DeleteOldFilesJobComponent());
}
private registerWebSocketListeners() {

View File

@ -0,0 +1,62 @@
import ApplicationComponent from "swaf/ApplicationComponent";
import FileModel from "./models/FileModel";
import {logger} from "swaf/Logger";
import config from "config";
import {WhereTest} from "swaf/db/ModelQuery";
import MysqlComponent from "swaf/components/MysqlComponent";
import FileController from "./controllers/FileController";
import Timeout = NodeJS.Timeout;
export default class DeleteOldFilesJobComponent extends ApplicationComponent {
private timeout?: Timeout;
private readonly interval: number = config.get<number>('delete_old_files_interval');
public async start(): Promise<void> {
if (this.getApp().as(MysqlComponent).canServe()) {
await this.run();
}
this.schedule();
}
public async stop(): Promise<void> {
if (this.timeout) {
clearInterval(this.timeout);
this.timeout = undefined;
}
}
private schedule(): void {
if (this.timeout) {
clearInterval(this.timeout);
this.timeout = undefined;
}
this.timeout = setInterval(() => {
if (this.getApp().as(MysqlComponent).canServe()) {
this.run().catch(err => logger.error(err));
}
}, this.interval);
logger.info(`Scheduled old file deletion job every ${this.interval}ms`);
}
private async run(): Promise<void> {
const filesToDelete = await FileModel.select()
.where('expires_at', new Date(), WhereTest.LE)
.get();
if (filesToDelete.length > 0) {
logger.info('Deleting old files...');
} else {
logger.info('No old file to delete.');
return;
}
for (const file of filesToDelete) {
logger.info(`Deleting ${file.id}`);
if (!file.shouldBeDeleted()) throw new Error('File should not be deleted.');
await FileController.deleteFile(file);
}
}
}