1.0.0 • Published 8 years ago
timeo v1.0.0
Timeo
Timeo is a cron job like service with co-flow and fiber yielding, with on socket message injection. .
Install to your repo:
$ npm install timeo --save
Version
0.2.0
Tech
Timeo uses a number of open source projects to work properly:
- co
- cron
- node fibers
- koa-compose
Example
var timeo = require('./lib/timeo.js');
var middleware_ex = function *(next){
var ctx = this;
ctx.a = ctx.a ? ctx.a : 0;
ctx.b = ctx.b ? ctx.b : 1;
ctx.c = ctx.c ? ctx.c : 2;
ctx.d = ctx.d ? ctx.d : 3;
ctx.count = ctx.count ? ctx.count : 0;
ctx.msg = 'starting';
console.log(ctx.msg);
yield next;
};
//The cron middleware will run every 5 seconds
var cronTime = '*/5 * * * * *';
var cron_middleware_ex = function *(){
var ctx = this;
ctx.a = ctx.a + 1;
ctx.b = ((ctx.b * 2) + 1)/2;
ctx.msg = 'ping';
ctx.count++;
console.log('cron_middlware_ex');
console.log(ctx);
return true;
};
//The cron middleware will run every 7 seconds
var cronTime2 = '*/7 * * * * *';
var cron_middleware_ex2 = function *(){
var ctx = this;
ctx.a = ctx.a - 1;
ctx.b = (ctx.b/2 + 1);
ctx.c = ctx.c + 1;
ctx.d = ctx.d * 2;
ctx.msg = 'pong';
ctx.count++;
console.log('cron_middlware_ex2');
console.log(ctx);
return true;
};
//The cron middleware will run every 17 seconds
var cronTime3 = '*/17 * * * * *';
var cron_middleware_ex3 = function *(){
var ctx = this;
ctx.a = ctx.a + 1;
ctx.b = ctx.b + 1;
ctx.c = ctx.c + 1;
if(ctx.msg === 'ping') {
ctx.d = (ctx.d * 2) / ctx.count;
} else if(ctx.msg === 'pong'){
ctx.d = ((ctx.d - 5) / 2) / (5 + ctx.count) ;
}
ctx.msg = 'strike';
ctx.count++;
console.log('cron_middlware_ex3');
console.log(ctx);
return true;
};
var middleware_ex2 = function *(next){
console.log('Hello there!');
yield next;
};
var middleware_ex3 = function *(next){
console.log('Hello there 2!');
yield next;
};
var middleware_ex4 = function *(next){
var ctx = this;
ctx.on('data', function(data){
console.log('message: ' + data);
});
yield next;
};
//Complete function cannot be a generator function
var complete = function (){
console.log('Cron finished');
};
var app = timeo();
app.use(middleware_ex);
//arugments app.cron(middleware gen function, the cron time, function run when complete, run on start, timezone)
app.cron(cron_middleware_ex,cronTime, complete, true, false);
app.use(middleware_ex2);
app.cron(cron_middleware_ex2, cronTime2, complete, true, false);
app.use(middleware_ex3);
app.cron(cron_middleware_ex3, cronTime3, complete, true, false);
app.use(middleware_ex4);
app.start(4444);
// Fired off by curl 127.0.0.1:4444
Development
Want to contribute? Great!
License
MIT
Authors