pg-boss 9.0.3 → 10.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/timekeeper.js CHANGED
@@ -6,7 +6,7 @@ const pMap = require('p-map')
6
6
 
7
7
  const queues = {
8
8
  CRON: '__pgboss__cron',
9
- SEND_IT: '__pgboss__send-it'
9
+ SEND_IT: '__pgboss__send_it'
10
10
  }
11
11
 
12
12
  const events = {
@@ -52,6 +52,9 @@ class Timekeeper extends EventEmitter {
52
52
  // cache the clock skew from the db server
53
53
  await this.cacheClockSkew()
54
54
 
55
+ await this.manager.createQueue(queues.CRON)
56
+ await this.manager.createQueue(queues.SEND_IT)
57
+
55
58
  await this.manager.work(queues.CRON, { newJobCheckIntervalSeconds: this.config.cronWorkerIntervalSeconds }, (job) => this.onCron(job))
56
59
  await this.manager.work(queues.SEND_IT, { newJobCheckIntervalSeconds: this.config.cronWorkerIntervalSeconds, teamSize: 50, teamConcurrency: 5 }, (job) => this.onSendIt(job))
57
60
 
@@ -134,8 +137,7 @@ class Timekeeper extends EventEmitter {
134
137
  async checkSchedulesAsync () {
135
138
  const opts = {
136
139
  retryLimit: 2,
137
- retentionSeconds: 60,
138
- onComplete: false
140
+ retentionSeconds: 60
139
141
  }
140
142
 
141
143
  await this.manager.sendDebounced(queues.CRON, null, opts, 60)
@@ -145,8 +147,8 @@ class Timekeeper extends EventEmitter {
145
147
  if (this.stopped) return
146
148
 
147
149
  try {
148
- if (this.config.__test__throw_clock_monitoring) {
149
- throw new Error(this.config.__test__throw_clock_monitoring)
150
+ if (this.config.__test__throw_cron_processing) {
151
+ throw new Error(this.config.__test__throw_cron_processing)
150
152
  }
151
153
 
152
154
  const items = await this.getSchedules()
@@ -186,8 +188,7 @@ class Timekeeper extends EventEmitter {
186
188
  async send (job) {
187
189
  const options = {
188
190
  singletonKey: job.name,
189
- singletonSeconds: 60,
190
- onComplete: false
191
+ singletonSeconds: 60
191
192
  }
192
193
 
193
194
  await this.manager.send(queues.SEND_IT, job, options)
package/src/tools.js ADDED
@@ -0,0 +1,28 @@
1
+ module.exports = {
2
+ delay
3
+ }
4
+
5
+ function delay (ms, error) {
6
+ const { setTimeout } = require('timers/promises')
7
+ const ac = new AbortController()
8
+
9
+ const promise = new Promise((resolve, reject) => {
10
+ setTimeout(ms, null, { signal: ac.signal })
11
+ .then(() => {
12
+ if (error) {
13
+ reject(new Error(error))
14
+ } else {
15
+ resolve()
16
+ }
17
+ })
18
+ .catch(resolve)
19
+ })
20
+
21
+ promise.abort = () => {
22
+ if (!ac.signal.aborted) {
23
+ ac.abort()
24
+ }
25
+ }
26
+
27
+ return promise
28
+ }
package/src/worker.js CHANGED
@@ -1,4 +1,4 @@
1
- const delay = require('delay')
1
+ const { delay } = require('./tools')
2
2
 
3
3
  const WORKER_STATES = {
4
4
  created: 'created',
@@ -34,7 +34,7 @@ class Worker {
34
34
  this.beenNotified = true
35
35
 
36
36
  if (this.loopDelayPromise) {
37
- this.loopDelayPromise.clear()
37
+ this.loopDelayPromise.abort()
38
38
  }
39
39
  }
40
40
 
@@ -74,7 +74,7 @@ class Worker {
74
74
 
75
75
  this.lastJobDuration = duration
76
76
 
77
- if (!this.stopping && !this.beenNotified && duration < this.interval) {
77
+ if (!this.stopping && !this.beenNotified && (this.interval - duration > 500)) {
78
78
  this.loopDelayPromise = delay(this.interval - duration)
79
79
  await this.loopDelayPromise
80
80
  this.loopDelayPromise = null
@@ -91,7 +91,7 @@ class Worker {
91
91
  this.state = WORKER_STATES.stopping
92
92
 
93
93
  if (this.loopDelayPromise) {
94
- this.loopDelayPromise.clear()
94
+ this.loopDelayPromise.abort()
95
95
  }
96
96
  }
97
97
  }
package/types.d.ts CHANGED
@@ -20,20 +20,20 @@ declare namespace PgBoss {
20
20
  }
21
21
 
22
22
  interface QueueOptions {
23
- uuid?: "v1" | "v4";
24
23
  monitorStateIntervalSeconds?: number;
25
24
  monitorStateIntervalMinutes?: number;
26
25
  }
27
26
 
28
27
  interface SchedulingOptions {
29
- noScheduling?: boolean;
28
+ schedule?: boolean;
30
29
 
31
30
  clockMonitorIntervalSeconds?: number;
32
31
  clockMonitorIntervalMinutes?: number;
33
32
  }
34
33
 
35
34
  interface MaintenanceOptions {
36
- noSupervisor?: boolean;
35
+ supervise?: boolean;
36
+ migrate?: boolean;
37
37
 
38
38
  deleteAfterSeconds?: number;
39
39
  deleteAfterMinutes?: number;
@@ -56,11 +56,6 @@ declare namespace PgBoss {
56
56
  & RetentionOptions
57
57
  & RetryOptions
58
58
  & JobPollingOptions
59
- & CompletionOptions
60
-
61
- interface CompletionOptions {
62
- onComplete?: boolean;
63
- }
64
59
 
65
60
  interface ExpirationOptions {
66
61
  expireInSeconds?: number;
@@ -82,14 +77,15 @@ declare namespace PgBoss {
82
77
  }
83
78
 
84
79
  interface JobOptions {
80
+ id?: string,
85
81
  priority?: number;
86
82
  startAfter?: number | string | Date;
87
83
  singletonKey?: string;
88
- useSingletonQueue?: boolean;
89
84
  singletonSeconds?: number;
90
85
  singletonMinutes?: number;
91
86
  singletonHours?: number;
92
87
  singletonNextSlot?: boolean;
88
+ deadLetter?: string;
93
89
  }
94
90
 
95
91
  interface ConnectionOptions {
@@ -98,7 +94,7 @@ declare namespace PgBoss {
98
94
 
99
95
  type InsertOptions = ConnectionOptions;
100
96
 
101
- type SendOptions = JobOptions & ExpirationOptions & RetentionOptions & RetryOptions & CompletionOptions & ConnectionOptions;
97
+ type SendOptions = JobOptions & ExpirationOptions & RetentionOptions & RetryOptions & ConnectionOptions;
102
98
 
103
99
  type ScheduleOptions = SendOptions & { tz?: string }
104
100
 
@@ -109,7 +105,7 @@ declare namespace PgBoss {
109
105
 
110
106
  interface CommonJobFetchOptions {
111
107
  includeMetadata?: boolean;
112
- enforceSingletonQueueActiveLimit?: boolean;
108
+ priority?: boolean;
113
109
  }
114
110
 
115
111
  type JobFetchOptions = CommonJobFetchOptions & {
@@ -127,7 +123,6 @@ declare namespace PgBoss {
127
123
 
128
124
  type FetchOptions = {
129
125
  includeMetadata?: boolean;
130
- enforceSingletonQueueActiveLimit?: boolean;
131
126
  } & ConnectionOptions;
132
127
 
133
128
  interface WorkHandler<ReqData> {
@@ -183,7 +178,7 @@ declare namespace PgBoss {
183
178
 
184
179
  interface JobWithMetadata<T = object> extends Job<T> {
185
180
  priority: number;
186
- state: 'created' | 'retry' | 'active' | 'completed' | 'expired' | 'cancelled' | 'failed';
181
+ state: 'created' | 'retry' | 'active' | 'completed' | 'cancelled' | 'failed';
187
182
  retrylimit: number;
188
183
  retrycount: number;
189
184
  retrydelay: number;
@@ -198,7 +193,7 @@ declare namespace PgBoss {
198
193
  createdon: Date;
199
194
  completedon: Date | null;
200
195
  keepuntil: Date;
201
- oncomplete: boolean,
196
+ deadletter: string,
202
197
  output: object
203
198
  }
204
199
 
@@ -214,7 +209,7 @@ declare namespace PgBoss {
214
209
  singletonKey?: string;
215
210
  expireInSeconds?: number;
216
211
  keepUntil?: Date | string;
217
- onComplete?: boolean
212
+ deadLetter?: string;
218
213
  }
219
214
 
220
215
  interface MonitorState {
@@ -223,7 +218,6 @@ declare namespace PgBoss {
223
218
  retry: number;
224
219
  active: number;
225
220
  completed: number;
226
- expired: number;
227
221
  cancelled: number;
228
222
  failed: number;
229
223
  }
@@ -236,7 +230,7 @@ declare namespace PgBoss {
236
230
  id: string,
237
231
  name: string,
238
232
  options: WorkOptions,
239
- state: 'created' | 'retry' | 'active' | 'completed' | 'expired' | 'cancelled' | 'failed',
233
+ state: 'created' | 'active' | 'stopping' | 'stopped'
240
234
  count: number,
241
235
  createdOn: Date,
242
236
  lastFetchedOn: Date,
@@ -250,7 +244,8 @@ declare namespace PgBoss {
250
244
  interface StopOptions {
251
245
  destroy?: boolean,
252
246
  graceful?: boolean,
253
- timeout?: number
247
+ timeout?: number,
248
+ wait?: boolean
254
249
  }
255
250
 
256
251
  interface OffWorkOptions {
@@ -300,10 +295,6 @@ declare class PgBoss extends EventEmitter {
300
295
  sendAfter(name: string, data: object, options: PgBoss.SendOptions, dateString: string): Promise<string | null>;
301
296
  sendAfter(name: string, data: object, options: PgBoss.SendOptions, seconds: number): Promise<string | null>;
302
297
 
303
- sendOnce(name: string, data: object, options: PgBoss.SendOptions, key: string): Promise<string | null>;
304
-
305
- sendSingleton(name: string, data: object, options: PgBoss.SendOptions): Promise<string | null>;
306
-
307
298
  sendThrottled(name: string, data: object, options: PgBoss.SendOptions, seconds: number): Promise<string | null>;
308
299
  sendThrottled(name: string, data: object, options: PgBoss.SendOptions, seconds: number, key: string): Promise<string | null>;
309
300
 
@@ -320,9 +311,6 @@ declare class PgBoss extends EventEmitter {
320
311
  work<ReqData>(name: string, options: PgBoss.BatchWorkOptions & { includeMetadata: true }, handler: PgBoss.BatchWorkWithMetadataHandler<ReqData>): Promise<string>;
321
312
  work<ReqData>(name: string, options: PgBoss.BatchWorkOptions, handler: PgBoss.BatchWorkHandler<ReqData>): Promise<string>;
322
313
 
323
- onComplete(name: string, handler: Function): Promise<string>;
324
- onComplete(name: string, options: PgBoss.WorkOptions, handler: Function): Promise<string>;
325
-
326
314
  offWork(name: string): Promise<void>;
327
315
  offWork(options: PgBoss.OffWorkOptions): Promise<void>;
328
316
 
@@ -338,43 +326,37 @@ declare class PgBoss extends EventEmitter {
338
326
  publish(event: string, data: object): Promise<string[]>;
339
327
  publish(event: string, data: object, options: PgBoss.SendOptions): Promise<string[]>;
340
328
 
341
- offComplete(name: string): Promise<void>;
342
- offComplete(options: PgBoss.OffWorkOptions): Promise<void>;
343
-
344
329
  fetch<T>(name: string): Promise<PgBoss.Job<T> | null>;
345
330
  fetch<T>(name: string, batchSize: number): Promise<PgBoss.Job<T>[] | null>;
346
331
  fetch<T>(name: string, batchSize: number, options: PgBoss.FetchOptions & { includeMetadata: true }): Promise<PgBoss.JobWithMetadata<T>[] | null>;
347
332
  fetch<T>(name: string, batchSize: number, options: PgBoss.FetchOptions): Promise<PgBoss.Job<T>[] | null>;
348
333
 
349
- fetchCompleted<T>(name: string): Promise<PgBoss.Job<T> | null>;
350
- fetchCompleted<T>(name: string, batchSize: number): Promise<PgBoss.Job<T>[] | null>;
351
- fetchCompleted<T>(name: string, batchSize: number, options: PgBoss.FetchOptions & { includeMetadata: true }): Promise<PgBoss.JobWithMetadata<T>[] | null>;
352
- fetchCompleted<T>(name: string, batchSize: number, options: PgBoss.FetchOptions): Promise<PgBoss.Job<T>[] | null>;
353
-
354
- cancel(id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
355
- cancel(ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
334
+ cancel(name: string, id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
335
+ cancel(name: string, ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
356
336
 
357
- resume(id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
358
- resume(ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
337
+ resume(name: string, id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
338
+ resume(name: string, ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
359
339
 
360
- complete(id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
361
- complete(id: string, data: object, options?: PgBoss.ConnectionOptions): Promise<void>;
362
- complete(ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
340
+ complete(name: string, id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
341
+ complete(name: string, id: string, data: object, options?: PgBoss.ConnectionOptions): Promise<void>;
342
+ complete(name: string, ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
363
343
 
364
- fail(id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
365
- fail(id: string, data: object, options?: PgBoss.ConnectionOptions): Promise<void>;
366
- fail(ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
344
+ fail(name: string, id: string, options?: PgBoss.ConnectionOptions): Promise<void>;
345
+ fail(name: string, id: string, data: object, options?: PgBoss.ConnectionOptions): Promise<void>;
346
+ fail(name: string, ids: string[], options?: PgBoss.ConnectionOptions): Promise<void>;
367
347
 
368
348
  getQueueSize(name: string, options?: object): Promise<number>;
369
- getJobById(id: string, options?: PgBoss.ConnectionOptions): Promise<PgBoss.JobWithMetadata | null>;
349
+ getJobById(name: string, id: string, options?: PgBoss.ConnectionOptions): Promise<PgBoss.JobWithMetadata | null>;
370
350
 
351
+ createQueue(name: string, policy: 'standard' | 'short' | 'singleton' | 'stately'): Promise<void>;
371
352
  deleteQueue(name: string): Promise<void>;
372
- deleteAllQueues(): Promise<void>;
353
+ purgeQueue(name: string): Promise<void>;
373
354
  clearStorage(): Promise<void>;
374
355
 
375
356
  archive(): Promise<void>;
376
357
  purge(): Promise<void>;
377
358
  expire(): Promise<void>;
359
+ maintain(): Promise<void>;
378
360
 
379
361
  schedule(name: string, cron: string, data?: object, options?: PgBoss.ScheduleOptions): Promise<void>;
380
362
  unschedule(name: string): Promise<void>;
package/version.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
- "schema": 20
2
+ "schema": 21
3
3
  }