jobbed
v0.4.7
Published
Jobbed is a simple background job/task processing queue for Node.js (>= 7.6) using `cluster` & separate Node processes, powered by Redis.
Downloads
5
Maintainers
Readme
Jobbed
Jobbed is a simple background job/task processing queue for Node.js (>= 7.6) using cluster
& separate Node processes, powered by Redis.
Features
- [x] each worker runs its tasks in a separate Node.js process
- [x] RESTful JSON API
- [x] integrated UI
- [x] logging per job/task
- [ ] delay job/task execution
- [ ] job/task expiry value for being in active state
Install
npm install -g jobbed
Usage
Step 1: Create a task
Each job triggers an execution of a Task i.e. a recipe what to do for that job. It is defined as a class
with perform
method. Task
corresponds to Worker
from similar projects such as resque or sidekiq. It is named this way to avoid the clash with cluster
workers.
const { Task } = require('jobbed');
class LoopTask extends Task {
perform(args) {
this.log(`Running inside: ${process.pid}`);
this.log(`Args: ${args}`);
let progress = 0;
for (var i = 0; i < 1e10; i++) {
if (i % 1e8 == 0) {
this.log(i)
this.progress(progress, 100);
progress++;
}
}
}
}
module.exports = LoopTask
Step 2: Run the server
Put your tasks in tasks/
directory and run
jobbed start
By default, it checks available number of cores and it instantiates the number of Node processes accordingly. You can specify number of process by hand using -c
option. Type jobbed start --help
to see all available options.
Step 3: Enqueue a job
You can enqueue a job to perform given task from a JavaScript application
const { enqueue } = require('jobbed');
enqueue('LoopTask', '[1, 2, 3]');
By default, the enqueue
function puts the new job on default
queue; this can be changed with the name
parameter from options
.
enqueue('LoopTask', '[1, 2, 3]', { name: 'high' });
A job can be scheduled to run at a specific time using scheduledAt
parameter.
enqueue('LoopTask', '[1, 2, 3]', { name: 'high', scheduledAt: Date.now() + Sugar.Number.days(4) });
It is also possible to enqueue a job through Jobbed REST API
http POST :3000/api/enqueue task=LoopTask args='[1,2,3]'
(optional) Step 4: Check the progress via UI
Go to localhost:3000
to check the job proegress through Jobbed UI.
Jobbed UI
Jobbed comes with a built-in web UI that allows to quickly see the status of all jobs. Here's a preview of what it looks like:
Environments
You can distinguish visually the UI between staging
and production
environments by specifying CRANKSHAFT_ENV
variable accordingly. You can set this variable when launching Jobbed with jobbed start
e.g.
CRANKSHAFT_ENV=production jobbed start
It will add a small color bar at the top to help you identify at a glance which UI instance you are currently using.
Concepts
Queues
Queue is a list of Job items, stored so as to be retrievable and handled in the order of insertion. You can create a Queue by giving it a name. This is a lower level API which usually shouldn't be used directly - it's advised to use enqueue
helper.
const { Queue } = require('jobbed');
const highQueue = new Queue('high');
Logging
You can log on per job/task basis by using this.log(message)
method, where message
is an object or a string.
this.log("This is my log message");
this.log({ a: 1, b: 2});
Create tasks from CLI
You can create a task in tasks
using CLI
jobbed create foo
This command will create FooTask.js
task in tasks/
directory.