Schedule your Azure Mobile Service Pinger

This is a continuation of my previous blog Create your own monitor with AMS. In my previous blog I had mentioned how we can create an URL ping monitor using Node.JS nodes in AMS. However there was something missing!

For any URL or service monitoring it does not make any sense until and unless it is scheduled. The actual flow should be a background task which will be scheduled after a specific time interval and execute the business logic which will do the pinging request. This was a little bit difficult until few days ago, when the Azure Mobile service team released Scheduler service.

Scheduler scripts were used to

1.       Support background processing

2.       Prune unnecessary data

3.       Send push notification after an interval

Etc.

To explore this more I started integrating the Ping script

Step1: Go to your azure management portal -> Click on your Mobile Service.


Step 2: Double click on the service and click on the scheduler tab


Step 3: On clicking “Create a scheduled job” you will be asked about the Job Name and other details



Here I have set the scheduler to 15 minutes. One can also set the same to a higher timespan or on demand. Click on confirm icon.

Step 4: Once the Scheduler Job gets created by default it will be in Disable state.


We will enable this script later after writing our logic in it.
Step 5: Double click on the job and then click on Script tab



Comment the console.warn() statement and paste the following script inside the function Pinger(). Here in this sample my data base is the same which was created during installation of the sample TodoList

Following  is the pinger script:
function Pinger() {

    //Import the modules that you will require to perform this request

    var req = require('request');

    var util = require('util');

    var sql;

    var responseStatus;

    var time = new Date();  

    //Send request to the url which you want to monitor. Note this is a get request

    req.get({

    uri:"http://www.microsoft.com/",

    headers:{'content-type': 'application/x-www-form-urlencoded'},      

   }, 

   //Write the callback module for the same

   function (error, response, body) {

   if (!error && response.statusCode == 200) {

       //If the response is successful insert the result with the status code in the TodoItem table

       responseStatus = "Success";            

    }
    else
    {
        //In case of failure insert the failure response code in the TodoItem table

        responseStatus = "Failure";       

    }

   

    //Insert command in SQL

    sql = "INSERT INTO TodoItem (text) VALUES ('"+time.toDateString()+" "+time.getHours()+":"+

       time.getMinutes()+":"+time.getSeconds()+" Status:"+responseStatus+" , Response Code: "+

       util.format('%s',response.statusCode)+"')";     

    //Execute the SQl statement  

    mssql.query(sql, {

        success: function(results){

            console.log(results);

                    }          
    })
    }
);}

Here I am sending a get request to Microsoft.com and verifying after every 15 minutes whether it is up or not. I am storing the response status code with timestamp in the SQL Database TodoItem table.
To test the script , click on the Run Once option from the below menu bar.


Step 6: We can verify this by going to the TodoItem mobile service and the clicking on TodoList Table


In the above table I can verify the script is successfully pinging the specified URL and logging the response in the table.
Step 7: Now the last thing that has to be done is to enable the scheduler so that it continues the background process which will be doing a schedule monitoring.



That’s it! Our Pinger application is fully ready. As an advance version of this, on failure of any url ping a notification can be sent in a form of push notification or email to the system administrator through SendGrid or a sms through Twillio who can take quick action.

Hope you liked this post.

Wish you all a very happy and prosperous New Year!! J

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module