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

simple-cron

v0.5.2

Published

SimpleCron is a simple cron runner.

Downloads

9

Readme

SimpleCron

SimpleCron is a simple cron runner. I've tried more or less to adhere to the functionality of the traditional unix cron utility. This includes the behaviour during a system time change.

As per the cron man page, in the event of a system time change will do the the following:

  • If the time changes backwards by more than 3 hours, the cron intervals will be reset. This means that jobs will be re-run.
  • If the time changes forwards by more than 3 hours, the cron intervals will be reset. This means that any jobs missed will not get run.
  • If the time changes backwards by less than 3 hours, SimpleCron will continue running normally and those jobs will not get run again.
  • If the time changes forwards by less than 3 hours, SimpleCron will continue running normally and all missed jobs will be run immediately.

Installing

npm install --save simple-cron

or

yarn add simple-cron

Using

Here's a contrived example:

// Instantiate SimpleCron
const SimpleCron = require('simple-cron');
const cron = new SimpleCron(); // Takes an optional parameter to define runInterval

// SimpleCron is also an event emitter so you can
// easily tell when events occur.
// Valid events are run, stop, schedule, cancel, invoke
cron.on('invoke', (jobId) => {
  console.log(`job '${jobId}' just ran!`);
});

// This is something we want to do every minute
const sendEmails = () => {
  const addresses = getAddresses();
  addresses.forEach((address) => {
    sendEmail();
  });
};

// Schedule this job to run every minute
const emailJobId = cron.schedule('* * * * *', sendEmails);

// Start running SimpleCron
cron.run();

// You can also schedule jobs after SimpleCron is already running if you want
const logJobId = cron.schedule('0 * * * *', () => {
  console.log('Another hour has passed you by...');
});

// Cancel a running job
cron.cancel(emailJobId);

// Shutdown SimpleCron. Returns a promise so you know when it's stopped.
cron.stop().then(() => { console.log('Shutdown complete.'); });

SimpleCon uses cron-parser to handle cron expressions.

API Reference

SimpleCron([options])

Constructor. Returns reference to cron instance. Take optional options object containing the following keys:

  • runInterval: The frequency in milliseconds for which SimpleCron will check to run jobs. Defaults to 100.

cron.run()

Runs SimpleCron. Starts running any scheduled jobs.

cron.stop()

Stops SimpleCron. Returns a promise that resolves when SimpleCron has finished stopping.

cron.schedule(expression, function)

Schedules a function to be called at an interval. Returns an id for this scheduled job.

cron.cancel(id)

Cancels a scheduled job so it no longer runs on an interval. Requires you to provide the id returned by cron.schedule().

Events

SimpleCron extends EventEmitter and emits the following events:

  • run
  • stop
  • cancel(id)
  • schedule(id)
  • invoke(id)

Testing

SimpleCron has thorough test coverage. To run the tests, clone the repo then do this:

npm i && npm test

SimpleCron uses semistandard for linting.