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-crontab

v0.0.8

Published

Crontab-based task scheduler for node.

Downloads

5,170

Readme

node-crontab

A task scheduler for node that uses the crontab syntax


Why node-crontab?

Need to schedule tasks in node and you already have the cron syntax? Then you need node-crontab! We take care of the task scheduling and timing and all you have to do is tell us what to do!

With node-crontab, you can pass in your callback, arguments for the callback, and even the context of the callback function! We also take care of times that are normally too long for setInterval/setTimeout to handle automatically!

So if this sounds like something you need, keep reading!

Setup

All you have to do is run this command in the directory of your node project:

npm install node-crontab

npm will take care of the rest!

Example Usage

Regular job

var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("*/2 * * * *", function(){ //This will call this function every 2 minutes
    console.log("It's been 2 minutes!");
});

Arguments

var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * * *", function(a){
    console.log("Hello " + a + "! It's been a minute!");
}, ["World"]);

Context

var crontab = require('node-crontab');
var obj = {a: "World"};
var jobId = crontab.scheduleJob("* * * * *", function(){
    console.log("Hello " + this.a + "! It's been a minute!");
}, null, obj);

Non-repeating

var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * * *", function(){
    console.log("Hello world! It's been a minute, but this will be the only time I run.");
}, null, null, false);

Killing a job

var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * * *", function(){
    console.log("It's been a minute!");
});
crontab.cancelJob(jobId); //Should cancel the job immediately

Suicidal jobs

var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * * *", function(){
    console.log("It's been a minute, but this is the last time I run.");
    crontab.cancelJob(jobId); // Jobs can cancel themselves, too!
});

API

int crontab.scheduleJob(cronTime, callback, [args], [context], [repeating = true])

  • cronTime - the crontab format of the schedule.
    • Example: "*/2 * * * *" would run every 2 minutes of every hour of every day
    • cronTime is parsed by cron-parser, full support information can be found on the cron-parser github page.
  • callback - The function you'd like to run
  • args - Arguments you'd like node-crontab to pass into your callback, in array notation
  • context - The "this" of the function you'd like node-crontab to call
  • repeating - Declare whether this task should repeat or not. Defaults to true, since usually you use the crontab format for repeating tasks.
  • Returns an ID that you pass into cancelJob in order to kill a job after it's been scheduled

bool crontab.cancelJob(jobId)

  • jobId - The ID of the job you'd like to cancel
  • Returns true/false, indicates whether it successfully canceled the job or not (will only be false if a job with that ID did not exist)

To Do

  • Create better tests. I've created basic tests, but they're not as good as they could be, probably.

Wrap-up

Have an issue? Feel free to open an issue in the issues page. If you want to help, feel free to fork and make pull requests!