pg-boss 11.1.2 → 12.0.0-beta2

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/src/index.js DELETED
@@ -1,211 +0,0 @@
1
- const EventEmitter = require('node:events')
2
- const plans = require('./plans')
3
- const Attorney = require('./attorney')
4
- const Contractor = require('./contractor')
5
- const Manager = require('./manager')
6
- const Timekeeper = require('./timekeeper')
7
- const Boss = require('./boss')
8
- const Db = require('./db')
9
- const { delay } = require('./tools')
10
-
11
- const events = {
12
- error: 'error',
13
- stopped: 'stopped'
14
- }
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
-
27
- static getConstructionPlans (schema) {
28
- return Contractor.constructionPlans(schema)
29
- }
30
-
31
- static getMigrationPlans (schema, version) {
32
- return Contractor.migrationPlans(schema, version)
33
- }
34
-
35
- static getRollbackPlans (schema, version) {
36
- return Contractor.rollbackPlans(schema, version)
37
- }
38
-
39
- static states = plans.JOB_STATES
40
- static policies = plans.QUEUE_POLICIES
41
-
42
- constructor (value) {
43
- super()
44
-
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
53
-
54
- if (db._pgbdb) {
55
- this.#promoteEvents(db)
56
- }
57
-
58
- const contractor = new Contractor(db, config)
59
-
60
- const manager = new Manager(db, config)
61
- const bossConfig = { ...config, manager }
62
-
63
- const boss = new Boss(db, bossConfig)
64
-
65
- const timekeeper = new Timekeeper(db, bossConfig)
66
- manager.timekeeper = timekeeper
67
-
68
- this.#promoteEvents(manager)
69
- this.#promoteEvents(boss)
70
- this.#promoteEvents(timekeeper)
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
86
- }
87
-
88
- if (this.#config.db) {
89
- return this.#config.db
90
- }
91
-
92
- return new Db(this.#config)
93
- }
94
-
95
- #promoteEvents (emitter) {
96
- for (const event of Object.values(emitter?.events)) {
97
- emitter.on(event, arg => this.emit(event, arg))
98
- }
99
- }
100
-
101
- #promoteFunctions (obj) {
102
- for (const func of obj?.functions) {
103
- this[func.name] = (...args) => func.apply(obj, args)
104
- }
105
- }
106
-
107
- async start () {
108
- if (this.#starting || this.#started) {
109
- return this
110
- }
111
-
112
- this.#starting = true
113
-
114
- if (this.#db._pgbdb && !this.#db.opened) {
115
- await this.#db.open()
116
- }
117
-
118
- if (this.#config.migrate) {
119
- await this.#contractor.start()
120
- } else {
121
- await this.#contractor.check()
122
- }
123
-
124
- await this.#manager.start()
125
-
126
- if (this.#config.supervise) {
127
- await this.#boss.start()
128
- }
129
-
130
- if (this.#config.schedule) {
131
- await this.#timekeeper.start()
132
- }
133
-
134
- this.#starting = false
135
- this.#started = true
136
- this.#stopped = false
137
-
138
- return this
139
- }
140
-
141
- async stop (options = {}) {
142
- if (this.#stoppingOn || this.#stopped) {
143
- return
144
- }
145
-
146
- let { close = true, graceful = true, timeout = 30000, wait = true } = options
147
-
148
- timeout = Math.max(timeout, 1000)
149
-
150
- this.#stoppingOn = Date.now()
151
-
152
- await this.#manager.stop()
153
- await this.#timekeeper.stop()
154
- await this.#boss.stop()
155
-
156
- await new Promise((resolve, reject) => {
157
- const shutdown = async () => {
158
- try {
159
- if (this.#config.__test__throw_shutdown) {
160
- throw new Error(this.#config.__test__throw_shutdown)
161
- }
162
-
163
- await this.#manager.failWip()
164
-
165
- if (this.#db._pgbdb && this.#db.opened && close) {
166
- await this.#db.close()
167
- }
168
-
169
- this.#stopped = true
170
- this.#stoppingOn = null
171
- this.#started = false
172
-
173
- this.emit(events.stopped)
174
- resolve()
175
- } catch (err) {
176
- this.emit(events.error, err)
177
- reject(err)
178
- }
179
- }
180
-
181
- if (!graceful) {
182
- return shutdown()
183
- }
184
-
185
- if (!wait) {
186
- resolve()
187
- }
188
-
189
- setImmediate(async () => {
190
- try {
191
- if (this.#config.__test__throw_stop_monitor) {
192
- throw new Error(this.#config.__test__throw_stop_monitor)
193
- }
194
-
195
- const isWip = () => this.#manager.getWipData({ includeInternal: false }).length > 0
196
-
197
- while ((Date.now() - this.#stoppingOn) < timeout && isWip()) {
198
- await delay(500)
199
- }
200
-
201
- await shutdown()
202
- } catch (err) {
203
- reject(err)
204
- this.emit(events.error, err)
205
- }
206
- })
207
- })
208
- }
209
- }
210
-
211
- module.exports = PgBoss