2.1.1 • Published 4 years ago

nest-bull-jobs v2.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

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);
  }
}
2.1.1

4 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago