Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleJob sorter
collapsetrue
var jobChainPath = null;
var jobChainNodeState = null;
var jobChainNodeNextState = null;

function spooler_init() {
  spooler_log.info( ".. performing spooler_init()" );

  jobChainPath = spooler_task.order.job_chain.path;
  jobChainNodeState = spooler_task.order.job_chain_node.state;
  jobChainNodeNextState = spooler_task.order.job_chain_node.next_state;

  return true;
}


function spooler_process() {
  var rc = true;

  // suspend all orders, sorting is done on spooler_exit()
  spooler_log.info( ".. suspending current order" );
  spooler_task.order.suspended = true;
  spooler_task.order.state = jobChainNodeState;

  return rc;
}


function spooler_exit() {
  spooler_log.info( ".. peforming spooler_exit()" );
  var rc = true;
  var orderList = Array();

  // select all orders of the current job node
  var response = spooler.execute_xml( "<show_job_chain job_chain='" + jobChainPath + "' what='job_chain_orders'/>" );
  var orderDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( response ) );
  var orderNodes = orderDOM.selectNodeList( "/spooler/answer/job_chain/job_chain_node[@state = '" + jobChainNodeState + "']/order_queue/order" );

  // traverse ordersorder list and checkadd iforders ordersto aresort completedarray
  for( orderIndex=0; orderIndex<orderNodes.getLength(); orderIndex++ ) {
    var orderNode = orderNodes.item(orderIndex);
    var orderID = orderDOM.selectSingleNodeValue( orderNode, "@id" );
    if (orderID == null) {
      continue;
    }
    spooler_log.info( ".... order found: " + orderID );
    orderList.push( orderID );
  }

  // alphabetical string sort
  orderList.sort(function(a, b){return (a > b) - (a < b) });
  // numeric sort
  // orderList.sort(function(a, b){return b - a) });

  for(i=0; i<orderList.length; i++) {
    spooler_log.info( ".... moving order: " + orderList[i] );
    var response = spooler.execute_xml( "<modify_order job_chain='" + jobChainPath + "' order='" + orderList[i] + "' state='" + jobChainNodeNextState + "' suspended='no'/>" );
    var orderDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( response ) );
    var errorCode = orderDOM.selectSingleNodeValue( "//ERROR/@code" );
    var errorText = orderDOM.selectSingleNodeValue( "//ERROR/@text" );
    if ( errorCode || errorText ) {
      spooler_log.error( "........ modify order state response: errorCode=" + errorCode + ", errorText=" + errorText );
      rc = false;
    }  
  }

  return rc;
}

...