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

nestjs-node-cron

v0.0.1

Published

NestJS Module for Scheduling Cron Jobs

Downloads

4

Readme

NestJS | node-cron

nestjs-node-cron is a NestJS module that provides a simple and intuitive API for scheduling tasks using cron expressions. It is built on top of the popular node-cron library. This package offers a service to manage cron tasks easily within a NestJS application.

Installation

To install the package, use npm or yarn:

npm install nestjs-node-cron 
or
yarn add nestjs-node-cron

Getting Started

NodeCronModule

First, you need to import the NodeCronModule into your NestJS module.

import { Module } from '@nestjs/common';
import { NodeCronModule } from 'nestjs-node-cron';

@Module({
  imports: [NodeCronModule],
})
export class AppModule {}`

NodeCronService

Inject the NodeCronService into your service or controller where you want to manage cron tasks.

import { Injectable } from  '@nestjs/common'; 
import { NodeCronService } from  'nestjs-node-cron'; 

@Injectable() 
export  class  CronService { 
	constructor(private readonly nodeCronService: NodeCronService) {}
	setupCronJobs() { 
		// Example usage 
	} 
}

API Documentation

Methods

Validate(expression: string): boolean

Validates whether the provided string is a valid cron expression.

Parameters:

  • expression: A string representing the cron expression to validate.

Returns:

  • boolean: true if the expression is valid, false otherwise.

Usage:

const isValid = this.nodeCronService.Validate('*/5 * * * *');
console.log(isValid); // Output: true

Schedule(expression: string, func: ((now: Date | "manual" | "init") => void) | string, options?: ScheduleOptions)

Schedules a new task to execute the given function according to the cron expression.

Parameters:

  • expression: A string representing the cron expression for the schedule.
  • func: A function to execute when the cron expression ticks. It can be a function or a string command.
  • options?: Optional scheduling options as defined by node-cron.

Returns:

  • ScheduledTask: The scheduled task instance.

Usage:

this.nodeCronService.Schedule('*/5 * * * *', () => {
  console.log('Running a task every 5 minutes');
});

this.nodeCronService.Schedule('0 0 * * *', 'echo "Midnight task"');

GetTasks()

Retrieves the list of tasks created using the schedule function.

Returns:

  • Map<string, ScheduledTask>: A map of task identifiers to scheduled task instances.

Usage:

const tasks = this.nodeCronService.GetTasks();
tasks.forEach((task, id) => {
  console.log(`Task ID: ${id}, Task: ${task}`);
});

Example Usage

Here's a complete example of how to use NodeCronService in a NestJS service:

import { Injectable } from '@nestjs/common';
import { NodeCronService } from 'nestjs-node-cron';

@Injectable()
export class Service {
  constructor(private readonly nodeCronService: NodeCronService) {
    this.setupCronJobs();
  }

  setupCronJobs() {
    // Schedule a task to run every minute
    this.nodeCronService.Schedule('* * * * *', () => {
      console.log('Task running every minute');
    });

    // Validate a cron expression
    const isValid = this.nodeCronService.Validate('0 0 * * *');
    console.log('Cron expression is valid:', isValid);

    // Get all scheduled tasks
    const tasks = this.nodeCronService.GetTasks();
    console.log('Scheduled tasks:', tasks);
  }
}

Credits

This package is built on top of the node-cron library. Thanks to the authors and contributors of node-cron for providing a robust cron scheduling solution for Node.js.

For more information, check out our GitHub repository