@wavezync/nestjs-pgboss 3.0.0 → 4.0.0

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/README.MD CHANGED
@@ -78,8 +78,8 @@ export class MyJobHandler {
78
78
  private readonly logger = new Logger(MyJobHandler.name);
79
79
 
80
80
  @Job('my-job')
81
- async handleMyJob(job: { data: any }) {
82
- this.logger.log('Handling job with data:', job.data);
81
+ async handleMyJob(jobs: any[]) {
82
+ this.logger.log(`Handling ${jobs.length} job(s)`);
83
83
  }
84
84
  }
85
85
 
@@ -1,8 +1,10 @@
1
- import { JobOptions } from "pg-boss";
1
+ import { JobOptions, WorkOptions } from "pg-boss";
2
2
  export declare const JOB_NAME = "JOB_NAME";
3
3
  export declare const JOB_OPTIONS = "JOB_OPTIONS";
4
4
  export declare const CRON_EXPRESSION = "CRON_EXPRESSION";
5
5
  export declare const CRON_OPTIONS = "CRON_OPTIONS";
6
6
  export declare const PG_BOSS_JOB_METADATA = "PG_BOSS_JOB_METADATA";
7
- export declare function Job<_TData extends object = any>(name: string, options?: JobOptions): (target: any, key: string, descriptor: PropertyDescriptor) => void;
7
+ export declare const WORK_NAME = "WORK_NAME";
8
+ export declare const WORK_OPTIONS = "WORK_OPTIONS";
9
+ export declare function Job<_TData extends object = any>(name: string, options?: WorkOptions): (target: any, key: string, descriptor: PropertyDescriptor) => void;
8
10
  export declare function CronJob<_TData extends object = any>(name: string, cron: string, options?: JobOptions): (target: any, key: string, descriptor: PropertyDescriptor) => void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PG_BOSS_JOB_METADATA = exports.CRON_OPTIONS = exports.CRON_EXPRESSION = exports.JOB_OPTIONS = exports.JOB_NAME = void 0;
3
+ exports.WORK_OPTIONS = exports.WORK_NAME = exports.PG_BOSS_JOB_METADATA = exports.CRON_OPTIONS = exports.CRON_EXPRESSION = exports.JOB_OPTIONS = exports.JOB_NAME = void 0;
4
4
  exports.Job = Job;
5
5
  exports.CronJob = CronJob;
6
6
  const common_1 = require("@nestjs/common");
@@ -9,6 +9,8 @@ exports.JOB_OPTIONS = "JOB_OPTIONS";
9
9
  exports.CRON_EXPRESSION = "CRON_EXPRESSION";
10
10
  exports.CRON_OPTIONS = "CRON_OPTIONS";
11
11
  exports.PG_BOSS_JOB_METADATA = "PG_BOSS_JOB_METADATA";
12
+ exports.WORK_NAME = "WORK_NAME";
13
+ exports.WORK_OPTIONS = "WORK_OPTIONS";
12
14
  function Job(name, options = {}) {
13
15
  return (target, key, descriptor) => {
14
16
  (0, common_1.SetMetadata)(exports.JOB_NAME, name)(target, key, descriptor);
@@ -1 +1 @@
1
- {"version":3,"file":"job.decorator.js","sourceRoot":"","sources":["../../lib/decorators/job.decorator.ts"],"names":[],"mappings":";;;AASA,kBAaC;AAED,0BAeC;AAvCD,2CAA6C;AAGhC,QAAA,QAAQ,GAAG,UAAU,CAAC;AACtB,QAAA,WAAW,GAAG,aAAa,CAAC;AAC5B,QAAA,eAAe,GAAG,iBAAiB,CAAC;AACpC,QAAA,YAAY,GAAG,cAAc,CAAC;AAC9B,QAAA,oBAAoB,GAAG,sBAAsB,CAAC;AAE3D,SAAgB,GAAG,CACjB,IAAY,EACZ,UAAsB,EAAE;IAExB,OAAO,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B,EAAE,EAAE;QAClE,IAAA,oBAAW,EAAC,gBAAQ,EAAE,IAAI,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QACrD,IAAA,oBAAW,EAAC,mBAAW,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC3D,IAAA,oBAAW,EAAC,4BAAoB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,CACxE,MAAM,EACN,GAAG,EACH,UAAU,CACX,CAAC;IACJ,CAAC,CAAC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,EAAC,mBAAW,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC3D,IAAA,oBAAW,EAAC,4BAAoB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,CACxE,MAAM,EACN,GAAG,EACH,UAAU,CACX,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,SAAgB,OAAO,CACrB,IAAY,EACZ,IAAY,EACZ,UAAsB,EAAE;IAExB,OAAO,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B,EAAE,EAAE;QAClE,IAAA,oBAAW,EAAC,gBAAQ,EAAE,IAAI,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QACrD,IAAA,oBAAW,EAAC,uBAAe,EAAE,IAAI,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC5D,IAAA,oBAAW,EAAC,oBAAY,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC5D,IAAA,oBAAW,EAAC,4BAAoB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,CACxE,MAAM,EACN,GAAG,EACH,UAAU,CACX,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -15,7 +15,6 @@ const core_1 = require("@nestjs/core");
15
15
  const pgboss_service_1 = require("./pgboss.service");
16
16
  const job_decorator_1 = require("./decorators/job.decorator");
17
17
  const consts_1 = require("./utils/consts");
18
- const helpers_1 = require("./utils/helpers");
19
18
  let HandlerScannerService = class HandlerScannerService {
20
19
  constructor(pgBossService, reflector, modulesContainer) {
21
20
  this.pgBossService = pgBossService;
@@ -44,22 +43,17 @@ let HandlerScannerService = class HandlerScannerService {
44
43
  const cronExpression = this.reflector.get(job_decorator_1.CRON_EXPRESSION, methodRef);
45
44
  const cronOptions = this.reflector.get(job_decorator_1.CRON_OPTIONS, methodRef);
46
45
  if (jobName) {
47
- const boundHandler = async (job) => {
48
- const extractedJob = (0, helpers_1.normalizeJob)(job);
49
- await methodRef.call(instance, extractedJob);
50
- };
51
46
  try {
52
47
  if (cronExpression) {
53
- await this.pgBossService.registerCronJob(jobName, cronExpression, boundHandler, {}, cronOptions);
48
+ await this.pgBossService.registerCronJob(jobName, cronExpression, methodRef.bind(instance), {}, cronOptions);
54
49
  this.logger.log(`Registered cron job: ${jobName}`);
50
+ continue;
55
51
  }
56
- else {
57
- await this.pgBossService.registerJob(jobName, boundHandler, jobOptions);
58
- this.logger.log(`Registered job: ${jobName}`);
59
- }
52
+ await this.pgBossService.registerJob(jobName, methodRef.bind(instance), jobOptions);
53
+ this.logger.log(`Registered job: ${jobName}`);
60
54
  }
61
55
  catch (error) {
62
- this.logger.error(`Error registering job ${jobName}:`, error);
56
+ this.logger.error(error, `Error registering job ${jobName}`);
63
57
  }
64
58
  }
65
59
  }
@@ -1 +1 @@
1
- {"version":3,"file":"handler-scanner.service.js","sourceRoot":"","sources":["../lib/handler-scanner.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAoD;AACpD,uCAA2D;AAC3D,qDAAiD;AACjD,8DAKoC;AAGpC,2CAAwC;AACxC,6CAA+C;AAGxC,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAGhC,YACmB,aAA4B,EAC5B,SAAoB,EACpB,gBAAkC;QAFlC,kBAAa,GAAb,aAAa,CAAe;QAC5B,cAAS,GAAT,SAAS,CAAW;QACpB,qBAAgB,GAAhB,gBAAgB,CAAkB;QALpC,WAAM,GAAG,IAAI,eAAM,CAAC,eAAM,CAAC,CAAC;IAM1C,CAAC;IAEJ,KAAK,CAAC,uBAAuB;QAC3B,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC;YACpD,MAAM,SAAS,GAAG,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,M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AAQ,EAAE,YAAY,CAAC,CAAC;gBAC/C,CAAC,CAAC;gBACF,IAAI,CAAC;oBACH,IAAI,cAAc,EAAE,CAAC;wBACnB,MAAM,IAAI,CAAC,aAAa,CAAC,eAAe,CACtC,OAAO,EACP,cAAc,EACd,YAAY,EACZ,EAAE,EACF,WAAW,CACZ,CAAC;wBACF,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,wBAAwB,OAAO,EAAE,CAAC,CAAC;oBACrD,CAAC;yBAAM,CAAC;wBACN,MAAM,IAAI,CAAC,aAAa,CAAC,WAAW,CAClC,OAAO,EACP,YAAY,EACZ,UAAU,CACX,CAAC;wBACF,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,mBAAmB,OAAO,EAAE,CAAC,CAAC;oBAChD,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;gBAChE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AA1EY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,mBAAU,GAAE;qCAKuB,8BAAa;QACjB,gBAAS;QACF,uBAAgB;GAN1C,qBAAqB,CA0EjC"}
1
+ {"version":3,"file":"handler-scanner.service.js","sourceRoot":"","sources":["../lib/handler-scanner.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAoD;AACpD,uCAA2D;AAC3D,qDAAiD;AACjD,8DAKoC;AAGpC,2CAAwC;AAGjC,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAGhC,YACmB,aAA4B,EAC5B,SAAoB,EACpB,gBAAkC;QAFlC,kBAAa,GAAb,aAAa,CAAe;QAC5B,cAAS,GAAT,SAAS,CAAW;QACpB,qBAAgB,GAAhB,gBAAgB,CAAkB;QALpC,WAAM,GAAG,IAAI,eAAM,CAAC,eAAM,CAAC,CAAC;IAM1C,CAAC;IAEJ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E,CAAC;gBACZ,IAAI,CAAC;oBACH,IAAI,cAAc,EAAE,CAAC;wBACnB,MAAM,IAAI,CAAC,aAAa,CAAC,eAAe,CACtC,OAAO,EACP,cAAc,EACd,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EACxB,EAAE,EACF,WAAW,CACZ,CAAC;wBACF,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,wBAAwB,OAAO,EAAE,CAAC,CAAC;wBACnD,SAAS;oBACX,CAAC;oBAED,MAAM,IAAI,CAAC,aAAa,CAAC,WAAW,CAClC,OAAO,EACP,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EACxB,UAAU,CACX,CAAC;oBACF,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,mBAAmB,OAAO,EAAE,CAAC,CAAC;gBAChD,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,yBAAyB,OAAO,EAAE,CAAC,CAAC;gBAC/D,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AAxEY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,mBAAU,GAAE;qCAKuB,8BAAa;QACjB,gBAAS;QACF,uBAAgB;GAN1C,qBAAqB,CAwEjC"}
@@ -3,13 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleRetry = handleRetry;
4
4
  const rxjs_1 = require("rxjs");
5
5
  const operators_1 = require("rxjs/operators");
6
+ const consts_1 = require("./consts");
7
+ const common_1 = require("@nestjs/common");
6
8
  function handleRetry(retryAttempts = 9, retryDelay = 3000, verbose = false, toRetry = (_err) => true) {
7
- return (source) => source.pipe((0, operators_1.retryWhen)((attempts) => attempts.pipe((0, operators_1.take)(retryAttempts), (0, operators_1.mergeMap)((error, index) => {
9
+ const logger = new common_1.Logger(consts_1.LOGGER);
10
+ return (source) => source.pipe((0, operators_1.retryWhen)((attempts) => attempts.pipe((0, operators_1.mergeMap)((error, index) => {
8
11
  const includeError = toRetry(error);
9
- if (verbose && includeError) {
10
- console.warn(`Attempt ${index + 1}: Retrying in ${retryDelay / 1000} seconds...`);
12
+ if (includeError) {
13
+ if (verbose) {
14
+ logger.warn(`Attempt ${index + 1}: Retrying in ${retryDelay / 1000} seconds...`);
15
+ }
16
+ if (index + 1 >= retryAttempts) {
17
+ return (0, rxjs_1.throwError)(() => new Error(error.message));
18
+ }
19
+ return (0, rxjs_1.of)(error).pipe((0, operators_1.delay)(retryDelay));
11
20
  }
12
- return includeError ? (0, rxjs_1.of)(error) : (0, rxjs_1.throwError)(error);
13
- }), (0, operators_1.delay)(retryDelay))));
21
+ return (0, rxjs_1.throwError)(() => error);
22
+ }))));
14
23
  }
15
24
  //# sourceMappingURL=handleRetry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"handleRetry.js","sourceRoot":"","sources":["../../lib/utils/handleRetry.ts"],"names":[],"mappings":";;AAGA,kCA0BC;AA7BD,+BAAsC;AACtC,8CAAkE;AAElE,SAAgB,WAAW,CACzB,aAAa,GAAG,CAAC,EACjB,UAAU,GAAG,IAAI,EACjB,OAAO,GAAG,KAAK,EACf,UAAiC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI;IAEpD,OAAO,CAAI,MAAoC,EAAE,EAAE,CACjD,MAAM,CAAC,IAAI,CACT,IAAA,qBAAS,EAAC,CAAC,QAAQ,EAAE,EAAE,CACrB,QAAQ,CAAC,IAAI,CACX,IAAA,gBAAI,EAAC,aAAa,CAAC,EACnB,IAAA,oBAAQ,EAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QACxB,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;QACpC,IAAI,OAAO,IAAI,YAAY,EAAE,CAAC;YAC5B,OAAO,CAAC,IAAI,CACV,WAAW,KAAK,GAAG,CAAC,iBAClB,UAAU,GAAG,IACf,aAAa,CACd,CAAC;QACJ,CAAC;QACD,OAAO,YAAY,CA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AAC;QAC3C,CAAC;QAED,OAAO,IAAA,iBAAU,EAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CACH,CACF,CACF,CAAC;AACN,CAAC"}
@@ -1,15 +1,17 @@
1
1
  import { SetMetadata } from "@nestjs/common";
2
- import { JobOptions } from "pg-boss";
2
+ import { JobOptions, WorkOptions } from "pg-boss";
3
3
 
4
4
  export const JOB_NAME = "JOB_NAME";
5
5
  export const JOB_OPTIONS = "JOB_OPTIONS";
6
6
  export const CRON_EXPRESSION = "CRON_EXPRESSION";
7
7
  export const CRON_OPTIONS = "CRON_OPTIONS";
8
8
  export const PG_BOSS_JOB_METADATA = "PG_BOSS_JOB_METADATA";
9
+ export const WORK_NAME = "WORK_NAME";
10
+ export const WORK_OPTIONS = "WORK_OPTIONS";
9
11
 
10
12
  export function Job<_TData extends object = any>(
11
13
  name: string,
12
- options: JobOptions = {},
14
+ options: WorkOptions = {},
13
15
  ) {
14
16
  return (target: any, key: string, descriptor: PropertyDescriptor) => {
15
17
  SetMetadata(JOB_NAME, name)(target, key, descriptor);
@@ -8,9 +8,8 @@ import {
8
8
  CRON_OPTIONS,
9
9
  } from "./decorators/job.decorator";
10
10
  import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
11
- import PgBoss, { WorkWithMetadataHandler } from "pg-boss";
11
+ import PgBoss from "pg-boss";
12
12
  import { LOGGER } from "./utils/consts";
13
- import { normalizeJob } from "./utils/helpers";
14
13
 
15
14
  @Injectable()
16
15
  export class HandlerScannerService {
@@ -44,6 +43,7 @@ export class HandlerScannerService {
44
43
 
45
44
  for (const methodName of methodNames) {
46
45
  const methodRef = instance[methodName];
46
+
47
47
  const jobName = this.reflector.get<string>(JOB_NAME, methodRef);
48
48
  const jobOptions = this.reflector.get<PgBoss.WorkOptions>(
49
49
  JOB_OPTIONS,
@@ -59,30 +59,27 @@ export class HandlerScannerService {
59
59
  );
60
60
 
61
61
  if (jobName) {
62
- const boundHandler: WorkWithMetadataHandler<any> = async (job) => {
63
- const extractedJob = normalizeJob(job);
64
- await methodRef.call(instance, extractedJob);
65
- };
66
62
  try {
67
63
  if (cronExpression) {
68
64
  await this.pgBossService.registerCronJob(
69
65
  jobName,
70
66
  cronExpression,
71
- boundHandler,
67
+ methodRef.bind(instance),
72
68
  {},
73
69
  cronOptions,
74
70
  );
75
71
  this.logger.log(`Registered cron job: ${jobName}`);
76
- } else {
77
- await this.pgBossService.registerJob(
78
- jobName,
79
- boundHandler,
80
- jobOptions,
81
- );
82
- this.logger.log(`Registered job: ${jobName}`);
72
+ continue;
83
73
  }
74
+
75
+ await this.pgBossService.registerJob(
76
+ jobName,
77
+ methodRef.bind(instance),
78
+ jobOptions,
79
+ );
80
+ this.logger.log(`Registered job: ${jobName}`);
84
81
  } catch (error) {
85
- this.logger.error(`Error registering job ${jobName}:`, error);
82
+ this.logger.error(error, `Error registering job ${jobName}`);
86
83
  }
87
84
  }
88
85
  }
@@ -1,5 +1,7 @@
1
1
  import { of, throwError } from "rxjs";
2
- import { mergeMap, retryWhen, take, delay } from "rxjs/operators";
2
+ import { mergeMap, retryWhen, delay } from "rxjs/operators";
3
+ import { LOGGER } from "./consts";
4
+ import { Logger } from "@nestjs/common";
3
5
 
4
6
  export function handleRetry(
5
7
  retryAttempts = 9,
@@ -7,23 +9,33 @@ export function handleRetry(
7
9
  verbose = false,
8
10
  toRetry: (err: any) => boolean = (_err: any) => true,
9
11
  ) {
12
+ const logger = new Logger(LOGGER);
13
+
10
14
  return <T>(source: import("rxjs").Observable<T>) =>
11
15
  source.pipe(
12
16
  retryWhen((attempts) =>
13
17
  attempts.pipe(
14
- take(retryAttempts),
15
18
  mergeMap((error, index) => {
16
19
  const includeError = toRetry(error);
17
- if (verbose && includeError) {
18
- console.warn(
19
- `Attempt ${index + 1}: Retrying in ${
20
- retryDelay / 1000
21
- } seconds...`,
22
- );
20
+
21
+ if (includeError) {
22
+ if (verbose) {
23
+ logger.warn(
24
+ `Attempt ${index + 1}: Retrying in ${
25
+ retryDelay / 1000
26
+ } seconds...`,
27
+ );
28
+ }
29
+
30
+ if (index + 1 >= retryAttempts) {
31
+ return throwError(() => new Error(error.message));
32
+ }
33
+
34
+ return of(error).pipe(delay(retryDelay));
23
35
  }
24
- return includeError ? of(error) : throwError(error);
36
+
37
+ return throwError(() => error);
25
38
  }),
26
- delay(retryDelay),
27
39
  ),
28
40
  ),
29
41
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wavezync/nestjs-pgboss",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "A NestJS module that integrates pg-boss for job scheduling and handling.",
5
5
  "license": "MIT",
6
6
  "author": "samaratungajs@wavezync.com",