1 | package schedframe.scheduling.tasks; |
---|
2 | |
---|
3 | |
---|
4 | import org.qcg.broker.schemas.resreqs.ParentType; |
---|
5 | import org.qcg.broker.schemas.resreqs.ResourceRequirements; |
---|
6 | import org.qcg.broker.schemas.resreqs.types.TaskStatesName; |
---|
7 | |
---|
8 | import java.io.StringWriter; |
---|
9 | import java.io.Writer; |
---|
10 | import java.util.ArrayList; |
---|
11 | import java.util.Iterator; |
---|
12 | import java.util.List; |
---|
13 | |
---|
14 | import org.joda.time.DateTime; |
---|
15 | |
---|
16 | import qcg.shared.constants.BrokerConstants; |
---|
17 | |
---|
18 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
19 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * |
---|
24 | * @author Marcin Krystek |
---|
25 | * |
---|
26 | */ |
---|
27 | public class Job implements JobInterface<ResourceRequirements> { |
---|
28 | |
---|
29 | protected List<Task> tasks; |
---|
30 | protected int senderId; |
---|
31 | protected boolean isRegistered; |
---|
32 | |
---|
33 | public Job(String id){ |
---|
34 | tasks = new ArrayList<Task>(); |
---|
35 | } |
---|
36 | |
---|
37 | public Job(ResourceRequirements resourceRequirements) throws Exception{ |
---|
38 | org.qcg.broker.schemas.resreqs.Task task[] = resourceRequirements.getTask(); |
---|
39 | if(task == null || task.length == 0) |
---|
40 | throw new NoSuchFieldException("No tasks are defined for job."); |
---|
41 | |
---|
42 | this.tasks = new ArrayList<Task>(); |
---|
43 | |
---|
44 | for(int i = 0; i < task.length; i++){ |
---|
45 | this.tasks.add(new Task(task[i])); |
---|
46 | } |
---|
47 | |
---|
48 | isRegistered = true; |
---|
49 | } |
---|
50 | |
---|
51 | public void add(Task task) { |
---|
52 | this.tasks.add(task); |
---|
53 | } |
---|
54 | |
---|
55 | public String getId() { |
---|
56 | if(this.tasks.size() == 0) |
---|
57 | throw new RuntimeException("No tasks are defined for job, so it is not possible to obtain job id."); |
---|
58 | |
---|
59 | return this.tasks.get(0).getJobId(); |
---|
60 | } |
---|
61 | |
---|
62 | public List<Task> getTask() { |
---|
63 | return this.tasks; |
---|
64 | } |
---|
65 | |
---|
66 | public Task getTask(String taskId) throws NoSuchFieldException { |
---|
67 | if(taskId == null) |
---|
68 | throw new IllegalArgumentException("TaskId can not be null. Specify appropriate taskId."); |
---|
69 | |
---|
70 | if(this.tasks == null || this.tasks.size() == 0) |
---|
71 | throw new NoSuchFieldException("No tasks are defined for job."); |
---|
72 | |
---|
73 | Task retTask = null; |
---|
74 | |
---|
75 | Iterator<Task> itr = this.tasks.iterator(); |
---|
76 | while(itr.hasNext() && retTask == null){ |
---|
77 | Task task = itr.next(); |
---|
78 | if(taskId.equals(task.getId())){ |
---|
79 | retTask = task; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | if(retTask == null) |
---|
84 | throw new NoSuchFieldException("Task "+taskId + " is not available in job " + getId()); |
---|
85 | |
---|
86 | return retTask; |
---|
87 | } |
---|
88 | |
---|
89 | public int getTaskCount() { |
---|
90 | return this.tasks.size(); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | public ResourceRequirements getDescription() { |
---|
95 | |
---|
96 | ResourceRequirements resReq = new ResourceRequirements(); |
---|
97 | if(this.tasks == null) |
---|
98 | return resReq; |
---|
99 | |
---|
100 | Iterator<Task> itr = this.tasks.iterator(); |
---|
101 | |
---|
102 | while(itr.hasNext()){ |
---|
103 | Task task = (Task) itr.next(); |
---|
104 | resReq.addTask(task.getDescription()); |
---|
105 | } |
---|
106 | |
---|
107 | return resReq; |
---|
108 | } |
---|
109 | |
---|
110 | public String getDocument() throws Exception { |
---|
111 | ResourceRequirements resReq = getDescription(); |
---|
112 | Writer writer = new StringWriter(); |
---|
113 | |
---|
114 | resReq.marshal(writer); |
---|
115 | |
---|
116 | return writer.toString(); |
---|
117 | } |
---|
118 | |
---|
119 | public boolean isFinished(){ |
---|
120 | |
---|
121 | for(int i = 0; i < tasks.size(); i++){ |
---|
122 | //if(tasks.get(i).getStatus() != BrokerConstants.TASK_STATUS_FINISHED) |
---|
123 | // return false; |
---|
124 | if(!tasks.get(i).isFinished()) |
---|
125 | return false; |
---|
126 | } |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | public DateTime getSubmissionTimeToBroker(){ |
---|
131 | return tasks.get(0).getSubmissionTimeToBroker(); |
---|
132 | } |
---|
133 | |
---|
134 | public int getStatus(){ |
---|
135 | boolean isForAll = true; |
---|
136 | int baseStatus = tasks.get(0).getStatus(); |
---|
137 | |
---|
138 | for(int i = 1; i < tasks.size() && isForAll; i++){ |
---|
139 | Task t = tasks.get(i); |
---|
140 | isForAll = (t.getStatus() == baseStatus); |
---|
141 | switch(t.getStatus()){ |
---|
142 | case (int)BrokerConstants.TASK_STATUS_QUEUED: |
---|
143 | return (int)BrokerConstants.TASK_STATUS_QUEUED; |
---|
144 | case (int)BrokerConstants.TASK_STATUS_UNSUBMITTED: |
---|
145 | return (int)BrokerConstants.TASK_STATUS_UNSUBMITTED; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if(isForAll && baseStatus == BrokerConstants.TASK_STATUS_FINISHED) |
---|
150 | return (int)BrokerConstants.JOB_STATUS_FINISHED; |
---|
151 | |
---|
152 | return -1; |
---|
153 | } |
---|
154 | |
---|
155 | public boolean setStatus(String taskId, int status){ |
---|
156 | boolean found = false; |
---|
157 | for(int i = 0; i < tasks.size() && !found; i++){ |
---|
158 | Task t = tasks.get(i); |
---|
159 | if(taskId.equals(t.getId())){ |
---|
160 | try { |
---|
161 | t.setStatus(status); |
---|
162 | } catch (Exception e) { |
---|
163 | // TODO Auto-generated catch block |
---|
164 | e.printStackTrace(); |
---|
165 | } |
---|
166 | found = true; |
---|
167 | } |
---|
168 | } |
---|
169 | return found; |
---|
170 | } |
---|
171 | |
---|
172 | public void setSenderId(int id){ |
---|
173 | this.senderId = id; |
---|
174 | } |
---|
175 | |
---|
176 | public int getSenderId(){ |
---|
177 | return this.senderId; |
---|
178 | } |
---|
179 | |
---|
180 | public int getUserId(){ |
---|
181 | return this.senderId; |
---|
182 | } |
---|
183 | |
---|
184 | public void setStatus(int status) throws Exception{ |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | public boolean isRegistered() { |
---|
189 | return isRegistered; |
---|
190 | } |
---|
191 | |
---|
192 | public void register(JobRegistryImpl jobRegistry) { |
---|
193 | isRegistered = jobRegistry.addJob(this); |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | @Override |
---|
198 | public void accept(WorkloadUnitHandler wuh) { |
---|
199 | wuh.handleJob(this); |
---|
200 | } |
---|
201 | |
---|
202 | private List<Task> getReadyTasks(){ |
---|
203 | |
---|
204 | List<Task> readyTasks = new ArrayList<Task>(); |
---|
205 | int size = tasks.size(); |
---|
206 | |
---|
207 | for(int i = 0; i < size; i++){ |
---|
208 | int parCnt; |
---|
209 | int previousTaskReadyCnt = 0; |
---|
210 | Task task = tasks.get(i); |
---|
211 | if(task.getStatus() != (int)BrokerConstants.TASK_STATUS_UNSUBMITTED) |
---|
212 | continue; |
---|
213 | try{ |
---|
214 | parCnt = task.getDescription().getWorkflow().getParentCount(); |
---|
215 | } catch(Exception e){ |
---|
216 | parCnt = 0; |
---|
217 | } |
---|
218 | if(parCnt == 0) { |
---|
219 | readyTasks.add(task); |
---|
220 | } |
---|
221 | else { |
---|
222 | for(int j = 0; j < parCnt; j++){ |
---|
223 | ParentType par = task.getDescription().getWorkflow().getParent(j); |
---|
224 | if(par.getTriggerState().compareTo(TaskStatesName.FINISHED) == 0){ |
---|
225 | try { |
---|
226 | if(!getTask(par.getContent()).isFinished()){ |
---|
227 | break; |
---|
228 | } |
---|
229 | } catch (NoSuchFieldException e) { |
---|
230 | // TODO Auto-generated catch block |
---|
231 | e.printStackTrace(); |
---|
232 | } |
---|
233 | } |
---|
234 | previousTaskReadyCnt++; |
---|
235 | } |
---|
236 | |
---|
237 | if(previousTaskReadyCnt == parCnt && task.getDescription().getWorkflow().getAnd() != null) |
---|
238 | readyTasks.add(task); |
---|
239 | else if(previousTaskReadyCnt > 0 && task.getDescription().getWorkflow().getOr() != null) |
---|
240 | readyTasks.add(task); |
---|
241 | } |
---|
242 | } |
---|
243 | return readyTasks; |
---|
244 | } |
---|
245 | |
---|
246 | } |
---|