source: DCWoRMS/trunk/src/schedframe/scheduling/tasks/Job.java @ 477

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