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/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
- constructor (value) {
29
- const config = Attorney.getConfig(value)
39
+ static states = plans.JOB_STATES
40
+ static policies = plans.QUEUE_POLICIES
30
41
 
42
+ constructor (value) {
31
43
  super()
32
44
 
33
- const db = getDb(config)
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
- promoteEvent.call(this, db, 'error')
55
+ this.#promoteEvents(db)
37
56
  }
38
57
 
39
- const manager = new Manager(db, config)
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.stoppingOn = null
56
- this.stopped = true
57
- this.config = config
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
- const db = new Db(config)
70
- db.isOurs = true
71
- return db
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
- function promoteFunction (obj, func) {
75
- this[func.name] = (...args) => func.apply(obj, args)
88
+ if (this.#config.db) {
89
+ return this.#config.db
76
90
  }
77
91
 
78
- function promoteEvent (emitter, event) {
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.starting || this.started) {
110
+ if (this.#starting || this.#started) {
85
111
  return
86
112
  }
87
113
 
88
- this.starting = true
114
+ this.#starting = true
89
115
 
90
- if (this.db.isOurs && !this.db.opened) {
91
- await this.db.open()
116
+ if (this.#db.isOurs && !this.#db.opened) {
117
+ await this.#db.open()
92
118
  }
93
119
 
94
- if (this.config.migrate) {
95
- await this.contractor.start()
120
+ if (this.#config.migrate) {
121
+ await this.#contractor.start()
96
122
  } else {
97
- await this.contractor.check()
123
+ await this.#contractor.check()
98
124
  }
99
125
 
100
- this.manager.start()
126
+ this.#manager.start()
101
127
 
102
- if (this.config.supervise) {
103
- await this.boss.supervise()
128
+ if (this.#config.supervise) {
129
+ await this.#boss.supervise()
104
130
  }
105
131
 
106
- if (this.config.monitorStateIntervalSeconds) {
107
- await this.boss.monitor()
132
+ if (this.#config.monitorStateIntervalSeconds) {
133
+ await this.#boss.monitor()
108
134
  }
109
135
 
110
- if (this.config.schedule) {
111
- await this.timekeeper.start()
136
+ if (this.#config.schedule) {
137
+ await this.#timekeeper.start()
112
138
  }
113
139
 
114
- this.starting = false
115
- this.started = true
116
- this.stopped = false
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.stoppingOn || this.stopped) {
148
+ if (this.#stoppingOn || this.#stopped) {
123
149
  return
124
150
  }
125
151
 
126
- let { destroy = false, graceful = true, timeout = 30000, wait = true } = options
152
+ let { close = true, graceful = true, timeout = 30000, wait = true } = options
127
153
 
128
154
  timeout = Math.max(timeout, 1000)
129
155
 
130
- this.stoppingOn = Date.now()
156
+ this.#stoppingOn = Date.now()
131
157
 
132
- await this.manager.stop()
133
- await this.timekeeper.stop()
134
- await this.boss.stop()
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.config.__test__throw_shutdown) {
140
- throw new Error(this.config.__test__throw_shutdown)
165
+ if (this.#config.__test__throw_shutdown) {
166
+ throw new Error(this.#config.__test__throw_shutdown)
141
167
  }
142
168
 
143
- await this.manager.failWip()
169
+ await this.#manager.failWip()
144
170
 
145
- if (this.db.isOurs && this.db.opened && destroy) {
146
- await this.db.close()
171
+ if (this.#db.isOurs && this.#db.opened && close) {
172
+ await this.#db.close()
147
173
  }
148
174
 
149
- this.stopped = true
150
- this.stoppingOn = null
151
- this.started = false
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.config.__test__throw_stop_monitor) {
172
- throw new Error(this.config.__test__throw_stop_monitor)
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.manager.getWipData({ includeInternal: false }).length > 0
201
+ const isWip = () => this.#manager.getWipData({ includeInternal: false }).length > 0
176
202
 
177
- while ((Date.now() - this.stoppingOn) < timeout && isWip()) {
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