Home
Softono

Nestjs Agenda

Open source MIT TypeScript
22
Stars
13
Forks
10
Issues
1
Watchers
2 years
Last Commit

 About Nestjs Agenda

Agenda for Nest.js, a NestJS distributed job scheduler based on MongoDB

Platforms

Web Self-hosted

Languages

TypeScript

Links

Need Help Installing Nestjs Agenda?

We provide expert installation service for this software. Our team will install, configure, and secure Nestjs Agenda on your server. plans start at just $30.

Nestjs Agenda

View on GitHub

nestjs-agenda

Agenda module for Nestjs

Agenda version is ^3.1.0

Installation

npm install nestjs-agenda

Usage

1. Import AgendaModule:

Sync register:

import { AgendaModule } from 'nestjs-agenda';

@Module({
  imports: [AgendaModule.register({ db: { address: 'mongodb://xxxxx' }})], // Same as configuring an agenda  
  providers: [...],
})
export class FooModule {}

Async register:

import { AgendaModule } from 'nestjs-agenda';

@Module({
  imports: [
    AgendaModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (config: ConfigService) => ({
        db: { address: config.get('MONGODB_URI') },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [...],
})
export class FooModule {}

2. Inject AgendaService (AgendaService is a instance of Agenda):

import { Injectable } from '@nestjs/common';
import { AgendaService } from 'nestjs-agenda';

@Injectable()
export class FooService {
  constructor(private readonly agenda: AgendaService) {
    // define a job, more details: [Agenda documentation](https://github.com/agenda/agenda)
    this.agenda.define('TEST_JOB', { lockLifetime: 10000 }, this.testJob.bind(this));
    // schedule a job
    this.agenda.schedule('10 seconds from now', 'TEST_JOB', {});
  }

  private async testJob(job: any, done: any): Promise<void> {
    console.log('a job');
    await job.remove();
    done();
  }
}