Skip to content

Commit

Permalink
refactor(be/tasks): ♻️ split to 2 private method
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Aug 9, 2024
1 parent ad49281 commit b1bea88
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions apps/be/src/app/tasks/services/task-scheduling.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
);
}),
);
}
}
}

0 comments on commit b1bea88

Please sign in to comment.