Version Differences for Agent Filter

(Agent Filter Script to Choose the Agent of the First Job of the Originating Workflow)
Line 63:
       
  return Where.is(myWhere); </pre>    return Where.is(myWhere); </pre> 
       
    + = Agent Filter to Select the Agent Used in Previous Build Life =  
    + This is for sticky jobs. They will use the same agent as last time if applicable. This can only be used in originating workflow jobs and it does not work with iterated jobs.  
    + ==== AHPSCRIPTS-43 ====  
    + <pre>import com.urbancode.anthill3.domain.agent.Agent;  
    + import com.urbancode.anthill3.runtime.scripting.*;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.*;  
       
    + return new Where() {  
    + public Agent[] filter(Agent[] agents) {  
    + jobConfig = LookupContext.getCurrent().getJobConfig();  
    + previousBuildLife = BuildLifeLookup.getCurrent().getPrevBuildLife();  
    + if (previousBuildLife != null) {  
    + previousWorkflow = previousBuildLife.getOriginatingWorkflow();  
    + previousJobs = previousWorkflow.getJobTraceArray();  
    + for (int j=0; j<previousJobs.length; j++) {  
    + if (jobConfig.equals(previousJobs[j].getJobConfig())) {  
    + if (previousJobs[j].getAgent() != null) {  
    + //System.out.println("Selecting prior agent: " + previousJobs[j].getAgent());  
    + return new Agent[] { previousJobs[j].getAgent() };  
    + }  
    + break;  
    + }  
    + }  
    + }  
    + //System.out.println("Selecting all agents, previous not found");  
    + return agents;  
    + }  
    + };</pre>  
       
    + Same as above but it only puts the prior agent at the front of the list so it is priority but it can go to another agent if available:  
    + <pre>import com.urbancode.anthill3.domain.agent.Agent;  
    + import com.urbancode.anthill3.runtime.scripting.*;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.*;  
    + import com.urbancode.commons.util.CollectionUtil;  
       
    + return new Where() {  
    + public Agent[] filter(Agent[] agents) {  
    + agentList = new LinkedList();  
    + CollectionUtil.addAll(agentList, agents);  
    + jobConfig = LookupContext.getCurrent().getJobConfig();  
    + previousBuildLife = BuildLifeLookup.getCurrent().getPrevBuildLife();  
    + if (previousBuildLife != null) {  
    + previousWorkflow = previousBuildLife.getOriginatingWorkflow();  
    + previousJobs = previousWorkflow.getJobTraceArray();  
    + for (int j=0; j<previousJobs.length; j++) {  
    + if (jobConfig.equals(previousJobs[j].getJobConfig())) {  
    + if (previousJobs[j].getAgent() != null) {  
    + System.out.println("Placing prior agent first in list: " + previousJobs[j].getAgent());  
    + Agent previousAgent = previousJobs[j].getAgent();  
    + agentList.remove(previousAgent);  
    + agentList.add(0, previousAgent);  
    + }  
    + break;  
    + }  
    + }  
    + }  
    + return (Agent[]) agentList.toArray(new Agent[agentList.size()]);  
    + }  
    + };</pre>