npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestcfork/schedule

v0.7.5

Published

NestCloud is a Node.js micro-service solution, writing by Typescript language and Nest.js.

Downloads

1

Readme

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 @nestcfork/schedule

Usage

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

@Module({
  imports: [
    ScheduleModule.forRoot(),
  ]
})
export class AppModule {
}
import { Injectable, Logger } from '@nestjs/common';
import { Cron, Timeout, Interval } from '@nestcfork/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 '@nestcfork/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 '@nestcfork/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 '@nestcfork/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 '@nestcfork/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 '@nestcfork/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.

| field | type | required | description | | ----------| -------- | -------- | -------------- | | methodRef | Function | true | job method | | timeout | number | true | milliseconds | | options | | false | see decorators |

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

Dynamic create a interval job.

| field | type | required | description | | ----------| -------- | -------- | -------------- | | methodRef | Function | true | job method | | timeout | number | true | milliseconds | | options | | false | see decorators |

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

Dynamic create a cron job.

| field | type | required | description | | --------- | -------------------------------------------- | -------- | --------------------------------------------- | | rule | Date string number CronObject CronObjLiteral | true | the cron rule | | methodRef | Function | true | job method | | options | | false | see 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.

| field | type | required | description | | --------------- | -------------------------------------------- | -------- | --------------------------------------------- | | rule | Date string number CronObject CronObjLiteral | true | The cron rule | | rule.dayOfWeek | number | true | Timezone | | options.name | string | false | The unique job key | | options.retries | number | false | the max retry count, default is -1 not retry | | options.retry | number | false | the 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.

| field | type | required | description | | ----------------- | ------- | -------- | --------------------------------------------- | | timeout | number | true | milliseconds | | options.retries | number | false | the max retry count, default is -1 not retry | | options.retry | number | false | the retry interval, default is 5000 | | options.immediate | boolean | false | executing job immediately |

Timeout(timeout: number): MethodDecorator

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

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

Schedule a timeout job.

| field | type | required | description | | ----------------- | ------- | -------- | --------------------------------------------- | | timeout | number | true | milliseconds | | options.retries | number | false | the max retry count, default is -1 not retry | | options.retry | number | false | the retry interval, default is 5000 | | options.immediate | boolean | false | executing 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.