1.0.2 • Published 2 years ago

nest-job-scheduler v1.0.2

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

nest-scheduler

This library can be used to create scheduled jobs which run on a NestJS API. The underlying job scheduler used in this project is node-cron.

Setup:

Setting up this library requires you to perform the following steps: 1. Install node-cron in your project using npm i node-cron --save 2. Import the module in your project.

  import { Module } from '@nestjs/common';
  import { JobSchedulerModule } from 'nest-job-scheduler';
  import { AppSchedulersService } from './app-schedulers.service';

  import { AppController } from './app.controller';
  import { AppService } from './app.service';

  @Module({
    imports: [JobSchedulerModule], // <----- Import the module here
    controllers: [AppController],
    providers: [AppService, AppSchedulersService],
  })
  export class AppModule { }
  1. Import the scheduler service in one of your global service from where you will schedule jobs

      import { Injectable } from "@nestjs/common";
      import { JobSchedulerService } from "nest-job-scheduler";
      import { AppService } from "./app.service";
    
      @Injectable()
      export class AppSchedulersService {
        constructor(private scheduler: JobSchedulerService, // <----- Use DI to import the scheduler
          private readonly appService: AppService) {
          this.scheduler.enqueue({ // <----- Enqueue all the required jobs in the constructor
            name: 'Greeting', // <----- Name must be unique across all jobs to uniquely identify the job
            cron: '*/5 * * * * *', // <----- Schedule of the job
            enabled: true, // <------ Weather the job should be enabled by default
          },
            this.appService.getData.bind(this), // <----- Specify the function to be executed 
            true // <----- Should run once immidiately?
          );
        }
      }

Access the scheduled jobs via API

When you start the target application which imports this package, a controller endpoint will get added to the application.

All the available endpoints in this controller can be found in this Postman collection

1.0.2

2 years ago

1.0.1

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

1.0.0

2 years ago