Make sure that the env variable DATABASE_URI
is defined.
- Create an entrypoint that creates an NestJs application context instance that contains the
PgBossWorkerModule
.
@Module({
imports: [
AppModule.forRoot(),
PgBossWorkerModule.forRoot({
queueName, // The name of the queue to process
concurrency, // The number of jobs to process concurrently
batchSize, // The number of jobs to fetch
fetchRefreshThreshold, // Refresh threshold to fetch jobs
pollInterval // The interval (in milliseconds) to poll for new jobs
})
]
})
class WorkerModule {}
class Worker extends WorkerContainer {
async bootstrap (): Promise<INestApplicationContext> {
return await NestFactory.createApplicationContext(WorkerModule)
}
}
const _worker = new Worker()
- Create a type to define the data your job needs
export interface MyJobData extends BaseJobData {
uuid: string
// other data here
}
- Create a job definition
@PgBossJob('queue-name')
export class MyJob extends BaseJob<MyJobData> {}
- Create a job handler (make sure to provide it)
@Injectable()
@PgBossJobHandler(MyJob)
export class MyJobHandler extends JobHandler<MyJob> {
public async run (data: MyJobData): Promise<void> {
// Do stuff
}
}