Arnaud D. Natali 2023-09-18 15:09:30 +02:00 committed by GitHub
commit 4c8d1dcba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 3 deletions

View File

@ -7,7 +7,8 @@ import CronService from "@Services/common/CronService/CronService";
(async () => { (async () => {
try { try {
const variables = await Container.get(BackendVariables).validate(); const variables = await Container.get(BackendVariables).validate();
if(variables.ENV === "stg"){ Container.get(CronService).archiveFiles();
if(variables.ENV !== "dev"){
Container.get(CronService).sendMails(); Container.get(CronService).sendMails();
} }
} catch (e) { } catch (e) {

View File

@ -1,14 +1,15 @@
import { Service } from "typedi"; import { Service } from "typedi";
import { CronJob } from "cron"; import { CronJob } from "cron";
import MailchimpService from "../MailchimpService/MailchimpService"; import MailchimpService from "../MailchimpService/MailchimpService";
import FilesService from "../FilesService/FilesService";
@Service() @Service()
export default class CronService { export default class CronService {
constructor(private mailchimpService: MailchimpService) {} constructor(private mailchimpService: MailchimpService, private filesService: FilesService) {}
public async sendMails() { public async sendMails() {
const cronJob = new CronJob("*/15 * * * * *", async () => { const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes
try { try {
await this.mailchimpService.sendEmails(); await this.mailchimpService.sendEmails();
} catch (e) { } catch (e) {
@ -21,4 +22,19 @@ export default class CronService {
cronJob.start(); cronJob.start();
} }
} }
public async archiveFiles() {
const cronJob = new CronJob("0 0 * * MON", async () => { // Every monday at midnight
try {
await this.filesService.archiveOldFiles();
} catch (e) {
console.error(e);
}
});
// Start job
if (!cronJob.running) {
cronJob.start();
}
}
} }

View File

@ -107,4 +107,32 @@ export default class FilesService extends BaseService {
return this.filesRepository.deleteKeyAndArchive(uid); return this.filesRepository.deleteKeyAndArchive(uid);
} }
/**
* @description : find files to be archived
* @throws {Error} If file key cannot be deleted or archived
*/
public async getFilesToBeArchived() {
return this.filesRepository.findMany({
where: {
archived_at: null,
created_at: { lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30 * 3) }, // 90 days
},
});
}
/**
* @description : find files to be archived
* @throws {Error} If file key cannot be deleted or archived
*/
public async archiveOldFiles() {
const files = await this.getFilesToBeArchived();
files.forEach(async (file) => {
try {
await this.deleteKeyAndArchive(file.uid);
} catch (error) {
console.error(error);
}
});
}
} }