Skip to content

Commit

Permalink
chore: fix dev env remove old task (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-jin authored Jun 10, 2021
1 parent 0030d24 commit 2bfc957
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 18 additions & 2 deletions packages/task/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AutoConfiguration {
container: IMidwayContainer,
app: IMidwayApplication
): Promise<void> {
await this.loadTask();
await this.loadTask(container);
await this.loadLocalTask();
await this.loadQueue(container);
}
Expand All @@ -51,8 +51,9 @@ export class AutoConfiguration {
});
}

async loadTask() {
async loadTask(container) {
const modules = listModule(MODULE_TASK_KEY);
const queueTaskMap = {};
for (const module of modules) {
const rules = getClassMetadata(MODULE_TASK_METADATA, module);
for (const rule of rules) {
Expand All @@ -65,10 +66,25 @@ export class AutoConfiguration {
const service = await ctx.requestContext.getAsync(module);
rule.value.call(service, job.data);
});
queueTaskMap[`${rule.name}:${rule.propertyKey}`] = queue;
const allJobs = await queue.getRepeatableJobs();
if (allJobs.length > 0) {
if (
!(
allJobs.length === 1 &&
allJobs[0].cron === rule.options.repeat.cron
)
) {
for (const item of allJobs) {
await queue.removeRepeatableByKey(item.key);
}
}
}
queue.add({}, rule.options);
this.queueList.push(queue);
}
}
container.registerObject('queueTaskMap', queueTaskMap);
}

async loadLocalTask() {
Expand Down
11 changes: 9 additions & 2 deletions packages/task/src/service/queueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ export class QueueService {
@Inject('queueMap')
queueMap;

@Inject('queueTaskMap')
queueTaskMap;

/**
* @deprecated please use execute method
*/
async excute(queueName: any, data: any, options: JobOptions) {
async excute(queueName: any, data: any, options: JobOptions = {}) {
return this.execute(queueName, data, options);
}

async execute(queueName: any, data: any, options: JobOptions) {
async execute(queueName: any, data: any, options: JobOptions = {}) {
const queue = this.queueMap[`${queueName.name}:execute`] as Queue;
return await queue.add(data, options);
}

getClassQueue(queueName: any): Queue {
return this.queueMap[`${queueName.name}:execute`] as Queue;
}

getQueueTask(queueClass: string, queueName: string): Queue {
return this.queueTaskMap[`${queueClass}:${queueName}`] as Queue;
}
}

0 comments on commit 2bfc957

Please sign in to comment.