bullmq 6.3.7 → 6.3.9

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.
Files changed (56) hide show
  1. package/dist/cjs/classes/flow-producer.js +4 -4
  2. package/dist/cjs/classes/queue-base.js +5 -13
  3. package/dist/cjs/classes/queue-events-producer.js +5 -5
  4. package/dist/cjs/classes/queue-events.js +7 -10
  5. package/dist/cjs/classes/queue.js +1 -1
  6. package/dist/cjs/classes/worker.js +3 -3
  7. package/dist/cjs/index.js +1 -0
  8. package/dist/cjs/postgres/create-postgres-backend.js +3 -5
  9. package/dist/cjs/postgres/postgres-connection.js +15 -2
  10. package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
  11. package/dist/cjs/types/default-queue-options.js +2 -0
  12. package/dist/cjs/types/no-infer.js +2 -0
  13. package/dist/cjs/types/queue-type.js +2 -0
  14. package/dist/cjs/utils/create-backend.js +26 -8
  15. package/dist/cjs/utils/with-backend.js +54 -0
  16. package/dist/cjs/version.js +1 -1
  17. package/dist/esm/classes/flow-producer.d.ts +8 -3
  18. package/dist/esm/classes/flow-producer.js +4 -4
  19. package/dist/esm/classes/job-scheduler.d.ts +6 -3
  20. package/dist/esm/classes/job.d.ts +16 -10
  21. package/dist/esm/classes/queue-base.d.ts +12 -6
  22. package/dist/esm/classes/queue-base.js +5 -13
  23. package/dist/esm/classes/queue-events-producer.d.ts +9 -3
  24. package/dist/esm/classes/queue-events-producer.js +5 -5
  25. package/dist/esm/classes/queue-events.d.ts +9 -3
  26. package/dist/esm/classes/queue-events.js +8 -11
  27. package/dist/esm/classes/queue-getters.d.ts +2 -1
  28. package/dist/esm/classes/queue.d.ts +11 -8
  29. package/dist/esm/classes/queue.js +1 -1
  30. package/dist/esm/classes/worker.d.ts +10 -5
  31. package/dist/esm/classes/worker.js +3 -3
  32. package/dist/esm/index.d.ts +1 -0
  33. package/dist/esm/index.js +1 -0
  34. package/dist/esm/interfaces/minimal-queue.d.ts +3 -2
  35. package/dist/esm/interfaces/queue-backend.d.ts +5 -5
  36. package/dist/esm/interfaces/queue-options.d.ts +12 -9
  37. package/dist/esm/interfaces/worker-options.d.ts +2 -1
  38. package/dist/esm/postgres/create-postgres-backend.d.ts +5 -6
  39. package/dist/esm/postgres/create-postgres-backend.js +3 -5
  40. package/dist/esm/postgres/pg-types.d.ts +1 -3
  41. package/dist/esm/postgres/postgres-connection.js +15 -2
  42. package/dist/esm/postgres/postgres-queue-backend.d.ts +3 -3
  43. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  44. package/dist/esm/types/default-queue-options.d.ts +10 -0
  45. package/dist/esm/types/default-queue-options.js +1 -0
  46. package/dist/esm/types/no-infer.d.ts +5 -0
  47. package/dist/esm/types/no-infer.js +1 -0
  48. package/dist/esm/types/queue-type.d.ts +4 -0
  49. package/dist/esm/types/queue-type.js +1 -0
  50. package/dist/esm/utils/create-backend.d.ts +9 -5
  51. package/dist/esm/utils/create-backend.js +26 -8
  52. package/dist/esm/utils/with-backend.d.ts +36 -0
  53. package/dist/esm/utils/with-backend.js +51 -0
  54. package/dist/esm/version.d.ts +1 -1
  55. package/dist/esm/version.js +1 -1
  56. package/package.json +12 -11
@@ -15,13 +15,13 @@ const enums_1 = require("../enums");
15
15
  * All Jobs can be in different queues, either children or parents,
16
16
  */
17
17
  class FlowProducer extends events_1.EventEmitter {
18
- constructor(opts = { connection: {} }, backendFactory = (0, create_backend_1.getDefaultBackendFactory)()) {
18
+ constructor(opts, backendFactory) {
19
19
  super();
20
- this.opts = opts;
21
- this.opts = Object.assign({}, opts);
20
+ this.opts = Object.assign(opts === undefined ? { connection: {} } : {}, opts);
22
21
  // The flow producer is not bound to a single queue: each flow entry carries
23
22
  // its own queue identity, so the backend is created with an empty name.
24
- this.backend = backendFactory('', this.opts);
23
+ const factory = backendFactory !== null && backendFactory !== void 0 ? backendFactory : (0, create_backend_1.getDefaultBackendFactory)();
24
+ this.backend = factory('', this.opts);
25
25
  this.backend.on('error', (error) => {
26
26
  if (this.listenerCount('error') > 0) {
27
27
  this.emit('error', error);
@@ -11,23 +11,15 @@ const job_1 = require("./job");
11
11
  *
12
12
  */
13
13
  class QueueBase extends events_1.EventEmitter {
14
- /**
15
- *
16
- * @param name - The name of the queue.
17
- * @param opts - Options for the queue.
18
- * @param backendFactory - Factory used to build the {@link IQueueBackend}.
19
- * Defaults to the Redis backend; inject a different factory to use another
20
- * datastore or a test mock.
21
- */
22
- constructor(name, opts = { connection: {} }, backendFactory = (0, create_backend_1.getDefaultBackendFactory)(), hasBlockingConnection = false) {
14
+ constructor(name, opts, backendFactory, hasBlockingConnection = false) {
23
15
  super();
24
- this.name = name;
25
- this.opts = opts;
26
16
  this.closed = false;
27
17
  this.hasBlockingConnection = false;
28
- this.backendFactory = backendFactory;
18
+ this.name = name;
19
+ this.backendFactory =
20
+ backendFactory !== null && backendFactory !== void 0 ? backendFactory : (0, create_backend_1.getDefaultBackendFactory)();
29
21
  this.hasBlockingConnection = hasBlockingConnection;
30
- this.opts = Object.assign({}, opts);
22
+ this.opts = Object.assign(opts === undefined ? { connection: {} } : {}, opts);
31
23
  if (!name) {
32
24
  throw new Error('Queue name must be provided');
33
25
  }
@@ -7,11 +7,11 @@ const queue_base_1 = require("./queue-base");
7
7
  * The QueueEventsProducer class is used for publishing custom events.
8
8
  */
9
9
  class QueueEventsProducer extends queue_base_1.QueueBase {
10
- constructor(name, opts = {
11
- connection: {},
12
- }, backendFactory) {
13
- super(name, Object.assign({ blockingConnection: false }, opts), backendFactory);
14
- this.opts = opts;
10
+ constructor(name, opts, backendFactory) {
11
+ super(name, opts && Object.assign({ blockingConnection: false }, opts), backendFactory);
12
+ if (opts) {
13
+ this.opts = opts;
14
+ }
15
15
  }
16
16
  /**
17
17
  * Publish custom event to be processed in QueueEvents.
@@ -4,7 +4,6 @@ exports.QueueEvents = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const utils_1 = require("../utils");
6
6
  const queue_base_1 = require("./queue-base");
7
- const ioredis_client_1 = require("./ioredis-client");
8
7
  /**
9
8
  * The QueueEvents class is used for listening to the global events
10
9
  * emitted by a given queue.
@@ -13,15 +12,13 @@ const ioredis_client_1 = require("./ioredis-client");
13
12
  *
14
13
  */
15
14
  class QueueEvents extends queue_base_1.QueueBase {
16
- constructor(name, _a = {
17
- connection: {},
18
- }, backendFactory) {
19
- var { connection, autorun = true } = _a, opts = tslib_1.__rest(_a, ["connection", "autorun"]);
20
- super(name, Object.assign(Object.assign({}, opts), { connection: (0, utils_1.isRedisInstance)(connection)
21
- ? ((0, ioredis_client_1.isIRedisClient)(connection)
22
- ? connection
23
- : (0, ioredis_client_1.createIORedisClient)(connection)).duplicate()
24
- : connection }), backendFactory, true);
15
+ constructor(name, opts, backendFactory) {
16
+ const { autorun = true } = opts !== null && opts !== void 0 ? opts : {};
17
+ const queueOptions = opts && Object.assign({}, opts);
18
+ if (queueOptions) {
19
+ delete queueOptions.autorun;
20
+ }
21
+ super(name, queueOptions, backendFactory, true);
25
22
  this.running = false;
26
23
  this.blocking = false;
27
24
  this.opts = Object.assign({
@@ -36,7 +36,7 @@ const utils_1 = require("../utils");
36
36
  class Queue extends queue_getters_1.QueueGetters {
37
37
  constructor(name, opts, backendFactory) {
38
38
  var _a;
39
- super(name, Object.assign({}, opts), backendFactory);
39
+ super(name, opts, backendFactory);
40
40
  this.token = (0, utils_1.randomUUID)();
41
41
  this.libName = 'bullmq';
42
42
  this.jobsOpts = (_a = opts === null || opts === void 0 ? void 0 : opts.defaultJobOptions) !== null && _a !== void 0 ? _a : {};
@@ -37,6 +37,9 @@ class Worker extends queue_base_1.QueueBase {
37
37
  return new errors_1.RateLimitError();
38
38
  }
39
39
  constructor(name, processor, opts, backendFactory) {
40
+ if (!opts || opts.connection === undefined || opts.connection === null) {
41
+ throw new Error('Worker requires a connection');
42
+ }
40
43
  super(name, Object.assign(Object.assign({ drainDelay: 5, concurrency: 1, lockDuration: 30000, maximumRateLimitDelay: 30000, maxStalledCount: 1, stalledInterval: 30000, autorun: true, runRetryDelay: 15000 }, opts), { blockingConnection: true }), backendFactory);
41
44
  this.abortDelayController = null;
42
45
  this.blockUntil = 0;
@@ -47,9 +50,6 @@ class Worker extends queue_base_1.QueueBase {
47
50
  this.waiting = null;
48
51
  this.running = false;
49
52
  this.mainLoopRunning = null;
50
- if (!opts || !opts.connection) {
51
- throw new Error('Worker requires a connection');
52
- }
53
53
  if (typeof this.opts.maxStalledCount !== 'number' ||
54
54
  this.opts.maxStalledCount < 0) {
55
55
  throw new Error('maxStalledCount must be greater or equal than 0');
package/dist/cjs/index.js CHANGED
@@ -9,5 +9,6 @@ tslib_1.__exportStar(require("./utils"), exports);
9
9
  // to prevent circular references
10
10
  tslib_1.__exportStar(require("./types/processor"), exports);
11
11
  tslib_1.__exportStar(require("./utils/create-backend"), exports);
12
+ tslib_1.__exportStar(require("./utils/with-backend"), exports);
12
13
  // PostgreSQL backend (optional; requires the `pg` peer dependency at runtime).
13
14
  tslib_1.__exportStar(require("./postgres"), exports);
@@ -10,11 +10,9 @@ const postgres_queue_backend_1 = require("./postgres-queue-backend");
10
10
  * dedicated `LISTEN` client); the high-level classes depend only on
11
11
  * `IQueueBackend` and never touch a `pg` client directly.
12
12
  *
13
- * The `opts.connection` value is forwarded to {@link PostgresConnection} and may
14
- * be a connection string, a node-postgres pool config (optionally carrying a
15
- * `schema`), or an already-built `pg.Pool` instance. `pg` is lazily required
16
- * only when a config/string is passed, so Redis-only users never need it
17
- * installed.
13
+ * Accepts a connection string, pool config (optionally carrying a `schema`),
14
+ * or an existing `pg.Pool`. The `pg` driver is loaded lazily for config/string
15
+ * connections; Redis-only users do not need it installed.
18
16
  *
19
17
  * Inject this into the queue classes (or set it as the process-wide default via
20
18
  * `setDefaultBackendFactory(createPostgresBackend)`) to back BullMQ with
@@ -44,7 +44,10 @@ const LISTEN_KEEPALIVE_INITIAL_DELAY_MS = 10000;
44
44
  function enableSocketKeepAlive(client) {
45
45
  var _a;
46
46
  const stream = (_a = client.connection) === null || _a === void 0 ? void 0 : _a.stream;
47
- if (!stream || typeof stream.setKeepAlive !== 'function') {
47
+ if (!stream ||
48
+ typeof stream !== 'object' ||
49
+ !('setKeepAlive' in stream) ||
50
+ typeof stream.setKeepAlive !== 'function') {
48
51
  return false;
49
52
  }
50
53
  try {
@@ -188,7 +191,7 @@ class PostgresConnection extends events_1.EventEmitter {
188
191
  // callers racing on the first use don't each open a connection — the
189
192
  // `await` below is exactly where a second caller would otherwise slip in.
190
193
  if (!this.listenClientPromise) {
191
- this.listenClientPromise = (async () => {
194
+ const establishing = (async () => {
192
195
  if (this.pgModule && this.listenClientConfig) {
193
196
  const client = new this.pgModule.Client(Object.assign(Object.assign({}, this.listenClientConfig), {
194
197
  // A LISTEN subscription is bound to one physical connection. Enable
@@ -220,6 +223,16 @@ class PostgresConnection extends events_1.EventEmitter {
220
223
  return client;
221
224
  }
222
225
  })();
226
+ this.listenClientPromise = establishing;
227
+ // Memoize only a connect that succeeds. A rejected one (e.g. Postgres
228
+ // mid-restart) would otherwise be handed to every later caller, so the
229
+ // worker could never LISTEN again until the process restarts. Callers
230
+ // sharing this attempt still see its rejection.
231
+ establishing.catch(() => {
232
+ if (this.listenClientPromise === establishing) {
233
+ this.listenClientPromise = undefined;
234
+ }
235
+ });
223
236
  }
224
237
  return this.listenClientPromise;
225
238
  }