Simplistic in-memory job/worker manager with rate limiting and custom worker counts
Simple in-memory job/worker manager with rate limiting and custom worker counts. (Status: experimental)
This is a small library that aids in executing long-running processes outside of your other events. It has no underlying backing store, so it's mostly useful for fire-and-forget tasks like crawling & scraping, updating caches, sending out non-important emails etc.
The plan is to add hooks for you to easily manage how it reads and writes to the queue, so you could use any kind of a backing store you see fit.
It's likely that a module with pre-made backing stores will be added separately, under a corresponding name, like stevejobs-redis etc.
There is a server wrapper version of this module called [https://github.com/arnorhs/stevejobs-server](SteveJobs Server) that wraps this module in an http server listening for /job POST requests, so you could use that to have this running on its own machine, for instance.
npm install stevejobs
var steveJobs = require'stevejobs';var steve = steveJobsdelay: 4000 // msworkers: 3 // 3 concurrent workers;steveaddHandler'do_something'// do something here - make sure to call done when your task is donedone;;steveaddJob'do_something' myvar;stevestart;
The module doesn't output anything by default. To log to console, you can hook into the 'log' event:
The object passed in has the following properties:
time (Date object),
getMessage (function to get the log message),
level (number range 0-2 dictating severity)
Not much in terms of error handling as is. The job will be retried a few times, and then if it fails again, this event will be emitted, allowing you to take action..
steveon'job_error'// the job variable has the data, how many times it was retried and its name.;
delay: Time in milliseconds that a worker waits for before starting on the next job.
maxRetries: How often a job will be retried on error before being abandoned.
workers: Number of worker loops that can be executing job handlers concurrently.
Adds a new job to the queue. The name can be any string/number and the data parameter can be any type, as long as your corresponding handler knows what to do with it. Currently this method does not support multiple parameters, so you're going to have to pass in an array, an object or something like that if you want to pass in multiple vars.
Adds a new job handler for a given name. Adding multiple handlers results in an error. The callback will be given two parameters, a callback you need to call when your handler is done executing, and the data parameter you passed in in the
addJob() method. If you don't call that callback, the corresponding worker will stop and not continue unless you restart the server.
You call this method to start the worker loop. Until this has been called, no work will be done.