nest-bull-jobs
v2.1.1
Published
Bull tasks wrapper for NestJS framework
Downloads
11
Maintainers
Readme
Description
This is a Bull task wrapper module for Nest.
Installation
$ npm i --save nest-bull-jobs bull
$ npm i --save-dev @types/bull
Quick Start
import { Injectable } from '@nestjs/common';
import Bull = require('bull');
import { Task } from '../lib';
@Injectable()
export class AppTasks {
@Task({ name: 'justATest' })
justATest(job: Bull.Job, done: Bull.DoneCallback) {
const result: number = (job.data || []).reduce((a, b) => a + b);
setTimeout(() => {
done(null, result);
}, 900);
}
}
import { Controller, Get } from '@nestjs/common';
import * as Bull from 'bull';
import { BullService } from '../lib';
import { AppTasks } from './app.tasks';
@Controller('app')
export class AppController {
constructor(
private readonly bullService: BullService,
private readonly tasks: AppTasks,
) {}
@Get()
public async runTask() {
const opt: Bull.JobOptions = { lifo: true };
const data = [1, 2, 3];
const result = await this.bullService.createJob(this.tasks.justATest, data, opt).then((job) => {
return job.finished();
});
return result;
}
}
import { Module, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { BullModule, BullTaskRegisterService } from '../lib';
import { AppTasks } from './app.tasks';
import { AppController } from './app.controller';
@Module({
imports: [BullModule],
controllers: [AppController],
providers: [AppTasks],
})
export class AppModule implements OnModuleInit {
constructor(
private readonly moduleRef: ModuleRef,
private readonly taskRegister: BullTaskRegisterService,
) {}
onModuleInit() {
this.taskRegister.setModuleRef(this.moduleRef);
this.taskRegister.register(AppTasks);
}
}