Using Nodejs+redis to develop a message queue and timed task processing

Source: Internet
Author: User
Tags message queue

Author: Robanlee

Original articles, reproduced please specify: Radish Lee http://www.robanlee.com

SOURCE is here: Https://github.com/robanlee123/RobCron




Time is limited, no detailed comments, questions or comments welcome @ I, also welcome everyone to criticize correct.

Some of the information required in this article is as follows:

1. NODEJS ==> can go nodejs.org Download the latest source code.
2. Redis ==> Redis.io
3. An open-source queuing system for KUE ==> Nodejs
4. Node-schedule ==> NODEJS An open source dispatch system

Needless to say, to introduce the process of the task first:

1. Nodejs or PHP or another language to write Redis a scheduled task, such as doing something every minute, here to use SayHello instead of the good
2. Use Nodejs to read this task and convert it to Node's dispatch task (Node-schedule to complete)
3. The scheduler [Node-schedule] distributes tasks according to set rules.
4. Kue accept the task and join the queue to execute.
5. Done

STEP1: Create a task

/** * add task *  @author  [email protected] *///load function, load some lib centrally, This source code please refer to the last attached file Var loader = require ('./loader ');   function addtask (opts) {         new loader (This);                  //Default Settings          this.opts = {                 keyids: ' Schedule:job:ids ',                 keylists: ' Schedule:job:list ',                 keyjob: ' Schedule:job: '          }                / /merge configuration, similar to JQUERY: EXTEND &NBSp;      this.mergeparams (opts);  };//merge  optionsaddtask.prototype.mergeparams = function  ( param ) {         if (undefined === this.opts )  {                 return false;         }                 for (Var x in param)  {                  if (param[x] != undefined &&  '  !=  PARAM[X])  {                         this.opts[x] = param[x];                 }        }};// Add Data Method addtask.prototype.pushdata = function  ( data ) {          if (undefined == data )  {                 console.log ('--error:data is null ');                 return false;         }         This.getIncr.call (This,function (response,obj) {                 var id = response;                 obj.redisclient.rpush (Obj.opts.keyLists,id, function (err,response) {       &nbsP;                if (ERR)  throw err;                 });                          data.id = id;                 var m =  Obj.redisClient.multi ();                 for (Var x in data)  {                         m.hset (  obj.opts.keyjob+id,x,data[x] );                 }                                 m.exec (function (err,response) {                          if (ERR)  throw err;                          console.log (' [info]  task: [' +data.name+ '] has been set successful! ');                 });                              };//get Redis current self-increment idaddtask.prototype.getincr =  function  (callBack) {        VAR OBJ = THIS;         THIS.REDISCLIENT.INCR ( This.opts.keyids,function (Err,response) {                 console.log ("[info] current id is : "  +  Response);                 CallBack (response, obj);         });

Load this lib to write a demo:

var data = {' name ': ' Taskdemo ', ' Created ':D ate.now (), ' state ': 1, ' type ': ' Untitled ', ' R ' Ule ': ' */1 * * * * *//This task rule can be a crontab rule, which indicates that};var job is executed once per minute = new AddTask (); job.pushdata (data);

Execute this script and if everything is OK, you will see the following information:

NODEJS output:

REDIS:

The next step is to get the data and convert it to a dispatch task.

Source:

Var loader = require ('./loader '); Var tasklog = require ("./TaskLog"); function  scheduletask () {        new loader (this);          this.opts = {                 keyids: ' Schedule:job:ids ',                 keylists: ' Schedule:job:list ',                 keyjob: ' Schedule:job: '          }                 this.task = {                 taskDemo:undefined        };                  //monitor Cancel task Operation          this.listencancel ();}; scheduletask.prototype.setscheduletask = function  (data,obj) {          this.task[data.name] =  this.libs[' Node-schedule '].scheduleJob ( Data.rule,function () {                 obj.setqueue (data);                 console.log (' [info] task : '  + data.name +  '  has been  set in queue! ');         });         }; scheduletask.prototype.setqueue = function  (datas) {                 var jobs = this.libs.kue.createqueue ();          jobs.create (datas.name ,{                 ' Name: ': datas.name,                 ' State ': 1        }). Save ();                  console.log ("[info] task [" +datas.name+ " ] has been queued! ");                 this.setlog ( datas);}; scheduletask.prototype.setlog = function  (ResponseData) {         var logData = {                 jobid:responsedata.id,                name:responsedata.name,                 result:1         };                                                  new tasklog ( Logdata);         console.log ("[Info] task has been  loged ");}; scheduletask.prototype.getjob = function  () {         This.getJobIndex.call (This,function (response,obj) {                 for (var x in response )  {                          Obj.redisClient.hgetall (Obj.opts.keyjob+response[x],function (err,data) {                                  console.log ("[info] task:[" +data.name+ "] has  Been loaded! ");                                  obj.setscheduletask (data,  obj);                         });                 }        });}; scheduletask.prototype.gEtjobindex = function (callBack) {        //read tasks  from <list schedule:job:list>        var o  = this;        this.redisclient.lrange (this.opts.keyLists,0 , -1,function (Err,response) {                 if (ERR)  throw err;                 callback (Response, o);         });}; scheduletask.prototype.listencancel = function  () {         var job = this.libs.kue.createqueue ();         var  that = this;        job.process (' CancelJob ', function ( Data,done) {                  that.task[ Data.data.data].cancel ();                 console.log (' [info] task:  ' +data.data.data +  '  has been  Canceled ')  ;                 done ();         });

Execute code:

var x = new ScheduleTask (); X.getjob ();

After waiting a minute, the Nodejs console will output (this task will be executed every minute without cancellation):

Second minute:

REDIS Current Data:

This data adds some of the Kue's tasks, q:job:[]:inactive this to identify that the task has not been executed, the task status after execution has

Complete active failed delay four kinds

At this point, only the steps to perform the task are left.

Var loader = require ('./loader '); Function exectask () {         new loader (This);                  var job = this.libs.kue.createqueue ();         job.process (' Taskdemo ', function (Data,done) {                 console.log (' [info] task: ' +data.type+ ' # ' +data.id+ '  has been executed successful! ');                                       You can do what you want to do before             //done                                   done ();  // Don't forget to call this method         });                 }//Add a Kue task that cancels the scheduled task exectask.prototype.addcanceljob =  function  () {        var job = This.libs.kue.createQueue ();         job.create (' Canceljob ',  {data : ' Taskdemo '}). Save ();         console.log (' [info] task:  Canceljob has been send! ');}

Execute this script:

var et = new Exectask ();//Cancel timed Task Et.addcanceljob ();

There will be 2 results after execution.

1. The program will perform the tasks in the current queue.

2. The scheduled task will be canceled and the task will no longer be assigned by schedule after the next minute.

Task Execution Results:

Cancel the response of the task:

Note the last line ...

Using Nodejs+redis to develop a message queue and timed task processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.