A progressive Node.js framework for building efficient and scalable server-side applications.
$ npm i --save nestjs-agenda-module agenda
Once the installation process is complete, we can import the AgendaModule into the root AppModule.
//app.module.ts
import { Module } from '@nestjs/common';
import { AgendaModule } from 'nestjs-agenda-module';
@Module({
imports: [
AgendaModule.forRoot({
db: { address: 'MONGO_CONNECTION_URI' },
}),
],
})
export class AppModule {}
Before you can use a job, you must define its processing behavior.
For do this you need create processors-definer
//example.processors-definer.ts
import { ProcessorsDefiner } from 'nestjs-agenda-module';
@ProcessorsDefiner()
export class ExampleProcessorsDefiner {}
And then you can define processor
with special decorator
@Processor(jobName, [options])
//example.processors-definer.ts
import { ProcessorsDefiner, Processor } from 'nestjs-agenda-module';
@ProcessorsDefiner()
export class ExampleProcessorsDefiner {
@Processor("EXAMPLE_JOB")
public async exampleJob() {}
}
Also you can get access to current job
data or done
function with job context
//example.processors-definer.ts
import { Job } from 'agenda';
import {
ProcessorsDefiner,
Processor,
Context,
JobContext
} from 'nestjs-agenda-module';
interface ExampleJobData {
message: string;
}
@ProcessorsDefiner()
export class ExampleProcessorsDefiner {
@Processor('EXAMPLE_JOB')
public async exampleJob(
@Context() context: JobContext<ExampleJobData>,
) {
const job: Job<ExampleJobData> = context.job;
const done: Function = context.done;
console.log(job.attrs.data.message);
done();
}
}
//app.module.ts
import { Module } from '@nestjs/common';
import { AgendaModule } from 'nestjs-agenda-module';
import { ExampleProcessorsDefiner } from './example.processors-definer.ts';
@Module({
imports: [
AgendaModule.forRoot({
db: { address: 'MONGO_CONNECTION_URI' },
}),
],
providers: [ExampleProcessorsDefiner],
})
export class AppModule {}
//example.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { Agenda } from 'agenda';
import { InjectAgenda } from 'nestjs-agenda-module';
@Injectable()
export class ExampleService {
public constructor(
@InjectAgenda()
private readonly agenda: Agenda,
) {}
public async createJob(): Promise<void> {
this.agenda.every('15 minutes', 'EXAMPLE_JOB', { message: 'text' }, { skipImmediate: true });
this.agenda.schedule('1 day', 'EXAMPLE_JOB', { message: 'text' });
this.agenda.now('EXAMPLE_JOB', { message: 'text' });
//etc
}
}
You have every
schedule
and now
decorators who provided default agenda behavior for create job. You can provide default data or job options like in default agenda. Just read documentation
//example.processors-definer.ts
import {
ProcessorsDefiner,
Processor,
Context,
JobContext,
Now,
Schedule,
Every,
} from 'nestjs-agenda-module';
@ProcessorsDefiner()
export class ExampleProcessorsDefiner {
@Processor('EXAMPLE_JOB_1')
@Every('15 minutes', { skipImmediate: true }, { message: 'test' })
public async exampleJob1(@Context() context: JobContext) {
done();
}
@Processor('EXAMPLE_JOB_2')
@Schedule('tomorrow at noon', { message: 'test' })
public async exampleJob2(@Context() context: JobContext) {
done();
}
@Processor('EXAMPLE_JOB_3')
@Now({ message: 'test' })
public async exampleJob3(@Context() context: JobContext) {
done();
}
}