2015年9月21日星期一

nodejs-persistable-scheduler: A nodejs based, persist by mysql, timely and repeatable task framework

In the past months, I were working in a volunteer project, which target is to create a P2P parking slot sharing platform for IBMers in Beijing. You must know that parking slots is what kind of cherish if you have ever living here.
As I am the back-end service architect of said platform, I was responsible to design/implement most part of the back-end services and transactions.
Inside the development, one of my technical requirement is a timely scheduler framework in NODEJS, which should has the following capabilities:
  • Customizable task functions;
  • Repeatable with customized frequency;
  • Persist the task data into a mysql DB;
  • Task executing status recording;
  • Fallback after restart the service;
  • Easy to use;
By going through the public modules inside NPM, I got nothing perfect candidate.
So finally I decide to build one. Now the module is finished. Following is the introduction of said module nodejs-persistable-scheduler:
A nodejs based, persist by mysql, timely and repeatable task framework;
==============================
A nodejs based, persist by mysql, timely task framework;
$ npm install nodejs-persistable-scheduler
  1. q: A library for promises (CommonJS/Promises/A,B,D);
  2. mysql: A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.
This module is a nodejs based, persist by mysql, timely task framework;
There are 4 components that compose the whole module:
Which is repsonsible to doing the initialization works for making the functions of whole module available. In general, the initialization works are sperated into 2 parts: A system initialization task which will create 2 database tables named "fixtimetask" and "scanrecord" in the DB reference that discribed in the configurations; and a customized initialization task which will add maintaining level tasks(not user event triggered tasks, on some other words) if you need any.
This component will be executed ONLY ONCE (Well, actually, you can execute it more than once within some tricky way, referring to the (Best Prectice)[#tests:] please);
Which is responsible to doing 2 steps of fallback work evertime the current instance of nodejs-persistable-scheduler startup: 1. Find out the in-complete executing initialization and maintainess tasks in the last running from the persistance, and run them again; And 2 start the tasks maintainess monitor;
Which is a framework for registering/executing/delaying/hastening/cancelling/pausing/restarting tasks from customers;
Which is responsible to recording the initialization/maintainess task status into persistance while executing.

var scheduler = require('nodejs-persistable-scheduler');
var q = require('q');
var methodArr = [];
/***************************
 ***************************
  DONT CHANGE THE ORDER of mehtodArr!!!!
  JUST PUSH THE NEW ADDED ONES AT THE TAIL!!!
  **************************
  **************************/
method1 = function(param) {
  var defer = Q.defer();
  //functional codes for method1...
  return defer.promise;
};

methodArr.push({name:'METHOD1',method1});

method2 = function(param) {
  var defer = Q.defer();
  //functional codes for method2...
  return defer.promise;
};
methodArr.push({name:'METHOD2',method2});

initialTask = function() {
  var defer = Q.defer();
  var AM = new Date();
  AM.setHours(12,0,0);
  scheduler.registerTask("daily",AM,'METHOD1',null,null).then(function(ID) {
    if (ID)
    {
      defer.resolve(true);
    }
  },function(err) {
    defer.reject(err);
  });
  return defer.promise;
};

var schedulerConfig = {
  initFunc: initialTask, 
  scanInterval: 24*60*60*1000//scan interval time, in MS
};

var initialConfig = {
  db:
  {
    host: '',
    user: '',
    password: '',
    database:'',
    port: 
  },//DB config
  methods:methodArr,
  scheduler:schedulerConfig
};

scheduler.initialize(initialConfig);
Caution that:
  1. Please keep available of the DB naming 'fixtimetask' and 'scanrecord' for module 'nodejs-persistable-scheduler';
  2. Inside one NODEJS instance, or multiple NODEJS instances that sharing a same DB, only 1 'nodejs-persistable-scheduler' instance allowed.
  3. For different NODEJS instances that are not sharing a same DB, there could be multiple 'nodejs-persistable-scheduler' instances.
Tips:
If you want to re-initial the whole 'nodejs-persistable-scheduler' module (run the customized initialization module more than once), you can clean up the record in DB table 'scanrecord' with recordType equals to 0;
Refer the comments inside nodejs-persistable-scheduler/index.js please;

2008年11月11日星期二

Only a start...

I will record some technicial experiences in this BLOG. For my personal review, and for sharing with you, too.