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

node-cron-job

v0.2.3

Published

An easy-to-use cron job/task scheduler for node.

Downloads

254

Readme

This is a cron job scheduler for Node.

Allows jobs to be defined in a separate module.

It can fork a new process for each job, resulting in zero impact on application performance.

Table of Contents

Install

$ npm install node-cron-job

Setup

The jobs have to be declared in a separate module such as:

// jobs.js

exports.first_job = {
    
    after: {                // Configuring this job to run after this period.
        seconds: 2,
        minutes:2,
        hours: 1,
        days: 3
    },
    job: function () {
        console.log("first_job");
    },
    spawn: true             
}

exports.second_job = {
    
    on: "*/2 * * * * *",    // Cron tab instruction.
    job: function () {
        console.log("second_job");
    },
    spawn: false            // If false, the job will not run in a separate process.
}

Next, the module may be used any where within your code to indicate the absolute path to the jobs file and explicit instructions to start those jobs. Example:

// main.js

var cronjob = require('node-cron-job');


cronjob.setJobsPath(__dirname + '/jobs.js');  // Absolute path to the jobs module.

cronjob.startJob('first_job');

cronjob.startJob('second_job');

API

Each job exported by the jobs module can have the following objects:

  • on: The cron tab instruction string that defines the schedule. Please see crontab.org.

cron-parser is used to parse these.

  • after: A easier-to-use but less expressive way to schedule a job. It defines the time after which the job is supposed to run, periodically. This will take priority over on. Example:
after: {
    hours: 2,
    days: 10
},
  • job: The job closure/function that is to be scheduled.

  • spawn: A boolean value telling the module to run this job in a separate forked process or in the same thread as your application. This should be set according to scalibility and peformance needs. The default value is true.

The module includes these methods:

  • setJobsPath(abs_path): Sets the absolute path to the jobs module.

  • startJob(job_name): Starts the given job.

  • startAllJobs(): Starts all jobs defined in the jobs module.

Note

If your application is a cluster with multiple instances, beware that unless you load and start your jobs module in the master process, your jobs may run on schedule once per node instance !

Future releases

An option will be introduced soon allowing all the jobs to run in one separate process. The API will be extended to include methods allowing running jobs to halt. Some of the work may be moved to a C++ extension in future releases.