add cronJob to archive old files
This commit is contained in:
parent
ca6d1e5a4a
commit
4313cc939c
@ -7,7 +7,8 @@ import CronService from "@Services/common/CronService/CronService";
|
||||
(async () => {
|
||||
try {
|
||||
const variables = await Container.get(BackendVariables).validate();
|
||||
if(variables.ENV === "stg"){
|
||||
Container.get(CronService).archiveFiles();
|
||||
if(variables.ENV !== "dev"){
|
||||
Container.get(CronService).sendMails();
|
||||
}
|
||||
} catch (e) {
|
||||
|
@ -1,14 +1,15 @@
|
||||
import { Service } from "typedi";
|
||||
import { CronJob } from "cron";
|
||||
import MailchimpService from "../MailchimpService/MailchimpService";
|
||||
import FilesService from "../FilesService/FilesService";
|
||||
|
||||
|
||||
@Service()
|
||||
export default class CronService {
|
||||
constructor(private mailchimpService: MailchimpService) {}
|
||||
constructor(private mailchimpService: MailchimpService, private filesService: FilesService) {}
|
||||
|
||||
public async sendMails() {
|
||||
const cronJob = new CronJob("*/15 * * * * *", async () => {
|
||||
const cronJob = new CronJob("*/15 * * * *", async () => { // Every 15 minutes
|
||||
try {
|
||||
await this.mailchimpService.sendEmails();
|
||||
} catch (e) {
|
||||
@ -21,4 +22,19 @@ export default class CronService {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,4 +107,32 @@ export default class FilesService extends BaseService {
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user