Skip to content

Commit

Permalink
feat: migrate to pgboss 10
Browse files Browse the repository at this point in the history
  • Loading branch information
samaratungajs committed Sep 9, 2024
1 parent 04c0897 commit a115473
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 104 deletions.
2 changes: 1 addition & 1 deletion lib/handler-scanner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class HandlerScannerService {
await this.pgBossService.registerJob(
jobName,
boundHandler,
jobOptions as PgBoss.BatchWorkOptions,
jobOptions,
);
this.logger.log(`Registered job: ${jobName}`);
}
Expand Down
17 changes: 15 additions & 2 deletions lib/pgboss.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,33 @@ export class PgBossService {
await this.pgBoss.schedule(name, cron, data ?? {}, options ?? {});
await this.pgBoss.work<TData>(
name,
{ ...options, includeMetadata: true },
{ ...this.transformOptions(options), includeMetadata: true },
handler,
);
}

async registerJob<TData extends object>(
name: string,
handler: WorkWithMetadataHandler<TData>,
options?: PgBoss.BatchWorkOptions,
options?: PgBoss.WorkOptions,
) {
await this.pgBoss.work<TData>(
name,
{ ...options, includeMetadata: true },
handler,
);
}
private transformOptions(
options?: PgBoss.WorkOptions | PgBoss.ScheduleOptions,
) {
if (!options) return {};

const transformedOptions: any = { ...options };

if (typeof options.priority === "number") {
transformedOptions.priority = options.priority > 0;
}

return transformedOptions;
}
}
23 changes: 14 additions & 9 deletions lib/utils/conversion-helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { JobOptions, BatchWorkOptions } from "pg-boss";

export function convertToBatchWorkOptions(
jobOptions: JobOptions = {},
): BatchWorkOptions {
return {
...jobOptions,
batchSize: 1,
};
import PgBoss from "pg-boss";

export function transformOptions(
options?: PgBoss.WorkOptions | PgBoss.ScheduleOptions,
) {
if (!options) return {};

const transformedOptions: any = { ...options };

if (typeof options.priority === "number") {
transformedOptions.priority = options.priority > 0;
}

return transformedOptions;
}
97 changes: 8 additions & 89 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"peerDependencies": {
"@nestjs/common": "^9 || ^10",
"@nestjs/core": "^9 || ^10",
"pg-boss": "^9",
"pg-boss": "^10",
"reflect-metadata": "^0.1.13 || ^0.2.0",
"rxjs": "^7.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions test/pgboss.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe("PgBossService", () => {
describe("registerJob", () => {
it("should call PgBoss work with correct parameters", async () => {
const handler = jest.fn();
const options = { batchSize: 5 };
const options = {};

await service.registerJob("test-job", handler, options);

expect(mockPgBoss.work).toHaveBeenCalledWith(
"test-job",
{ batchSize: 5, includeMetadata: true },
{ includeMetadata: true },
handler,
);
});
Expand Down

0 comments on commit a115473

Please sign in to comment.