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:
领英推荐
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!