Socket
Socket
Sign inDemoInstall

nest-schedule

Package Overview
Dependencies
68
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nest-schedule

Nest - modern, fast, powerful node.js web framework (@schedule)


Version published
Weekly downloads
2.1K
increased by14.47%
Maintainers
1
Install size
5.79 MB
Created
Weekly downloads
 

Readme

Source

Nest Logo

Nest Schedule

NPM Version Package License NPM Downloads

Description

This is a Nest module for using decorator schedule jobs.

Installation

$ npm i --save nest-schedule

Usage

import { Module } from '@nestjs/common';
import { ScheduleModule } from 'nest-schedule';

@Module({
  imports: [
    ScheduleModule.register(),
  ]
})
export class AppModule {
}
import { Injectable } from '@nestjs/common';
import { Cron, Interval, Timeout, NestSchedule } from 'nest-schedule';

@Injectable() // Only support SINGLETON scope
export class ScheduleService extends NestSchedule {    
  @Cron('0 0 2 * *', {
    startTime: new Date(), 
    endTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
  })
  async cronJob() {
    console.log('executing cron job');
  }
  
  @Timeout(5000)
  onceJob() {
    console.log('executing once job');
  }
  
  @Interval(2000)
  intervalJob() {
    console.log('executing interval job');
    
    // if you want to cancel the job, you should return true;
    return true;
  }
}

Dynamic Schedule Job

import { Injectable } from '@nestjs/common';
import { InjectSchedule, Schedule } from 'nest-schedule';

@Injectable()
export class ScheduleService {    
  constructor(
    @InjectSchedule() private readonly schedule: Schedule,
  ) {
  }
  
  createJob() {
    // schedule a 2s interval job
    this.schedule.scheduleIntervalJob('my-job', 2000, () => {
      console.log('executing interval job');
    });
  }
  
  cancelJob() {
    this.schedule.cancelJob('my-job');
  }
}

Distributed Support

1. Extend NestDistributedSchedule class
import { Injectable } from '@nestjs/common';
import { Cron, NestDistributedSchedule } from 'nest-schedule';

@Injectable()
export class ScheduleService extends NestDistributedSchedule {  
  constructor() {
    super();
  }
  
  async tryLock(method: string) {
    if (lockFail) {
      return false;
    }
    
    return () => {
      // Release here.
    }
  }
  
  @Cron('0 0 4 * *')
  async cronJob() {
    console.log('executing cron job');
  }
}
2. Use UseLocker decorator
import { ILocker, IScheduleConfig, InjectSchedule, Schedule } from 'nest-schedule';
import { Injectable } from '@nestjs/common';

// If use NestCloud, it supports dependency injection.
@Injectable()
export class MyLocker implements ILocker {
  private key: string;
  private config: IScheduleConfig;

  constructor(
    @InjectSchedule() private readonly schedule: Schedule,
  ) {
  }

  init(key: string, config: IScheduleConfig): void {
    this.key = key;
    this.config = config;
    console.log('init my locker: ', key, config);
  }

  release(): any {
    console.log('release my locker');
  }

  tryLock(): Promise<boolean> | boolean {
    console.log('apply my locker');
    return true;
  }
}
import { Injectable } from '@nestjs/common';
import { Cron, NestSchedule, UseLocker } from 'nest-schedule';
import { MyLocker } from './my.locker';

@Injectable()
export class ScheduleService extends NestSchedule {  
  @Cron('0 0 4 * *')
  @UseLocker(MyLocker)
  async cronJob() {
    console.log('executing cron job');
  }
}

API

class ScheduleModule

static register(config: IGlobalConfig): DynamicModule

Register schedule module.

fieldtyperequireddescription
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.waitingbooleanfalsethe scheduler will not schedule job when this job is running, if waiting is true

class Schedule

scheduleCronJob(key: string, cron: string, callback: JobCallback, config?: ICronJobConfig)

Schedule a cron job.

fieldtyperequireddescription
keystringtrueThe unique job key
cronstringtrueThe cron expression
callback() => Promise<boolean>booleanIf return true in callback function, the schedule will cancel this job immediately
config.startTimeDatefalseThe start time of this job
config.endTimeDatefalseThe end time of this job
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.waitingbooleanfalsethe scheduler will not schedule job when this job is running, if waiting is true
config.immediatebooleanfalserunning job immediately
scheduleIntervalJob(key: string, interval: number, callback: JobCallback, config?: IJobConfig)

Schedule a interval job.

fieldtyperequireddescription
keystringtrueThe unique job key
intervalnumbertruemilliseconds
callback() => Promise<boolean>booleanIf return true in callback function, the schedule will cancel this job immediately
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.waitingbooleanfalsethe scheduler will not schedule job when this job is running, if waiting is true
config.immediatebooleanfalserunning job immediately
scheduleTimeoutJob(key: string, timeout: number, callback: JobCallback, config?: IJobConfig)

Schedule a timeout job.

fieldtyperequireddescription
keystringtrueThe unique job key
timeoutnumbertruemilliseconds
callback() => Promise<boolean>booleanIf return true in callback function, the schedule will cancel this job immediately
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.immediatebooleanfalserunning job immediately
cancelJob(key: string)

Cancel job.

Decorators

Cron(expression: string, config?: ICronJobConfig): MethodDecorator

Schedule a cron job.

fieldtyperequireddescription
expressionstringtruethe cron expression
config.keystringfalseThe unique job key
config.startTimeDatefalsethe job's start time
config.endTimeDatefalsethe job's end time
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.waitingbooleanfalsethe scheduler will not schedule job when this job is running, if waiting is true
config.immediatebooleanfalserunning job immediately

Interval(milliseconds: number, config?: IJobConfig): MethodDecorator

Schedule a interval job.

fieldtyperequireddescription
millisecondsnumbertruemilliseconds
config.keystringfalseThe unique job key
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.waitingbooleanfalsethe scheduler will not schedule job when this job is running, if waiting is true
config.immediatebooleanfalserunning job immediately

Timeout(milliseconds: number, config?: IJobConfig): MethodDecorator

Schedule a timeout job.

fieldtyperequireddescription
millisecondsnumbertruemilliseconds
config.keystringfalseThe unique job key
config.enablebooleanfalsedefault is true, when false, the job will not execute
config.maxRetrynumberfalsethe max retry count, default is -1 not retry
config.retryIntervalnumberfalsethe retry interval, default is 5000
config.immediatebooleanfalserunning job immediately

InjectSchedule(): PropertyDecorator

Inject Schedule instance

UseLocker(locker: ILocker | Function): MethodDecorator

Make your job support distribution.

If you use NestCloud, the Locker will support dependency injection, or not use injection please.

Stay in touch

  • Author - miaowing

License

FAQs

Last updated on 26 Dec 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc