1 | package schedframe.scheduling; |
---|
2 | |
---|
3 | import grms.shared.constants.BrokerConstants; |
---|
4 | import org.qcg.broker.schemas.resreqs.ResourceRequirements; |
---|
5 | |
---|
6 | import java.io.StringWriter; |
---|
7 | import java.io.Writer; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.List; |
---|
11 | |
---|
12 | import org.joda.time.DateTime; |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | * @author Marcin Krystek |
---|
18 | * |
---|
19 | */ |
---|
20 | public class Job implements JobInterface<ResourceRequirements> { |
---|
21 | |
---|
22 | protected List<Task> tasks; |
---|
23 | protected int senderId; |
---|
24 | |
---|
25 | public Job(String id){ |
---|
26 | tasks = new ArrayList<Task>(); |
---|
27 | } |
---|
28 | |
---|
29 | public Job(ResourceRequirements resourceRequirements) throws Exception{ |
---|
30 | org.qcg.broker.schemas.resreqs.Task task[] = resourceRequirements.getTask(); |
---|
31 | if(task == null || task.length == 0) |
---|
32 | throw new NoSuchFieldException("No tasks are defined for job."); |
---|
33 | |
---|
34 | this.tasks = new ArrayList<Task>(); |
---|
35 | |
---|
36 | for(int i = 0; i < task.length; i++){ |
---|
37 | this.tasks.add(new Task(task[i])); |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | public void add(Task task) { |
---|
43 | this.tasks.add(task); |
---|
44 | } |
---|
45 | |
---|
46 | public String getId() { |
---|
47 | if(this.tasks.size() == 0) |
---|
48 | throw new RuntimeException("No tasks are defined for job, so it is not possible to obtain job id."); |
---|
49 | |
---|
50 | return this.tasks.get(0).getJobId(); |
---|
51 | } |
---|
52 | |
---|
53 | public List<Task> getTask() { |
---|
54 | return this.tasks; |
---|
55 | } |
---|
56 | |
---|
57 | public Task getTask(String taskId) throws NoSuchFieldException { |
---|
58 | if(taskId == null) |
---|
59 | throw new IllegalArgumentException("TaskId can not be null. Specify appropriate taskId."); |
---|
60 | |
---|
61 | if(this.tasks == null || this.tasks.size() == 0) |
---|
62 | throw new NoSuchFieldException("No tasks are defined for job."); |
---|
63 | |
---|
64 | Task retTask = null; |
---|
65 | |
---|
66 | Iterator<Task> itr = this.tasks.iterator(); |
---|
67 | while(itr.hasNext() && retTask == null){ |
---|
68 | Task task = itr.next(); |
---|
69 | if(taskId.equals(task.getId())){ |
---|
70 | retTask = task; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if(retTask == null) |
---|
75 | throw new NoSuchFieldException("Task "+taskId + " is not available in job " + getId()); |
---|
76 | |
---|
77 | return retTask; |
---|
78 | } |
---|
79 | |
---|
80 | public int getTaskCount() { |
---|
81 | return this.tasks.size(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | public ResourceRequirements getDescription() { |
---|
86 | |
---|
87 | ResourceRequirements resReq = new ResourceRequirements(); |
---|
88 | if(this.tasks == null) |
---|
89 | return resReq; |
---|
90 | |
---|
91 | Iterator<Task> itr = this.tasks.iterator(); |
---|
92 | |
---|
93 | while(itr.hasNext()){ |
---|
94 | Task task = (Task) itr.next(); |
---|
95 | resReq.addTask(task.getDescription()); |
---|
96 | } |
---|
97 | |
---|
98 | return resReq; |
---|
99 | } |
---|
100 | |
---|
101 | public String getDocument() throws Exception { |
---|
102 | ResourceRequirements resReq = getDescription(); |
---|
103 | Writer writer = new StringWriter(); |
---|
104 | |
---|
105 | resReq.marshal(writer); |
---|
106 | |
---|
107 | return writer.toString(); |
---|
108 | } |
---|
109 | |
---|
110 | public boolean isFinished(){ |
---|
111 | |
---|
112 | for(int i = 0; i < tasks.size(); i++){ |
---|
113 | //if(tasks.get(i).getStatus() != BrokerConstants.TASK_STATUS_FINISHED) |
---|
114 | // return false; |
---|
115 | if(!tasks.get(i).isFinished()) |
---|
116 | return false; |
---|
117 | } |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | public DateTime getSubmissionTimeToBroker(){ |
---|
122 | return tasks.get(0).getSubmissionTimeToBroker(); |
---|
123 | } |
---|
124 | |
---|
125 | public int getStatus(){ |
---|
126 | boolean isForAll = true; |
---|
127 | int baseStatus = tasks.get(0).getStatus(); |
---|
128 | |
---|
129 | for(int i = 1; i < tasks.size() && isForAll; i++){ |
---|
130 | Task t = tasks.get(i); |
---|
131 | isForAll = (t.getStatus() == baseStatus); |
---|
132 | switch(t.getStatus()){ |
---|
133 | case (int)BrokerConstants.TASK_STATUS_QUEUED: |
---|
134 | return (int)BrokerConstants.TASK_STATUS_QUEUED; |
---|
135 | case (int)BrokerConstants.TASK_STATUS_UNSUBMITTED: |
---|
136 | return (int)BrokerConstants.TASK_STATUS_UNSUBMITTED; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | if(isForAll && baseStatus == BrokerConstants.TASK_STATUS_FINISHED) |
---|
141 | return (int)BrokerConstants.JOB_STATUS_FINISHED; |
---|
142 | |
---|
143 | return -1; |
---|
144 | } |
---|
145 | |
---|
146 | public boolean setStatus(String taskId, int status){ |
---|
147 | boolean found = false; |
---|
148 | for(int i = 0; i < tasks.size() && !found; i++){ |
---|
149 | Task t = tasks.get(i); |
---|
150 | if(taskId.equals(t.getId())){ |
---|
151 | try { |
---|
152 | t.setStatus(status); |
---|
153 | } catch (Exception e) { |
---|
154 | // TODO Auto-generated catch block |
---|
155 | e.printStackTrace(); |
---|
156 | } |
---|
157 | found = true; |
---|
158 | } |
---|
159 | } |
---|
160 | return found; |
---|
161 | } |
---|
162 | |
---|
163 | public void setSenderId(int id){ |
---|
164 | this.senderId = id; |
---|
165 | } |
---|
166 | |
---|
167 | public int getSenderId(){ |
---|
168 | return this.senderId; |
---|
169 | } |
---|
170 | |
---|
171 | public int getUserID(){ |
---|
172 | return this.senderId; |
---|
173 | } |
---|
174 | |
---|
175 | public void setStatus(int status) throws Exception{ |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | } |
---|