egg icon indicating copy to clipboard operation
egg copied to clipboard

schedule的type设置为all的意义?

Open Zhu-jiayu opened this issue 6 years ago • 4 comments

What happens?

1.我有一个定时任务中去请求第三方的token, schedule.type设置的为'all', 如果我有4个进程去跑这个定时任务, 就会存在4个token, 其中只有一个有效, 那这个'all'的意义何在? 2.另外'all'一般用在什么情况下?

示例

exports.schedule = {
    interval: '10s',
    type: 'all', // only run in one worker
    disable: false,//配置该参数为 true 时,这个定时任务不会被启动。
    immediate: true,
  };
  
  exports.task = async ctx => {

    ctx.app.testdata = await ctx.curl('xxxxx')
  };

Zhu-jiayu avatar Sep 02 '20 05:09 Zhu-jiayu

你的场景需要自己扩展 schedule 策略用外部存储,不然集群多机部署一样会挂。

atian25 avatar Sep 02 '20 05:09 atian25

自己的场景需要自己根据具体情况来考虑,希望共用一个 token 就把 token 放到 redis 或者数据库或者别的地方。

JasinYip avatar Sep 02 '20 06:09 JasinYip

好的感谢, 我现在选择的的方案是type='worker', 在单个worker上跑定时任务,之后把结果传给其他进程, 我的另外一个问题是, type='all'一般用在什么情景下, 多个worker会存在多个结果

Zhu-jiayu avatar Sep 02 '20 06:09 Zhu-jiayu

你这种做法只能单机用,后面多机部署你怎么办呢?而且状态存了在本机,你要是没写好就可能会变成有状态应用了。

all 我常用在消费任务队列/消息队列上,定时检查是否有新任务加入队列,进而消费。

JasinYip avatar Sep 02 '20 06:09 JasinYip