KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > Executor


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 JavaDoc;
8 import java.io.IOException JavaDoc;
9
10
11 /**
12  * Thread that executes builds.
13  *
14  * @author Kohsuke Kawaguchi
15  */

16 public class Executor extends Thread JavaDoc {
17     private final Computer owner;
18     private final Queue queue;
19
20     private AbstractBuild<?,?> build;
21
22     private long startTime;
23
24     /**
25      * Executor number that identifies it among other executors for the same {@link Computer}.
26      */

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                     // we've got too many executors.
45
owner.removeExecutor(this);
46                     return;
47                 }
48             }
49
50             try {
51                 AbstractProject p = queue.pop();
52                 build = p.newBuild();
53             } catch (InterruptedException JavaDoc e) {
54                 continue;
55             } catch (IOException JavaDoc e) {
56                 e.printStackTrace();
57                 continue;
58             }
59             startTime = System.currentTimeMillis();
60             try {
61                 build.run();
62             } catch (Throwable JavaDoc e) {
63                 // for some reason the executor died. this is really
64
// a bug in the code, but we don't want the executor to die,
65
// so just leave some info and go on to build other things
66
e.printStackTrace();
67             }
68             build = null;
69         }
70     }
71
72     /**
73      * Returns the current {@link Build} this executor is running.
74      *
75      * @return
76      * null if the executor is idle.
77      */

78     public AbstractBuild getCurrentBuild() {
79         return build;
80     }
81
82     /**
83      * Gets the executor number that uniquely identifies it among
84      * other {@link Executor}s for the same computer.
85      *
86      * @return
87      * a sequential number starting from 0.
88      */

89     public int getNumber() {
90         return number;
91     }
92
93     /**
94      * Returns true if this {@link Executor} is ready for action.
95      */

96     public boolean isIdle() {
97         return build==null;
98     }
99
100     /**
101      * Returns the progress of the current build in the number between 0-100.
102      *
103      * @return -1
104      * if it's impossible to estimate the progress.
105      */

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     /**
119      * Computes a human-readable text that shows the expected remaining time
120      * until the build completes.
121      */

122     public String JavaDoc 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     /**
136      * Stops the current build.
137      */

138     public void doStop( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
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     /**
151      * Returns the executor of the current thread.
152      */

153     public static Executor currentExecutor() {
154         return (Executor)Thread.currentThread();
155     }
156 }
157
Popular Tags