Skip to content

[Question] 任务队列 (bull) runJob 分布式的情况下出现并发执行,是更新导致的? #4026

Answered by czy88840616
7kyun asked this question in Q&A
Discussion options

You must be logged in to vote

试了下,我简单描述下,测试代码是并发 2,定时 1s,任务执行会等待 10s,启动了两个子进程。

let count = 0;

@Processor('test', 2, {
  repeat: {
    cron: FORMAT.CRONTAB.EVERY_SECOND
  },
})
export class TestProcessor implements IProcessor {
  async execute(data) {
    console.log(`${new Date().toLocaleString()} 进程:${process.pid} processor start: ${count++}`);
    await new Promise(resolve => setTimeout(resolve, 10000));
    console.log(`${new Date().toLocaleString()} 进程:${process.pid} processor end`);
  }
}

结果如下:

每隔 5s,会定时触发一次任务,两个进程号为 8742 和 8743。

  • 1、38s 时,8742 触发了任务,编号 0
  • 2、39s 时,8742 触发了任务,编号 1
  • 3、40s 时,8743 触发了任务,编号 0
  • 4、41s 时,8743 触发了任务,编号 1
  • 5、48s 时,8742 编号 0 任务执行完毕

说明每个 worker 的并发度是独立计算的,并发的配置指的是每个 worker 最多可同时执行 job 的数量,而不是同…

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
1 reply
@7kyun
Comment options

Comment options

You must be logged in to vote
4 replies
@czy88840616
Comment options

@7kyun
Comment options

@czy88840616
Comment options

Answer selected by 7kyun
@7kyun
Comment options

Comment options

You must be logged in to vote
1 reply
@czy88840616
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
type: question / discussion This issue or pull request need be discussion
3 participants