ServiceNow FastBytes ::: Getting the best MID server for a REST call!

ServiceNow FastBytes ::: Getting the best MID server for a REST call!

Assuming that you have had experience with REST (outbound calls), you'd know that you'll have to use a MID server before making the call. Your code would probably be something like:

r.setMIDServer('YOUR-MID-SERVER');
r.execute();        

Now, the problem is:

  • You have hard-coded a MID server into your code - I'm pretty sure, most enterprises will have atleast a bunch of MID servers, not just one.
  • WHEN (not if), the said MID server goes down - or is overloaded for a few minutes, your REST calls are going to fail.


So, good solution would be to select a MID server dynamically - and one that is validated, up-and-running and if possible, with the least number of pending and processing jobs!

This can be done! The "ecc_agent_status" table has a lot of information about each MID server, so, the below script does just that - return a MID server name that's good and has the least jobs at the moment.

/** This is a static function in a Script-Include, if you need to use the code, you can use from line "var mids" onwards. 
*/ 

Consensus.get_midserver = function() {
    var mids = new GlideAggregate("ecc_agent_status");
    mids.addAggregate('SUM', 'pending_jobs');
    mids.addAggregate('SUM', 'processing_jobs');
    mids.groupBy('agent.name');
    mids.addQuery('agent.status', 'Up');
    mids.addQuery('agent.validated', true);
    mids.query();

    var least_jobs = Infinity;
    var selected_mid_server = "";

    while (mids.next()) {
        var pending_jobs = parseInt(mids.getAggregate('SUM', 'pending_jobs')) || 0;
        var processing_jobs = parseInt(mids.getAggregate('SUM', 'processing_jobs')) || 0;
        var total_jobs = pending_jobs + processing_jobs;

        if (total_jobs < least_jobs) {
            least_jobs = total_jobs;
            selected_mid_server = mids.getValue('agent.name');
        }
    }
    if (!selected_mid_server) {
        return false;
    }
    return selected_mid_server;
};        

Let me know your thoughts!

要查看或添加评论,请登录

Vinod Paul Fredrick的更多文章

社区洞察

其他会员也浏览了