Skip adding a layer if the Map Service is down?

2740
2
03-08-2016 11:30 AM
BrettGreenfield__DNR_
Occasional Contributor II

I've got a web map that consumes a number of map services.  Most are hosted on our internal server, but a couple are hosted externally.  One particular service tends to go down somewhat regularly, and when this happens the map itself breaks (not entirely, but it loses enough functionality to be a problem).

Outside of manually checking the web map every morning, I have absolutely no idea how to deal with this.  What usually happens is the service goes down in the middle of the day, we have a complaint from a customer, and I have to manually update the code to skip adding that service to the map until I get word that the service is back up.  Is there a better way of handling this?  Some way to use JS to check to see if the service is up and running before attempting to add the service?

Thanks in advance!

Tags (1)
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Brett,


  Sure you would use esriRequest to get the json of the map service url and if that esriRequest fails then you do not add that service layer.

JeffJacobson
Occasional Contributor III
<script src="//cdn.polyfill.io/v2/polyfill.min.js?features=default,Promise"></script>

var sampleUrl = "//services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer";
var badUrl = "//services.arcgisonline.com/ArcGIS/rest/services/Reference/FakeService/MapServer"

/**
 * Tests a URL
 * @param {string} url
 * @returns {Promise}
 */
function testService(url) {
  return new Promise(function(resolve, reject) {
    var request = new XMLHttpRequest();
    request.open("get", url + "?f=json");
    request.onloadend = function() {
        if (this.status !== 200) {
          reject({error: this.status});
      } else {
          var response = JSON.parse(this.response);
        if (response.error) {
            reject(response);
        } else {
            resolve(response);
        }
      }
    }
    request.send();
  });
}

[sampleUrl, badUrl].forEach(function(url) {
  testService(url).then(function(response) {
    console.debug(response)
  }, function(error){
    console.error(error);
  });
});