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

controlled-schedule

v2.0.0

Published

Recurring schedule for async tasks. Schedule next execution only after the current one finishes

Downloads

7

Readme

Controlled schedule

Build Status Coveralls

Recurring schedule for async tasks. Schedule next execution only after the current one finishes.

e.g:

const execute = require('controlled-schedule');

function task() {
  // some async operation
  // should accept a callback as parameter
  // or return a Promise
}

execute(task)
  .every('20s')
  .startIn('1m')
  .stopAfter('2h')
  .on('error', function(err) {
    // log error
  })
  .on('stop', function() {
    // do something
  });

For more examples check /examples folder and the end of this README.

Instalation

npm install controlled-schedule --save

Usage

  • Your task must return a Promise or accept a callback(err, value) as argument.

  • Methods accept time in milliseconds or in string format. e.g. '10 days', '10d', '10h', '2.5 hrs', '10h', '10m', '10s'

  • Every method returns the object reference so you can chain all operations.

Usage:

const execute = require('controlled-schedule');

//create schedule object
let schedule = execute(task);

// define interval between each execution
schedule.every('20s');

// define how long to start the first execution
schedule.startIn('1m');

// start immediately
schedule.start();

// define how long the task will be rescheduled
schedule.stopAfter('2h');

// stop the schedule (waits for task to finish if it's executing)
schedule.stop();

// events

// after a task runs
// err - error object passed by the task
// value - value returned by the task
schedule.on('run', function(err, value) {

});

// after a task runs with success
// value - value returned by the task
schedule.on('success', function(value) {

});

// after a task runs and return an error
// err - error object returned by the task
schedule.on('error', function(err) {

});

// when schedule stop is triggered
schedule.on('stop', function() {

});

// return Promise for the next execution
schedule.nextRun()
  .then(function(value) {

  })
  .catch(function(err) {

  });

More examples

Run task indefinitely with 1 hour interval between executions:

execute(task)
  .every('1h')
  .start();

Run for 1 minute starting every next task immediately after previous one finishes:

execute(task)
  .stopAfter('1m')
  .start();

Run task with 1 minute interval and stop if an error occurred:

let schedule =
  execute(task)
    .every('1m')
    .on('error', function(err) {
      schedule.stop();
      console.log(err);
    })
    .start()

For more examples check /examples folder.

License

MIT