Skip to content

Commit

Permalink
Merge pull request #7 from wavezync/fix/samaratungajs/error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kasvith authored Aug 30, 2024
2 parents b88d5eb + 8b80784 commit 1c507e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
40 changes: 22 additions & 18 deletions lib/handler-scanner.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Injectable, Logger } from "@nestjs/common";
import { Reflector, ModulesContainer } from "@nestjs/core";
import { PgBossService } from "./pgboss.service";
import {
Expand All @@ -9,7 +8,8 @@ import {
} from "./decorators/job.decorator";
import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
import { BatchWorkOptions } from "pg-boss";
import { LOGGER } from "utils/consts";
import { LOGGER } from "./utils/consts";
import { Injectable, Logger } from "@nestjs/common";

@Injectable()
export class HandlerScannerService {
Expand Down Expand Up @@ -57,22 +57,26 @@ export class HandlerScannerService {
if (jobName) {
const boundHandler = methodRef.bind(instance);

if (cronExpression) {
await this.pgBossService.registerCronJob(
jobName,
cronExpression,
boundHandler,
{},
cronOptions,
);
this.logger.log(`Registered cron job: ${jobName}`);
} else {
await this.pgBossService.registerJob(
jobName,
boundHandler,
jobOptions,
);
this.logger.log(`Registered job: ${jobName}`);
try {
if (cronExpression) {
await this.pgBossService.registerCronJob(
jobName,
cronExpression,
boundHandler,
{},
cronOptions,
);
this.logger.log(`Registered cron job: ${jobName}`);
} else {
await this.pgBossService.registerJob(
jobName,
boundHandler,
jobOptions,
);
this.logger.log(`Registered job: ${jobName}`);
}
} catch (error) {
this.logger.error(`Error registering job ${jobName}:`);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/pgboss.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class PgBossModule
constructor(
@Inject(PGBOSS_TOKEN) private readonly boss: PgBoss,
private readonly handlerScannerService: HandlerScannerService,
) {}
) {
this.boss.on("error", (error: Error) => {
this.logger.error(`PgBoss error: ${error.message}`, error.stack);
});
}

static forRootAsync(options: PgBossModuleAsyncOptions): DynamicModule {
const logger = new Logger(LOGGER);
Expand All @@ -53,6 +57,9 @@ export class PgBossModule
),
),
);
boss.on("error", (error: Error) => {
logger.error(`PgBoss error: ${error.message}`, error.stack);
});
logger.log("PgBoss started successfully");
return boss;
},
Expand Down

0 comments on commit 1c507e5

Please sign in to comment.