add cronJob to archive old files (#67)
solve this ticket: https://www.notion.so/smart-chain/8451b1abb95046ceae81b49cfe26fe71?v=ac57690f41144dd8b703ad8ce35b8f3f&p=aae880d89d5542af94affc637386a533&pm=s
This commit is contained in:
commit
4c8d1dcba6
@ -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) {
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user