koa 定时任务

定时任务用了node-scheduler,任务写在/scheduler/index.js
我想知道我在koaapp.js里面,
是像1一样,直接执行任务
还是像2一样,开个子进程执行任务
(还有一个问题,我要是使用cluster集群跑多核,怎么避免多个koa进程执行多次定时任务)

// app.js
const Koa = require('koa')
const app = new Koa()
const scheduler = require('./scheduler')
const cp = require('child_process')

// 1
scheduler()

// 2
cp.execFile('node', ['./scheduler/index.js'], function (err, stdout, stderr) {
  if (err) console.error(err)
})

app.listen(3000, () => {
  console.log('koa serve run http://localhost:3000')
})

=======
😭有大哥回答一下么

阅读 8.9k
1 个回答

我觉得定时任务是单独的服务,和普通的接口业务服务分开,用1就好,守护进程用别的库。
如果集群,得有个存储中心(比如redis的blocking list)做阻塞队列

Client publish:   LPUSH xxx
Client subscribe:   BLPOP xxx

另:定时任务最好有日志

推荐问题