taskscheduler
A simple, queue-backed and topic-oriented task scheduler for Node.js with pluggable queue implementation.
For a sample queue plugin that uses Amazon SQS, see: SQSTask
TL;DR
A simple call:
scheduler;
will schedule a javascript function (taskjob
in this case) that can "listen" to incoming messages on a sometopic
topic
in a queue every 100 milliseconds. A taskjob
implementation receives: (topic, message, callback) arguments when called. A 'topic' is typically
a separate queue/channel on a message queue.
You can send messages asynchronously to the queue with:
scheduler;
Status
An early release. Feel free to: inspect, hack, enjoy and contribute (e.g.: feedback, documentation or bug fixes), but consider it an "alpha" stability.
Installation
> npm install taskscheduler> npm install sqstask
USAGE
Before you can use taskscheduler you have to configure it:
Setup
var AWSConfig = "accessKeyId" : "..." "secretAccessKey" : "..." "awsAccountId" : "..." ; var util = sqstask = AWSConfig scheduler = sqstask;
Registering a Handler
var publisherHandlerID = scheduler; { consoledir"Task job fired, with message: " + message; var err = null; var random = Math + 1; if random === 5 err = "something"; console; ; };
De-Registering a Handler
//-- You can also de-register a task, if you don't want it running "forever". ;
Sending messages
scheduler; // end of topic ensuring. { for var i = 0; i<5; i++ scheduler; }
Plugin Implementation
To implement a plugin for a different queue, you must write a proper Node.js module that complies to the following requirements:
- Implements and exports following methods:
- put(topic, message, callback) : puts a message on a queue.
- get(topic, callback) : fetches a message from a queue
- topicEnsureExists(topic, callback) : checks if a topic (queue) exists on a queue and creates one if it doesn't.
- Implements and exports a Message class that:
- supports topic, body and id properties and has a constructor: (topic, body, id)
- implements release(callback) method which returns previously grabbed (and locked) message back to the queue
- implements del(callback) method that deletes a message from the queue
For a sample of a properly implemented queue task, inspect the source code of SQSTask