1 package hudson.model; 2 3 import hudson.Util; 4 import org.kohsuke.stapler.StaplerRequest; 5 import org.kohsuke.stapler.StaplerResponse; 6 7 import javax.servlet.ServletException ; 8 import java.io.IOException ; 9 10 11 16 public class Executor extends Thread { 17 private final Computer owner; 18 private final Queue queue; 19 20 private AbstractBuild<?,?> build; 21 22 private long startTime; 23 24 27 private int number; 28 29 public Executor(Computer owner) { 30 super("Executor #"+owner.getExecutors().size()+" for "+owner.getDisplayName()); 31 this.owner = owner; 32 this.queue = Hudson.getInstance().getQueue(); 33 this.number = owner.getExecutors().size(); 34 start(); 35 } 36 37 public void run() { 38 while(true) { 39 if(Hudson.getInstance().isTerminating()) 40 return; 41 42 synchronized(owner) { 43 if(owner.getNumExecutors()<owner.getExecutors().size()) { 44 owner.removeExecutor(this); 46 return; 47 } 48 } 49 50 try { 51 AbstractProject p = queue.pop(); 52 build = p.newBuild(); 53 } catch (InterruptedException e) { 54 continue; 55 } catch (IOException e) { 56 e.printStackTrace(); 57 continue; 58 } 59 startTime = System.currentTimeMillis(); 60 try { 61 build.run(); 62 } catch (Throwable e) { 63 e.printStackTrace(); 67 } 68 build = null; 69 } 70 } 71 72 78 public AbstractBuild getCurrentBuild() { 79 return build; 80 } 81 82 89 public int getNumber() { 90 return number; 91 } 92 93 96 public boolean isIdle() { 97 return build==null; 98 } 99 100 106 public int getProgress() { 107 AbstractBuild b = build.getProject().getLastSuccessfulBuild(); 108 if(b==null) return -1; 109 110 long duration = b.getDuration(); 111 if(duration==0) return -1; 112 113 int num = (int)((System.currentTimeMillis()-startTime)*100/duration); 114 if(num>=100) num=99; 115 return num; 116 } 117 118 122 public String getEstimatedRemainingTime() { 123 AbstractBuild b = build.getProject().getLastSuccessfulBuild(); 124 if(b==null) return "N/A"; 125 126 long duration = b.getDuration(); 127 if(duration==0) return "N/A"; 128 129 long eta = duration-(System.currentTimeMillis()-startTime); 130 if(eta<=0) return "N/A"; 131 132 return Util.getTimeSpanString(eta); 133 } 134 135 138 public void doStop( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 139 if(!Hudson.adminCheck(req,rsp)) 140 return; 141 142 interrupt(); 143 rsp.forwardToPreviousPage(req); 144 } 145 146 public Computer getOwner() { 147 return owner; 148 } 149 150 153 public static Executor currentExecutor() { 154 return (Executor)Thread.currentThread(); 155 } 156 } 157 | Popular Tags |