nest-agenda-jobs
v0.2.3
Published
Bull tasks wrapper for NestJS framework
Downloads
12
Readme
Description
This is a Agenda task wrapper module for Nest.
Installation
$ npm i --save nest-agenda-jobs
Quick Start
import { Injectable } from '@nestjs/common';
import { Task } from '../lib';
@Injectable()
export class AppTasks {
@Task({ name: 'justATest' })
justATest(job: Agenda.Job, done) {
const result: number = (job.attrs.data || []).reduce((a, b) => a + b);
setTimeout(() => {
done(null, result);
}, 900);
}
}
import { Controller, Get } from '@nestjs/common';
import { AgendaService } from '../lib';
import { AppTasks } from './app.tasks';
@Controller('app')
export class AppController {
constructor(
private readonly agendaService: AgendaService,
private readonly tasks: AppTasks,
) {}
@Get()
public async runTask() {
const data = [1, 2, 3];
await this.agendaService.createJob(this.tasks.justATest, {
type: 'now',
autoRemove: false,
}, data);
return true;
}
}
import { Module, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { AgendaModule, AgendaTaskRegisterService, AgendaService } from '../lib';
import { AppTasks } from './app.tasks';
import { AppController } from './app.controller';
@Module({
imports: [AgendaModule],
controllers: [AppController],
providers: [AppTasks],
})
export class AppModule implements OnModuleInit {
constructor(
private readonly moduleRef: ModuleRef,
private readonly taskRegister: AgendaTaskRegisterService,
) {}
async onModuleInit() {
this.taskRegister.setModuleRef(this.moduleRef);
await this.taskRegister.register(AppTasks, {
collection: 'test',
options: {
db: {
address: 'mongodb://127.0.0.1/agenda',
options: {
useNewUrlParser: true,
},
},
},
});
}
}