source: DCWoRMS/branches/coolemall/src/schedframe/scheduling/tasks/Job.java @ 1396

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