pg-boss 10.0.0-beta2 → 10.0.0-beta21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -20
- package/package.json +1 -3
- package/src/attorney.js +35 -36
- package/src/boss.js +13 -57
- package/src/contractor.js +18 -12
- package/src/db.js +12 -38
- package/src/index.js +92 -67
- package/src/manager.js +109 -164
- package/src/plans.js +404 -298
- package/src/timekeeper.js +51 -98
- package/types.d.ts +70 -67
package/src/index.js
CHANGED
|
@@ -13,6 +13,17 @@ const events = {
|
|
|
13
13
|
stopped: 'stopped'
|
|
14
14
|
}
|
|
15
15
|
class PgBoss extends EventEmitter {
|
|
16
|
+
#stoppingOn
|
|
17
|
+
#stopped
|
|
18
|
+
#starting
|
|
19
|
+
#started
|
|
20
|
+
#config
|
|
21
|
+
#db
|
|
22
|
+
#boss
|
|
23
|
+
#contractor
|
|
24
|
+
#manager
|
|
25
|
+
#timekeeper
|
|
26
|
+
|
|
16
27
|
static getConstructionPlans (schema) {
|
|
17
28
|
return Contractor.constructionPlans(schema)
|
|
18
29
|
}
|
|
@@ -25,130 +36,145 @@ class PgBoss extends EventEmitter {
|
|
|
25
36
|
return Contractor.rollbackPlans(schema, version)
|
|
26
37
|
}
|
|
27
38
|
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
static states = plans.JOB_STATES
|
|
40
|
+
static policies = plans.QUEUE_POLICIES
|
|
30
41
|
|
|
42
|
+
constructor (value) {
|
|
31
43
|
super()
|
|
32
44
|
|
|
33
|
-
|
|
45
|
+
this.#stoppingOn = null
|
|
46
|
+
this.#stopped = true
|
|
47
|
+
|
|
48
|
+
const config = Attorney.getConfig(value)
|
|
49
|
+
this.#config = config
|
|
50
|
+
|
|
51
|
+
const db = this.getDb()
|
|
52
|
+
this.#db = db
|
|
34
53
|
|
|
35
54
|
if (db.isOurs) {
|
|
36
|
-
|
|
55
|
+
this.#promoteEvents(db)
|
|
37
56
|
}
|
|
38
57
|
|
|
39
|
-
const
|
|
40
|
-
Object.keys(manager.events).forEach(event => promoteEvent.call(this, manager, manager.events[event]))
|
|
41
|
-
manager.functions.forEach(func => promoteFunction.call(this, manager, func))
|
|
58
|
+
const contractor = new Contractor(db, config)
|
|
42
59
|
|
|
60
|
+
const manager = new Manager(db, config)
|
|
43
61
|
const bossConfig = { ...config, manager }
|
|
44
62
|
|
|
45
63
|
const boss = new Boss(db, bossConfig)
|
|
46
|
-
Object.keys(boss.events).forEach(event => promoteEvent.call(this, boss, boss.events[event]))
|
|
47
|
-
boss.functions.forEach(func => promoteFunction.call(this, boss, func))
|
|
48
64
|
|
|
49
65
|
const timekeeper = new Timekeeper(db, bossConfig)
|
|
50
|
-
Object.keys(timekeeper.events).forEach(event => promoteEvent.call(this, timekeeper, timekeeper.events[event]))
|
|
51
|
-
timekeeper.functions.forEach(func => promoteFunction.call(this, timekeeper, func))
|
|
52
|
-
|
|
53
66
|
manager.timekeeper = timekeeper
|
|
54
67
|
|
|
55
|
-
this
|
|
56
|
-
this
|
|
57
|
-
this
|
|
58
|
-
this.db = db
|
|
59
|
-
this.boss = boss
|
|
60
|
-
this.contractor = new Contractor(db, config)
|
|
61
|
-
this.manager = manager
|
|
62
|
-
this.timekeeper = timekeeper
|
|
63
|
-
|
|
64
|
-
function getDb (config) {
|
|
65
|
-
if (config.db) {
|
|
66
|
-
return config.db
|
|
67
|
-
}
|
|
68
|
+
this.#promoteEvents(manager)
|
|
69
|
+
this.#promoteEvents(boss)
|
|
70
|
+
this.#promoteEvents(timekeeper)
|
|
68
71
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
this.#promoteFunctions(boss)
|
|
73
|
+
this.#promoteFunctions(contractor)
|
|
74
|
+
this.#promoteFunctions(manager)
|
|
75
|
+
this.#promoteFunctions(timekeeper)
|
|
76
|
+
|
|
77
|
+
this.#boss = boss
|
|
78
|
+
this.#contractor = contractor
|
|
79
|
+
this.#manager = manager
|
|
80
|
+
this.#timekeeper = timekeeper
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getDb () {
|
|
84
|
+
if (this.#db) {
|
|
85
|
+
return this.#db
|
|
72
86
|
}
|
|
73
87
|
|
|
74
|
-
|
|
75
|
-
this
|
|
88
|
+
if (this.#config.db) {
|
|
89
|
+
return this.#config.db
|
|
76
90
|
}
|
|
77
91
|
|
|
78
|
-
|
|
92
|
+
const db = new Db(this.#config)
|
|
93
|
+
db.isOurs = true
|
|
94
|
+
return db
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#promoteEvents (emitter) {
|
|
98
|
+
for (const event of Object.values(emitter?.events)) {
|
|
79
99
|
emitter.on(event, arg => this.emit(event, arg))
|
|
80
100
|
}
|
|
81
101
|
}
|
|
82
102
|
|
|
103
|
+
#promoteFunctions (obj) {
|
|
104
|
+
for (const func of obj?.functions) {
|
|
105
|
+
this[func.name] = (...args) => func.apply(obj, args)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
83
109
|
async start () {
|
|
84
|
-
if (this
|
|
110
|
+
if (this.#starting || this.#started) {
|
|
85
111
|
return
|
|
86
112
|
}
|
|
87
113
|
|
|
88
|
-
this
|
|
114
|
+
this.#starting = true
|
|
89
115
|
|
|
90
|
-
if (this
|
|
91
|
-
await this
|
|
116
|
+
if (this.#db.isOurs && !this.#db.opened) {
|
|
117
|
+
await this.#db.open()
|
|
92
118
|
}
|
|
93
119
|
|
|
94
|
-
if (this
|
|
95
|
-
await this
|
|
120
|
+
if (this.#config.migrate) {
|
|
121
|
+
await this.#contractor.start()
|
|
96
122
|
} else {
|
|
97
|
-
await this
|
|
123
|
+
await this.#contractor.check()
|
|
98
124
|
}
|
|
99
125
|
|
|
100
|
-
this
|
|
126
|
+
this.#manager.start()
|
|
101
127
|
|
|
102
|
-
if (this
|
|
103
|
-
await this
|
|
128
|
+
if (this.#config.supervise) {
|
|
129
|
+
await this.#boss.supervise()
|
|
104
130
|
}
|
|
105
131
|
|
|
106
|
-
if (this
|
|
107
|
-
await this
|
|
132
|
+
if (this.#config.monitorStateIntervalSeconds) {
|
|
133
|
+
await this.#boss.monitor()
|
|
108
134
|
}
|
|
109
135
|
|
|
110
|
-
if (this
|
|
111
|
-
await this
|
|
136
|
+
if (this.#config.schedule) {
|
|
137
|
+
await this.#timekeeper.start()
|
|
112
138
|
}
|
|
113
139
|
|
|
114
|
-
this
|
|
115
|
-
this
|
|
116
|
-
this
|
|
140
|
+
this.#starting = false
|
|
141
|
+
this.#started = true
|
|
142
|
+
this.#stopped = false
|
|
117
143
|
|
|
118
144
|
return this
|
|
119
145
|
}
|
|
120
146
|
|
|
121
147
|
async stop (options = {}) {
|
|
122
|
-
if (this
|
|
148
|
+
if (this.#stoppingOn || this.#stopped) {
|
|
123
149
|
return
|
|
124
150
|
}
|
|
125
151
|
|
|
126
|
-
let {
|
|
152
|
+
let { close = true, graceful = true, timeout = 30000, wait = true } = options
|
|
127
153
|
|
|
128
154
|
timeout = Math.max(timeout, 1000)
|
|
129
155
|
|
|
130
|
-
this
|
|
156
|
+
this.#stoppingOn = Date.now()
|
|
131
157
|
|
|
132
|
-
await this
|
|
133
|
-
await this
|
|
134
|
-
await this
|
|
158
|
+
await this.#manager.stop()
|
|
159
|
+
await this.#timekeeper.stop()
|
|
160
|
+
await this.#boss.stop()
|
|
135
161
|
|
|
136
162
|
await new Promise((resolve, reject) => {
|
|
137
163
|
const shutdown = async () => {
|
|
138
164
|
try {
|
|
139
|
-
if (this
|
|
140
|
-
throw new Error(this
|
|
165
|
+
if (this.#config.__test__throw_shutdown) {
|
|
166
|
+
throw new Error(this.#config.__test__throw_shutdown)
|
|
141
167
|
}
|
|
142
168
|
|
|
143
|
-
await this
|
|
169
|
+
await this.#manager.failWip()
|
|
144
170
|
|
|
145
|
-
if (this
|
|
146
|
-
await this
|
|
171
|
+
if (this.#db.isOurs && this.#db.opened && close) {
|
|
172
|
+
await this.#db.close()
|
|
147
173
|
}
|
|
148
174
|
|
|
149
|
-
this
|
|
150
|
-
this
|
|
151
|
-
this
|
|
175
|
+
this.#stopped = true
|
|
176
|
+
this.#stoppingOn = null
|
|
177
|
+
this.#started = false
|
|
152
178
|
|
|
153
179
|
this.emit(events.stopped)
|
|
154
180
|
resolve()
|
|
@@ -168,13 +194,13 @@ class PgBoss extends EventEmitter {
|
|
|
168
194
|
|
|
169
195
|
setImmediate(async () => {
|
|
170
196
|
try {
|
|
171
|
-
if (this
|
|
172
|
-
throw new Error(this
|
|
197
|
+
if (this.#config.__test__throw_stop_monitor) {
|
|
198
|
+
throw new Error(this.#config.__test__throw_stop_monitor)
|
|
173
199
|
}
|
|
174
200
|
|
|
175
|
-
const isWip = () => this
|
|
201
|
+
const isWip = () => this.#manager.getWipData({ includeInternal: false }).length > 0
|
|
176
202
|
|
|
177
|
-
while ((Date.now() - this
|
|
203
|
+
while ((Date.now() - this.#stoppingOn) < timeout && isWip()) {
|
|
178
204
|
await delay(500)
|
|
179
205
|
}
|
|
180
206
|
|
|
@@ -189,4 +215,3 @@ class PgBoss extends EventEmitter {
|
|
|
189
215
|
}
|
|
190
216
|
|
|
191
217
|
module.exports = PgBoss
|
|
192
|
-
module.exports.states = plans.states
|