Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vinks/nest-bull-jobs
Bull tasks wrapper for NestJS framework
https://github.com/vinks/nest-bull-jobs
Last synced: 19 days ago
JSON representation
Bull tasks wrapper for NestJS framework
- Host: GitHub
- URL: https://github.com/vinks/nest-bull-jobs
- Owner: vinks
- Created: 2018-12-05T13:25:13.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T02:37:23.000Z (almost 2 years ago)
- Last Synced: 2024-10-15T22:34:09.872Z (about 1 month ago)
- Language: TypeScript
- Size: 643 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Description
This is a [Bull](https://github.com/OptimalBits/bull) task wrapper module for [Nest](https://github.com/nestjs/nest).
## Installation
```bash
$ npm i --save nest-bull-jobs bull
$ npm i --save-dev @types/bull
```## Quick Start
```ts
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);
}
}
```