讲真 这是个神奇的框架

Q&A

如何只运行一次

  • redis 监听

egg-schedule

https://github.com/eggjs/egg/issues/1736#issuecomment-347760759

app/schedule/{name}.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { Subscription } from 'egg'

export default class Live extends Subscription {
static get schedule() {
return {
type: 'worker',
immediate: true,
}
}

async subscribe() {
console.log('eggjs 只运行一次')
}
}

agent.js / app.js

https://github.com/eggjs/egg/issues/2582#issuecomment-390539093

app.js

1
2
3
4
5
6
7
8
9
10
11
12
export default class App {
app

constructor(app) {
this.app = app
}

// 启动成功后运行
async willReady() {
console.log('eggjs 只运行一次')
}
}