Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to pgboss 10 #15

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/handler-scanner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
import PgBoss, { WorkWithMetadataHandler } from "pg-boss";
import { LOGGER } from "./utils/consts";
import { normalizeJob } from "./utils/helpers";

@Injectable()
export class HandlerScannerService {
Expand Down Expand Up @@ -52,14 +53,15 @@ export class HandlerScannerService {
CRON_EXPRESSION,
methodRef,
);
const cronOptions = this.reflector.get<any>(CRON_OPTIONS, methodRef);
const cronOptions = this.reflector.get<PgBoss.ScheduleOptions>(
CRON_OPTIONS,
methodRef,
);

if (jobName) {
const boundHandler: WorkWithMetadataHandler<any> = async (job) => {
const jobData = {
...job,
};
await methodRef.call(instance, jobData);
const extractedJob = normalizeJob(job);
await methodRef.call(instance, extractedJob);
};
try {
if (cronExpression) {
Expand All @@ -75,7 +77,7 @@ export class HandlerScannerService {
await this.pgBossService.registerJob(
jobName,
boundHandler,
jobOptions as PgBoss.BatchWorkOptions,
jobOptions,
);
this.logger.log(`Registered job: ${jobName}`);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/pgboss.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from "@nestjs/common";
import PgBoss, { WorkWithMetadataHandler } from "pg-boss";
import { Inject } from "@nestjs/common";
import { PGBOSS_TOKEN } from "./utils/consts";
import { transformOptions } from "./utils/helpers";

@Injectable()
export class PgBossService {
Expand All @@ -20,6 +21,7 @@ export class PgBossService {
data: TData,
options?: PgBoss.SendOptions,
) {
await this.pgBoss.createQueue(name);
await this.pgBoss.send(name, data, options);
}

Expand All @@ -29,6 +31,7 @@ export class PgBossService {
data?: TData,
options?: PgBoss.ScheduleOptions,
) {
await this.pgBoss.createQueue(name);
await this.pgBoss.schedule(name, cron, data ?? {}, options ?? {});
}

Expand All @@ -39,19 +42,21 @@ export class PgBossService {
data?: TData,
options?: PgBoss.ScheduleOptions,
) {
await this.pgBoss.createQueue(name);
await this.pgBoss.schedule(name, cron, data ?? {}, options ?? {});
await this.pgBoss.work<TData>(
name,
{ ...options, includeMetadata: true },
{ ...transformOptions(options), includeMetadata: true },
handler,
);
}

async registerJob<TData extends object>(
name: string,
handler: WorkWithMetadataHandler<TData>,
options?: PgBoss.BatchWorkOptions,
options?: PgBoss.WorkOptions,
) {
await this.pgBoss.createQueue(name);
await this.pgBoss.work<TData>(
name,
{ ...options, includeMetadata: true },
Expand Down
10 changes: 0 additions & 10 deletions lib/utils/conversion-helper.ts

This file was deleted.

22 changes: 22 additions & 0 deletions lib/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;
}

export function normalizeJob(job: any) {
if (typeof job === "object" && "0" in job) {
return job[0];
}
return job;
}
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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wavezync/nestjs-pgboss",
"version": "2.2.0",
"version": "3.0.0",
"description": "A NestJS module that integrates pg-boss for job scheduling and handling.",
"license": "MIT",
"author": "[email protected]",
Expand All @@ -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
6 changes: 4 additions & 2 deletions test/pgboss.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("PgBossService", () => {
work: jest.fn(),
send: jest.fn(),
schedule: jest.fn(),
createQueue: jest.fn(),
getQueue: jest.fn(),
} as any;

const module: TestingModule = await Test.createTestingModule({
Expand All @@ -32,13 +34,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