Skip to content

Commit

Permalink
fix: update helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
samaratungajs committed Sep 10, 2024
1 parent 45b9b2b commit 393cd73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
10 changes: 2 additions & 8 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 @@ -59,7 +60,7 @@ export class HandlerScannerService {

if (jobName) {
const boundHandler: WorkWithMetadataHandler<any> = async (job) => {
const extractedJob = this.normalizeJob(job);
const extractedJob = normalizeJob(job);
await methodRef.call(instance, extractedJob);
};
try {
Expand All @@ -86,11 +87,4 @@ export class HandlerScannerService {
}
}
}

private normalizeJob(job: any) {
if (typeof job === "object" && "0" in job) {
return job[0];
}
return job;
}
}
16 changes: 2 additions & 14 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 Down Expand Up @@ -45,7 +46,7 @@ export class PgBossService {
await this.pgBoss.schedule(name, cron, data ?? {}, options ?? {});
await this.pgBoss.work<TData>(
name,
{ ...this.transformOptions(options), includeMetadata: true },
{ ...transformOptions(options), includeMetadata: true },
handler,
);
}
Expand All @@ -62,19 +63,6 @@ export class PgBossService {
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;
}

async ensureQueueExists(queueName: string) {
const currentQueue = await this.pgBoss.getQueue(queueName);
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/conversion-helper.ts → lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export function transformOptions(

return transformedOptions;
}

export function normalizeJob(job: any) {
if (typeof job === "object" && "0" in job) {
return job[0];
}
return job;
}

0 comments on commit 393cd73

Please sign in to comment.