0.8.5 • Published 2 years ago

@nestcloud2/schedule v0.8.5

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

NestCloud - Schedule

Description

This schedule module is forked from @nestjs/schedule.

And add some new features:

  • Distributed supports that by using UseLocker() decorator.

  • Retryable Job.

  • Executing job immediately for Interval and Timeout job.

Installation

$ npm i --save @nestcloud2/schedule

Usage

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

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

@Injectable()
export class TasksService {
    private readonly logger = new Logger(TasksService.name);

    @Cron('45 * * * * *')
    handleCron() {
        this.logger.debug('Called when the current second is 45');
    }

    @Interval(5000)
    handleInterval() {
        this.logger.debug('Called every 5 seconds');
    }

    @Timeout(5000)
    handleTimeout() {
        this.logger.debug('Called after 5 seconds');
    }
}

Schedule Cron Job By Object Literal Syntax

@See node-schedule#object-literal-syntax

import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestcloud2/schedule';

@Injectable()
export class TasksService {
    private readonly logger = new Logger(TasksService.name);

    @Cron({
        rule: '45 * * * * *',
        start: new Date(Date.now() + 5000),
        end: new Date(Date.now() + 10000),
        tz: 'Asia/Shanghai',
    })
    handleCron() {
        this.logger.debug('Called when the time is Sunday 14:30');
    }
}

Schedule Cron Job With StartTime And EndTime

@See node-schedule#set-startTime-and-endTime

import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestcloud2/schedule';

@Injectable()
export class TasksService {
    private readonly logger = new Logger(TasksService.name);

    @Cron({ hour: 14, minute: 30, dayOfWeek: 0, tz: 'Asia/Shanghai' })
    handleCron() {
        this.logger.debug('Called when the current second is 45');
    }
}

Dynamic Schedule Job

import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { InjectSchedule, Schedule } from '@nestcloud2/schedule';

@Injectable()
export class TasksService implements OnModuleInit {
    private readonly logger = new Logger(TasksService.name);

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

    execute() {
        this.logger.debug('execute dynamic job');
    }

    onModuleInit() {
        this.schedule.createIntervalJob(this.execute.bind(this), 3000, { name: 'test_job' });
        this.schedule.deleteIntervalJob('test_job');
    }
}

Distributed Support

Dynamic job is not support distributed locker now.

  1. Implements Locker interface
import { Locker } from '@nestcloud2/schedule';
import { Injectable } from '@nestjs/common';

@Injectable()
export class TaskLocker implements Locker {
    private name: string;

    init(name: string): void {
        this.name = name;
    }

    release(): any {}

    async tryLock(): Promise<boolean> {
        return true;
    }
}
  1. Use your locker
import { Injectable, Logger } from '@nestjs/common';
import { Cron, UseLocker } from '@nestcloud2/schedule';
import { TaskLocker } from './TaskLocker';

@Injectable()
export class TasksService {
    private readonly logger = new Logger(TasksService.name);

    @Cron('45 * * * * *')
    @UseLocker(TaskLocker)
    handleCron() {
        this.logger.debug('Called when the current second is 45');
    }
}

API

class ScheduleModule

static forRoot(): DynamicModule

Import schedule module.

class Schedule

createTimeoutJob(methodRef: Function, timeout: number, options?: TimeoutOptions)

Dynamic create a timeout job.

fieldtyperequireddescription
methodRefFunctiontruejob method
timeoutnumbertruemilliseconds
optionsfalsesee decorators

createIntervalJob(methodRef: Function, timeout: number, options?: IntervalOptions)

Dynamic create a interval job.

fieldtyperequireddescription
methodRefFunctiontruejob method
timeoutnumbertruemilliseconds
optionsfalsesee decorators

createCronJob(rule: string | number | Date | CronObject | CronObjLiteral, methodRef, options?: CronOptions)

Dynamic create a cron job.

fieldtyperequireddescription
ruleDate string number CronObject CronObjLiteraltruethe cron rule
methodRefFunctiontruejob method
optionsfalsesee decorators

deleteTimeoutJob(name: string)

Delete a timeout job

deleteIntervalJob(name: string)

Delete a interval job

deleteCronJob(name: string)

Delete a cron job

getTimeoutJobs(): TimeoutJobOptions[]

Get all timeout jobs

getIntervalJobs(): IntervalJobOptions[]

Get all interval jobs

getCronJobs(): CronJobOptions[]

Get all cron jobs

Decorators

Cron(rule: string | number | Date | CronObject | CronObjLiteral, options?: CronOptions): MethodDecorator

Schedule a cron job.

fieldtyperequireddescription
ruleDate string number CronObject CronObjLiteraltrueThe cron rule
rule.dayOfWeeknumbertrueTimezone
options.namestringfalseThe unique job key
options.retriesnumberfalsethe max retry count, default is -1 not retry
options.retrynumberfalsethe retry interval, default is 5000

CronObject CronObjLiteral]

Interval(timeout: number): MethodDecorator

Interval(name: string, timeout: number): MethodDecorator

Interval(name: string, timeout: number, options?: IntervalOptions): MethodDecorator

Schedule a interval job.

fieldtyperequireddescription
timeoutnumbertruemilliseconds
options.retriesnumberfalsethe max retry count, default is -1 not retry
options.retrynumberfalsethe retry interval, default is 5000
options.immediatebooleanfalseexecuting job immediately

Timeout(timeout: number): MethodDecorator

Timeout(name: string, timeout: number): MethodDecorator

Timeout(name: string, timeout: number, options?: TimeoutOptions): MethodDecorator

Schedule a timeout job.

fieldtyperequireddescription
timeoutnumbertruemilliseconds
options.retriesnumberfalsethe max retry count, default is -1 not retry
options.retrynumberfalsethe retry interval, default is 5000
options.immediatebooleanfalseexecuting job immediately

InjectSchedule(): PropertyDecorator

Inject Schedule instance

UseLocker(locker: Locker | Function): MethodDecorator

Set a distributed locker for job.

Stay in touch

License

NestCloud is MIT licensed.

0.9.1

2 years ago

0.9.1-rc.0

2 years ago

0.8.5

2 years ago

0.9.0

2 years ago

0.8.4

2 years ago

0.8.3

2 years ago

0.8.0

2 years ago

0.7.21

2 years ago