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 🙏

© 2025 – Pkg Stats / Ryan Hefner

echoapi-cron-scheduler

v1.0.19

Published

A Node.js cron scheduler for managing and executing scheduled tasks.

Readme

CronScheduler

CronScheduler is a Node.js-based cron scheduler library that allows you to manage, execute, and monitor scheduled jobs. It can handle cron expressions, load jobs from a file, create new jobs, and handle job cancellation or restart.

Features

  • Supports cron expressions for flexible scheduling.
  • Can schedule jobs based on preset cycles (minute, hour, day, week).
  • Jobs can be loaded, created, canceled, or restarted.
  • Supports graceful shutdown and reporting of job status.
  • Jobs are stored in a local JSON file.

Installation

  1. Clone this repository or install it via npm:
npm install echoapi-cron-scheduler
  1. If you're using the GitHub repository, run:
git clone https://github.com/Apipo/echoapi-cron-scheduler.git
cd echoapi-cron-scheduler
npm install

Usage

Basic Usage

First, import the CronScheduler class and create an instance:

const CronScheduler = require('echoapi-cron-scheduler');
const scheduler = new CronScheduler();

Creating and Running Jobs

To create a new job, use the createJob method. It will store the job in a JSON file and execute the job based on the schedule provided.

const { option, test_events } = require('./tmp/request');
const { v4: uuidv4 } = require('uuid');

// Set a unique job ID
_.set(option, 'job_id', uuidv4());

// Create the job
scheduler.createJob(option, test_events);

// Load all jobs and start executing them
scheduler.loadJobs();

Start Job with Cron Expression

You can start a job using a cron expression:

scheduler.startJob('* * * * * *', 'exampleJob1', async () => {
    console.log('Executing job every minute...');
});

This will execute the job every minute.

Canceling and Restarting Jobs

To cancel a running job, use cancelJob:

scheduler.cancelJob('exampleJob1');

To restart a job, use restartJob:

scheduler.restartJob('exampleJob1');

Retrieving Job Information

You can retrieve a list of all scheduled jobs, including their current states (e.g., whether they are running):

const allJobs = scheduler.getAllJobs();
console.log(allJobs);

Graceful Shutdown

You can handle a graceful shutdown of all scheduled jobs by listening for SIGINT:

process.on('SIGINT', () => {
    scheduler.gracefulShutdown().then(() => { process.exit(0) });
});

Cron Expression Examples

Here are some cron expression examples you can use to schedule tasks:

  • * * * * * * — Every second
  • */5 * * * * * — Every 5 seconds
  • 0 * * * * * — Every minute
  • 0 0 * * * * — Every hour
  • 0 0 0 * * * — Every day at midnight
  • 0 0 * * 1 * — Every Monday at midnight

License

This project is licensed under the MIT License - see the LICENSE file for details.


### Explanation

1. **`package.json`**:
   - Defines the necessary dependencies (`node-schedule`, `axios`, `lodash`, `cron-parser`, etc.).
   - A basic `start` script that runs the main file (`index.js`).
   - Set the Node.js engine to ensure compatibility with the required Node version.

2. **`README.md`**:
   - Provides a basic overview of the CronScheduler.
   - Describes installation steps and usage examples for creating, managing, and executing cron jobs.
   - Includes explanations for cron expressions and how to handle graceful shutdowns.

### To Use This Project:

1. Clone the repository or install the package using `npm`.
2. Include your scheduler logic and job definitions in the `index.js` or another entry point.
3. Use the provided `createJob`, `startJob`, `cancelJob`, and other methods to interact with scheduled jobs.