From b1bea88f3a4f302f161bf78b8d2ae2171d450326 Mon Sep 17 00:00:00 2001 From: lehuygiang28 Date: Sat, 10 Aug 2024 01:49:29 +0700 Subject: [PATCH] refactor(be/tasks): :recycle: split to 2 private method --- .../tasks/services/task-scheduling.service.ts | 72 +++++++++++-------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/apps/be/src/app/tasks/services/task-scheduling.service.ts b/apps/be/src/app/tasks/services/task-scheduling.service.ts index a6749d8..e825f25 100644 --- a/apps/be/src/app/tasks/services/task-scheduling.service.ts +++ b/apps/be/src/app/tasks/services/task-scheduling.service.ts @@ -17,37 +17,8 @@ export class TaskSchedulingService implements OnModuleInit { ) {} async onModuleInit() { - /** - * Add cron background job to scan and delete tasks that have been soft deleted - */ - if (this.SCAN_DELETED_TASK_CRON_HISTORIC) { - await Promise.allSettled( - this.SCAN_DELETED_TASK_CRON_HISTORIC.map((cron) => { - this.clearTaskQueue.removeRepeatable( - 'clearTasks', - { - pattern: cron, - }, - 'clearTasks_id', - ); - }), - ); - } - - await this.clearTaskQueue.add( - 'clearTasks', - {}, - { - jobId: 'clearTasks_id', - removeOnComplete: true, - removeOnFail: true, - priority: 900, - attempts: 9, - repeat: { - pattern: this.SCAN_DELETED_TASK_CRON, - }, - }, - ); + await this.addCronToCleanDeletedTasks(); + await this.scanDeletedTasks(); } public async startCronTask(task: Task) { @@ -126,4 +97,43 @@ export class TaskSchedulingService implements OnModuleInit { return newTask; } + + /** + * Add a task to clean deleted tasks + */ + private async addCronToCleanDeletedTasks() { + await this.clearTaskQueue.add( + 'clearTasks', + {}, + { + jobId: 'clearTasks_id', + removeOnComplete: true, + removeOnFail: true, + priority: 900, + attempts: 9, + repeat: { + pattern: this.SCAN_DELETED_TASK_CRON, + }, + }, + ); + } + + private async scanDeletedTasks() { + /** + * Add cron background job to scan and delete tasks that have been soft deleted + */ + if (this.SCAN_DELETED_TASK_CRON_HISTORIC) { + await Promise.allSettled( + this.SCAN_DELETED_TASK_CRON_HISTORIC.map((cron) => { + this.clearTaskQueue.removeRepeatable( + 'clearTasks', + { + pattern: cron, + }, + 'clearTasks_id', + ); + }), + ); + } + } }