Skip to content

Schedules and manages workers like there's no tomorrow!

License

Notifications You must be signed in to change notification settings

forcemagic/worker-scheduler

Repository files navigation

worker-scheduler

npm Build Status Test Coverage License

Ever wanted to schedule jobs like "this thing should execute every x hours/minutes/seconds"? Then this library is for you! You can even trigger workers manually! Works with native promises, as I don't think you want to do synchronous work in your background workers.

What's a "worker"?

Let me just show you a quick example:

index.js

const Scheduler = require('worker-scheduler');

const myScheduler = new Scheduler();
const worker = new Scheduler.Worker('mycoolworker', require.resolve('./myworker'), 86400 * 1000);

worker.on('finish', (ip) => {
  console.log(ip);
});
worker.on('error', e => console.error(e));

myworker.js

const fetch = require('node-fetch');

// NOTE: This *has* to be an async function (or a Promise-returning function),
//       otherwise the scheduler won't know what to do with it!
module.exports = async (log) => {
  log('I can even log stuff here! Yay!');
  return fetch('https://api.ipify.org').then(res => res.text());
};

This simple example would fetch your public IP address, return it, and console.log it out every day! (Every worker is forked out of the scheduler, so don't worry about hanging your master process!)

About

Schedules and manages workers like there's no tomorrow!

Resources

License

Stars

Watchers

Forks

Packages

No packages published