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

fix: refactor error handling #7

Merged
merged 2 commits into from
Aug 30, 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
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