Name

global.SLARepairAJAXProcessor

Description

AJAX Processor endpoint for SLARepairClient Structure of request payload { repair_action recreate , when scheduled , table task , filter sys_idEQf6ed0972bae66af2298daa7515def0 , sys_id f6ed0972bae66af2298daa7515def0 , email email.address@domain.com } * repair_action The repair type you want to perform. Only recreate available (default recreate) * when When you want the repair to happen. Either now or scheduled (default now) * table The table name to use as a repair source * filter A filter to apply for all records to recreate * sys_id A single or array of sys_id to use as the rebuld source * email A single email address or array to notify of completion for scheduled jobs (not used for affectedRecords) Structure of response payload { request { request which generated this response }, result { count 300 }, status { code 200, info_msg An info message , err_msg An error message , warn_msg warning message , debug_msg debug message } } * request the request made for this response * result the result object for the request. * count the approximate number of records affected * status Status object containing return codes, messages etc.

Script

var SLARepairAJAXProcessor = Class.create();

SLARepairAJAXProcessor.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  initialize: function(request, responseXML, gc) {
      AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
      this.lu = new GSLog(SLARepair.LOG_PROPERTY, this.type);
      if (gs.getProperty(SLARepair.SLA_DATABASE_LOG, "db") === "node")
          this.lu.disableDatabaseLogs();
  },

  start: function() {
      var payload = this.getParameter("sysparm_payload");
      var request;
      if (!JSUtil.nil(payload))
          request = JSON.parse(payload);

      if (this.lu.atLevel(GSLog.DEBUG)) {
          if (request.table === SLAOfflineUpdateSNC.SLA_OFFLINE_UPDATE)
              this.lu.logDebug("start: Repairing (offline) " + request.table);
          else {
              if (!JSUtil.nil(request.sys_id))
                  this.lu.logDebug("start: Repairing (by sys_id) " + request.table + ":" + request.sys_id);
              else
                  this.lu.logDebug("start: Repairing (by filter) " + request.table + ":" + request.filter);
          }
      }
      var tableDisplayName = new GlideRecord(request.table).getClassDisplayValue();

      var worker = new GlideScriptedHierarchicalWorker();
      worker.setProgressName(gs.getMessage("Repair SLAs for {0}", tableDisplayName));
      worker.setScriptIncludeName("SLARepair");

      if (request.table === SLAOfflineUpdateSNC.SLA_OFFLINE_UPDATE) {
          worker.setScriptIncludeMethod("repairByOfflineUpdate");
          if (!JSUtil.nil(request.sys_id))
              worker.putMethodArg("sys_id", request.sys_id);
      } else {
          if (!JSUtil.nil(request.sys_id)) {
              worker.setScriptIncludeMethod("repairBySysId");
              worker.putMethodArg("sys_id", request.sys_id);
              worker.putMethodArg("table", request.table);
          } else {
              worker.setScriptIncludeMethod("repairByFilter");
              worker.putMethodArg("filter", request.filter);
              worker.putMethodArg("table", request.table);
          }
      }

      worker.setBackground(true);
      worker.start();

      return worker.getProgressID();
  },

  /**
   * validate that a repair can be performed.
   */
  validateRepair: function() {
      var resItem = this.newItem();
      var response = new SLARepairAJAXProcessor.Response();

      //JSON blob in payload.
      var strPayload = this.getParameter("payload");
      var request;
      if (!JSUtil.nil(strPayload))
          request = JSON.parse(strPayload);

      response.request = request;
      this._checkRequest(request, response);

      // If the request was not OK.
      if (response.status.code != 200) {
          resItem.setAttribute("payload", JSON.stringify(response));
          return resItem;
      }

      // We've checked everything, now we can do something with it.
      var slar = new SLARepair().setValidateOnly(true);
      response.result = this._runNow(slar, request);
      this._parseResult(response);
      resItem.setAttribute("payload", JSON.stringify(response));

      return resItem;
  },

  /**
   * Parse the output from the SLARepair
   */
  _parseResult: function(response) {

      if (typeof response.result === "undefined")
          response.result = {};

      //Check the audit information for the affected tables.
      if (typeof response.result.audit_info === "undefined")
          response.status.code = 503; //If there's no audit object something bad happened.
      else {
          // If we find one table that's audited the repair client will notify the user
          var hasAuditedTable = false;
          for (var table in response.result.audit_info)
              if (response.result.audit_info[table].audit_on === true)
                  hasAuditedTable = true;

          if (!hasAuditedTable)
              response.status.code = 503;
      }

      // Move messages into the status object
      if (typeof response.result.err_msg !== "undefined") {
          response.status.err_msg = response.result.err_msg;
          delete(response.result.err_msg);
      }

      if (typeof response.result.info_msg !== "undefined") {
          response.status.info_msg = response.result.info_msg;
          delete(response.result.info_msg);
      }

      if (typeof response.result.warn_msg !== "undefined") {
          response.status.warn_msg = response.result.warn_msg;
          delete(response.result.warn_msg);
      }

      //Remove message arrays from response status if there are no messages
      if (response.status.info_msg.length === 0)
          delete(response.status.info_msg);
      if (response.status.err_msg.length === 0)
          delete(response.status.err_msg);
      if (response.status.warn_msg.length === 0)
          delete(response.status.warn_msg);

      if (this.lu.atLevel(GSLog.DEBUG))
          this.lu.logDebug("[_parseResult] Response: " + JSON.stringify(response));
  },

  /**
   * Run the appropriate SLARepair method based on the request on the provided slaRepair instance.
   */
  _runNow: function(slaRepair, request) {
      var retVal;
      if (this.lu.atLevel(GSLog.DEBUG)) {
          if (request.table === SLAOfflineUpdateSNC.SLA_OFFLINE_UPDATE)
              this.lu.logDebug("Repairing (offline) " + request.table);
          else {
              if (!JSUtil.nil(request.sys_id))
                  this.lu.logDebug("Repairing (by sys_id) " + request.table + ":" + request.sys_id);
              else
                  this.lu.logDebug("Repairing (by filter) " + request.table + ":" + request.filter);
          }
      }

      if (request.table === SLAOfflineUpdateSNC.SLA_OFFLINE_UPDATE) {
          retVal = slaRepair.repairByOfflineUpdate(request.sys_id);
      } else {
          if (!JSUtil.nil(request.sys_id))
              retVal = slaRepair.repairBySysId(request.sys_id, request.table);
          else
              retVal = slaRepair.repairByFilter(request.filter, request.table);
      }
      return retVal;
  },

  /**
   * Check the request we received, if it has reasonable values etc.
   */
  _checkRequest: function(request, response) {
      if (JSUtil.nil(request)) {
          this.lu.logDebug("[_checkRequest] No request payload");
          response.status.code = 400;
          response.status.err_msg.push(gs.getMessage("A request was not provided"));
          return;
      }

      if (this.lu.atLevel(GSLog.DEBUG))
          this.lu.logDebug("[_checkRequest] Request: " + JSON.stringify(request));

      //Check we have what we need for a reasonable request.  Minimum params.
      if (request.table == null || (request.sys_id == null && request.filter == null && request.table !== SLAOfflineUpdateSNC.SLA_OFFLINE_UPDATE)) {
          this.lu.logDebug("[_checkRequest] Invalid request provided");
          response.status.err_msg.push(gs.getMessage("An invalid request was provided"));
          return;
      }

      //Set defaults if none or invalid provided
      if (JSUtil.nil(request.repair_action) || request.repair_action + "" != "recreate")
          request.repair_action = "recreate";

      if (!request.when || (request.when + "" != "now" && request.when + "" != "scheduled"))
          request.when = "now";
  },

  type: 'SLARepairAJAXProcessor'
});

SLARepairAJAXProcessor.Response = Class.create();
SLARepairAJAXProcessor.Response.prototype = {
  initialize: function() {
      this.status = {
          "code": 200,
          "info_msg": [],
          "err_msg": [],
          "warn_msg": []
      };
  },
  type: "SLARepairAJAXProcessor.Response"
};

Sys ID

f1e75252c32231003d2ae219cdba8fc3

Offical Documentation

Official Docs: