Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Created November 27, 2014 04:56
Show Gist options
  • Save alexanderscott/c4ad46a7e1b16735df5d to your computer and use it in GitHub Desktop.
Save alexanderscott/c4ad46a7e1b16735df5d to your computer and use it in GitHub Desktop.
Simple example using Punctual - a Redis-backed task scheduler for node.js
"use strict";
var util = require('util'),
Punctual = require('punctual');
/**
* Define a simple TaskRunner for logging to console
*/
function ConsolePrinter(){
ConsolePrinter.super_.call(this, {});
this.taskTypes = ['consolePrint'];
}
util.inherits( ConsolePrinter, Punctual.TaskRunner );
ConsolePrinter.prototype.runTask = function(task){
console.log("Processing Task ", task.id);
};
/**
* Instantiate a TaskScheduler to persist, fetch, and route tasks
*/
var scheduler = Punctual.TaskScheduler.create({
pollIntervalLength : 5000,
taskRunners : {
consolePrinter: new ConsolePrinter()
}
});
scheduler.start();
/**
* Insert sample tasks on an interval to be processed
*/
setInterval(function(){
var task = Punctual.Task.create({
type: 'consolePrint',
scheduledFor: (new Date().getTime() + (3000))
});
return scheduler.scheduleTask(task);
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment