SetCronJob Nodejs Client Module

26 / Jul / 2016 by Prince Soni 0 comments

What is SetCronJob

SetCronJob is a module that provides you with the ability to manage your application scheduler online. Modern applications require some kind of a scheduler as part of the application to perform tasks on particular instances. Managing this scheduler on the server, either locally or on a distributed system  is tricky task. Also, if machine restarts all the servers need to be reconfigured again. SetCronJob solves this problem  by managing all the schedulers on their servers and by eliminating developer responsibility to manage those schedulers.

SetCronJob helps you maintain multiple schedulers for your application, thereby reducing developer headache. It also provides you with the facility to monitor scheduler log and statistics.

SetCronJob website has a web management dashboard that provides the facility to manage your scheduler and execution log. You can also  schedule/delete/reset any of the schedulers if required from the management console. SetcronJob application management console looks as follows:

Screenshot from 2016-07-14 15:22:25

Why SetCronJob

  • SetCronJob manages all the jobs on their server so application performance increases as no local job needs to be managed
  • Locally created cron jobs have to be reconfigured again or at least once when the server/machine restarts, which involves overhead for developers as they have to re-configure all the jobs again. Setcronjob manages all the jobs on their server  so that the machine or system restart issue is resolved.
  • Managing cron jobs in a cluster environment is always an issue  as many clusters can execute the same cron job at the same time. This issue is resolved as SetCronJob executes only once on the server.
  • SetCronJob is capable of holding data, that can again be passed into the application once the scheduler executes.This is useful in performing data cleaning tasks timely.
  • SetCronJob web management console helps you to add, update, delete, re-run cron jobs that provide admin functionality in the application.
  • Managing timezone with any scheduler has always been an issue. Setronob resolves this as it has a timezone parameter that works for all the timezones.

NodeJs SetCronJob NPM Module

SetCronJob  contains REST APIs which can be used to create/update/delete the scheduler. SetCronJob has different types of APIs for managing CronJob, Group, Account, and Server. Complete Documentation can be found here.

Setcronjob npm module provides a wrapper to the SetCronJob API for Node.js. It provides a feature to set online schedulers for your application. These schedulers can call any API or task on your server corresponding to different time settings. You can retrieve your daily logs with respective execution results and can enable or disable schedulers easily. Setcronjob APIs are categorized in four parts as follows:

  • Server
  • Group
  • Cron
  • Account

For more information on the API request and responses visit the SetCronJob API docs .
SetCronJob npm module can be found here.

Module Usage

You need a valid SetCronJob Token to work with this module, which you can obtain after signing up  here. Once the token is received it can be used  to instantiate this module and make the API call.

Lets look into process of creating online scheduler using Setcronjob npm Module.

Installation

[code]

npm install setcronjob //install locally

npm install -g setcronjob //install globally

npm install setcronjob –save //install locally and save to package json

[/code]

Initilzation

[code]
var setCronJob = require(‘setcronjob’);
var cronJob = new setCronJob(‘xxxxxxxx’);

[/code]

Creating Schedular

[code]
//Add cron job
var params = {
month: 3,
day: 25,
hour: 18,
minute: 0,
url: ‘https://test.com:9000/api/v1//execute’,
timezone: ‘Asia/Kolkata’,
postData: ‘{"postId":"xyz"}’,
httpMethod: ‘POST’
};
cronJob.cron.add(params, function (err, result) {
if (err) {
console.log("Error : ", err)
} else {
console.log("Result : ", result)
}
});
[/code]

Once a schedule is created it will give you complete information about time and data passed to it, response looks like –

[code]
{"data":{"id":132,"groupId":0,"minute":["41"],"hour":["3"],"day":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"],"month":["1","2","3","4","5","6","7","8","9","10","11","12"],"weekday":["2"],"timezone":"Europe\/London","url":"http:\/\/example.com\/cron.php","postData":"","fail":0,"status":0,"name":"weekly","notify":0,"points":1},"info":[],"status":"success","code":0}

[/code]

Update scheduler – Scheduler id can be used to update  scheduler.

[code]
//Edit cron
var params = {
   id: ‘xyz’,
   month: 4,
   day: 26,
   hour: 19,
   minute: 20,
   url: ‘https://daytest.com:9000/api/v1//execute’,
   timezone: ‘Asia/Kolkata’,
   postData: ‘{"postId":"xyz"}’,
   httpMethod: ‘POST’
};
cronJob.cron.edit(params, function (err, result) {
   if (err) {
         console.log("Error : ", err)
     } else {
        console.log("Result : ", result)
   }
});

[/code]

List/Get Schedular

[code]

//list cron for a token
cronJob.cron.list(function (err, result) {
           if (err) {
              console.log("Error : ", err)
           } else {
              console.log("Result : ", result)
       }
});

//get one cron job information
var params = {
     id: ‘xxxxxx’
};
cronJob.cron.get(params, function (err, result) {
         if (err) {
             console.log("Error : ", err)
         } else {
            console.log("Result : ", result)
        }
});

[/code]

Delete/Disable Schedular

[code]

//delete cron
var params = {
id: ‘xyz’
};
cronJob.cron.delete(params, function(err, result) {
if (err) {
console.log("Error : ", err)
} else {
console.log("Result : ", result)
}
});

//get logs
var params = {
id: ‘xyz’
};
cronJob.cron.logs(params, function(err, result) {
if (err) {
console.log("Error : ", err)
} else {
console.log("Result : ", result)
}
});

[/code]

Complete example for all  APIs can be found below –

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *