@rvoh/psychic-workers 0.4.1 → 0.4.3

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.
@@ -325,7 +325,6 @@ class Background {
325
325
  /////////////////////////
326
326
  const namedQueueOptionsMap = nativeBullMQ.namedQueueOptions || {};
327
327
  Object.keys(namedQueueOptionsMap).forEach(queueName => {
328
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
329
328
  const namedQueueOptions = namedQueueOptionsMap[queueName];
330
329
  if (namedQueueOptions.queueConnection)
331
330
  this.redisConnections.push(namedQueueOptions.queueConnection);
@@ -372,7 +371,12 @@ class Background {
372
371
  * starts background workers
373
372
  */
374
373
  work() {
375
- this.connect({ activateWorkers: true });
374
+ process.on('uncaughtException', (error) => {
375
+ psychic_1.PsychicApp.log('[psychic-workers] uncaughtException:', error);
376
+ });
377
+ process.on('unhandledRejection', (error) => {
378
+ psychic_1.PsychicApp.log('[psychic-workers] unhandledRejection:', error);
379
+ });
376
380
  process.on('SIGTERM', () => {
377
381
  if (!EnvInternal_js_1.default.isTest)
378
382
  psychic_1.PsychicApp.log('[psychic-workers] handle SIGTERM');
@@ -387,6 +391,7 @@ class Background {
387
391
  .then(() => { })
388
392
  .catch(() => { });
389
393
  });
394
+ this.connect({ activateWorkers: true });
390
395
  }
391
396
  /**
392
397
  * adds the static method of a provided class to BullMQ
@@ -425,7 +430,7 @@ class Background {
425
430
  * @param importKey - (optional) the import key for the class
426
431
  * @param jobConfig - (optional) the background job config to use when backgrounding this method
427
432
  */
428
- async scheduledMethod(ObjectClass, pattern, method, { globalName, args = [], jobConfig = {}, }) {
433
+ async scheduledMethod(ObjectClass, pattern, method, { globalName, args = [], jobConfig = {}, scheduleOpts = {}, }) {
429
434
  this.connect();
430
435
  // `jobId` is used to determine uniqueness along with name and repeat pattern.
431
436
  // Since the name is really a job type and never changes, the `jobId` is the only
@@ -433,22 +438,18 @@ class Background {
433
438
  // now be enforced by combining class name, method name, and cron repeat pattern.
434
439
  //
435
440
  // See: https://docs.bullmq.io/guide/jobs/repeatable
436
- const jobId = `${ObjectClass.name}:${method}`;
441
+ const schedulerId = `${globalName}:${method}`;
437
442
  const queueInstance = this.queueInstance(jobConfig);
438
443
  if (!queueInstance)
439
444
  throw new Error(`Missing queue for: ${jobConfig.queue?.toString()}`);
440
- await queueInstance.add('BackgroundJobQueueStaticJob', {
441
- globalName,
442
- method,
443
- args,
444
- }, {
445
- repeat: {
446
- pattern,
445
+ await queueInstance.upsertJobScheduler(schedulerId, { pattern }, {
446
+ name: schedulerId,
447
+ opts: scheduleOpts,
448
+ data: {
449
+ globalName,
450
+ method,
451
+ args,
447
452
  },
448
- jobId,
449
- group: this.jobConfigToGroup(jobConfig),
450
- priority: this.mapPriorityWordToPriorityNumber(this.jobConfigToPriority(jobConfig)),
451
- // explicitly typing as JobsOptions because Psychic can't be aware of BullMQ Pro options
452
453
  });
453
454
  }
454
455
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1,6 +1,6 @@
1
1
  import { closeAllDbConnections, compact, pascalize } from '@rvoh/dream';
2
2
  import { PsychicApp } from '@rvoh/psychic';
3
- import { Job, Queue, Worker } from 'bullmq';
3
+ import { Job, Queue, Worker, } from 'bullmq';
4
4
  import ActivatingBackgroundWorkersWithoutDefaultWorkerConnection from '../error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js';
5
5
  import ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection from '../error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js';
6
6
  import DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection from '../error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js';
@@ -321,7 +321,6 @@ export class Background {
321
321
  /////////////////////////
322
322
  const namedQueueOptionsMap = nativeBullMQ.namedQueueOptions || {};
323
323
  Object.keys(namedQueueOptionsMap).forEach(queueName => {
324
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
325
324
  const namedQueueOptions = namedQueueOptionsMap[queueName];
326
325
  if (namedQueueOptions.queueConnection)
327
326
  this.redisConnections.push(namedQueueOptions.queueConnection);
@@ -368,7 +367,12 @@ export class Background {
368
367
  * starts background workers
369
368
  */
370
369
  work() {
371
- this.connect({ activateWorkers: true });
370
+ process.on('uncaughtException', (error) => {
371
+ PsychicApp.log('[psychic-workers] uncaughtException:', error);
372
+ });
373
+ process.on('unhandledRejection', (error) => {
374
+ PsychicApp.log('[psychic-workers] unhandledRejection:', error);
375
+ });
372
376
  process.on('SIGTERM', () => {
373
377
  if (!EnvInternal.isTest)
374
378
  PsychicApp.log('[psychic-workers] handle SIGTERM');
@@ -383,6 +387,7 @@ export class Background {
383
387
  .then(() => { })
384
388
  .catch(() => { });
385
389
  });
390
+ this.connect({ activateWorkers: true });
386
391
  }
387
392
  /**
388
393
  * adds the static method of a provided class to BullMQ
@@ -421,7 +426,7 @@ export class Background {
421
426
  * @param importKey - (optional) the import key for the class
422
427
  * @param jobConfig - (optional) the background job config to use when backgrounding this method
423
428
  */
424
- async scheduledMethod(ObjectClass, pattern, method, { globalName, args = [], jobConfig = {}, }) {
429
+ async scheduledMethod(ObjectClass, pattern, method, { globalName, args = [], jobConfig = {}, scheduleOpts = {}, }) {
425
430
  this.connect();
426
431
  // `jobId` is used to determine uniqueness along with name and repeat pattern.
427
432
  // Since the name is really a job type and never changes, the `jobId` is the only
@@ -429,22 +434,18 @@ export class Background {
429
434
  // now be enforced by combining class name, method name, and cron repeat pattern.
430
435
  //
431
436
  // See: https://docs.bullmq.io/guide/jobs/repeatable
432
- const jobId = `${ObjectClass.name}:${method}`;
437
+ const schedulerId = `${globalName}:${method}`;
433
438
  const queueInstance = this.queueInstance(jobConfig);
434
439
  if (!queueInstance)
435
440
  throw new Error(`Missing queue for: ${jobConfig.queue?.toString()}`);
436
- await queueInstance.add('BackgroundJobQueueStaticJob', {
437
- globalName,
438
- method,
439
- args,
440
- }, {
441
- repeat: {
442
- pattern,
441
+ await queueInstance.upsertJobScheduler(schedulerId, { pattern }, {
442
+ name: schedulerId,
443
+ opts: scheduleOpts,
444
+ data: {
445
+ globalName,
446
+ method,
447
+ args,
443
448
  },
444
- jobId,
445
- group: this.jobConfigToGroup(jobConfig),
446
- priority: this.mapPriorityWordToPriorityNumber(this.jobConfigToPriority(jobConfig)),
447
- // explicitly typing as JobsOptions because Psychic can't be aware of BullMQ Pro options
448
449
  });
449
450
  }
450
451
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1,5 +1,5 @@
1
1
  import { Dream } from '@rvoh/dream';
2
- import { Job, Queue, Worker } from 'bullmq';
2
+ import { Job, JobSchedulerTemplateOptions, Queue, Worker } from 'bullmq';
3
3
  import { BackgroundJobConfig, BackgroundJobData, BackgroundQueuePriority, JobTypes } from '../types/background.js';
4
4
  /**
5
5
  * the underlying class driving the `background` singleton,
@@ -145,12 +145,11 @@ export declare class Background {
145
145
  * @param importKey - (optional) the import key for the class
146
146
  * @param jobConfig - (optional) the background job config to use when backgrounding this method
147
147
  */
148
- scheduledMethod(ObjectClass: Record<'name', string>, pattern: string, method: string, { globalName, args, jobConfig, }: {
148
+ scheduledMethod(ObjectClass: Record<'name', string>, pattern: string, method: string, { globalName, args, jobConfig, scheduleOpts, }: {
149
149
  globalName: string;
150
150
  args?: any[];
151
- filepath?: string;
152
- importKey?: string;
153
151
  jobConfig?: BackgroundJobConfig<any>;
152
+ scheduleOpts?: JobSchedulerTemplateOptions;
154
153
  }): Promise<void>;
155
154
  private queueInstance;
156
155
  /**
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@rvoh/psychic-workers",
4
4
  "description": "Background job system for Psychic applications",
5
- "version": "0.4.1",
5
+ "version": "0.4.3",
6
6
  "author": "RVO Health",
7
7
  "repository": {
8
8
  "type": "git",
@@ -53,7 +53,7 @@
53
53
  "@types/node": "^22.5.1",
54
54
  "@types/pg": "^8",
55
55
  "@types/supertest": "^6.0.2",
56
- "bullmq": "^5.49.1",
56
+ "bullmq": "^5.52.0",
57
57
  "eslint": "^9.9.1",
58
58
  "express": "^4.21.2",
59
59
  "ioredis": "^5.4.1",
@@ -73,4 +73,4 @@
73
73
  "vitest": "^3.1.1"
74
74
  },
75
75
  "packageManager": "yarn@4.7.0"
76
- }
76
+ }