Skip to content

Commit

Permalink
fix: execute state check (#308)
Browse files Browse the repository at this point in the history
taskService.findExecuteTask 添加判断,只处理状态仍为 waiting 状态的 task
  • Loading branch information
elrrrrrrr authored Sep 2, 2022
1 parent 091420a commit f673ab8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/core/service/TaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class TaskService extends AbstractService {
const taskId = await this.queueAdapter.pop<string>(taskType);
if (taskId) {
const task = await this.taskRepository.findTask(taskId);
if (task) {
// 队列中的任务预期为 createTask 和 retryTask 传入
// 可能任务已经触发或执行完成,需要再次判断一下任务状态
if (task && task.state === TaskState.Waiting) {
task.setExecuteWorker();
task.state = TaskState.Processing;
task.attempts += 1;
Expand Down
12 changes: 12 additions & 0 deletions test/core/service/TaskService/findExecuteTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,17 @@ describe('test/core/service/TaskService/findExecuteTask.test.ts', () => {
task = await taskService.findExecuteTask(TaskType.SyncPackage);
assert(!task);
});

it('should check task state before execute', async () => {
const task1 = await packageSyncerService.createTask('foo-1');
const task2 = await packageSyncerService.createTask('foo-2');
// task 已被执行成功
await taskService.finishTask(task1, TaskState.Success, '');

let executeTask = await taskService.findExecuteTask(task1.type);
assert(executeTask === null);
executeTask = await taskService.findExecuteTask(task1.type);
assert(executeTask?.taskId === task2.taskId);
});
});
});

0 comments on commit f673ab8

Please sign in to comment.