View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.persistence.model;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.persistence.Column;
24  import javax.persistence.Entity;
25  import javax.persistence.FetchType;
26  import javax.persistence.GeneratedValue;
27  import javax.persistence.Id;
28  import javax.persistence.Lob;
29  import javax.persistence.ManyToOne;
30  import javax.persistence.OneToOne;
31  import javax.persistence.Transient;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.gwe.persistence.model.live.JobLive;
36  import org.gwe.persistence.model.live.OrderLive;
37  import org.gwe.persistence.model.order.DaemonRequest;
38  import org.gwe.persistence.model.order.JobDescriptor;
39  import org.gwe.utils.IOUtils;
40  import org.hibernate.annotations.GenericGenerator;
41  
42  /**
43   * @author Marco Ruiz
44   * @since Aug 8, 2007
45   */
46  @GenericGenerator(name = "jobInfoIdGenerator", strategy = "org.gwe.persistence.model.JobInfoIdGenerator")
47  @Entity
48  public class JobInfo extends BaseModelInfo<String> {
49  	
50  	private static Log log = LogFactory.getLog(JobInfo.class);
51  
52  	private static final String JOB_WORKSPACE_PATH_PREFIX = "job-";
53  
54  	// Creation Time
55  	@Id
56  	@GeneratedValue(generator = "jobInfoIdGenerator")
57  	private String id;
58  
59  	@ManyToOne(fetch = FetchType.EAGER)
60  	private OrderInfo order;
61  	private int jobNum;
62  
63      @Lob @Column(length = 20480)
64      private JobDescriptor descriptor; 
65      
66  	@Lob @Column(length = 32768)
67  	protected DaemonRequest<?> request;
68  	
69  	@Column
70  	private int failures = 0;
71  
72  	@OneToOne(fetch = FetchType.EAGER)
73  	private JobExecutionInfo execution = null;
74  	
75  	@Transient
76  	private List<JobExecutionInfo> executions = new ArrayList<JobExecutionInfo>();
77  	
78  	public JobInfo() {}
79  
80  	public JobInfo(int jobNum, JobDescriptor descriptor, DaemonRequest<?> daemonRequest) {
81  		this.jobNum = jobNum;
82      	this.descriptor = descriptor;
83  		this.request = daemonRequest;
84  	}
85  
86  	// Creation Time
87  	public String getId() {
88  		return id;
89  	}
90  	
91  	public void setId(String id) {
92  		this.id = id;
93  	}
94  
95  	public OrderInfo getOrder() {
96  		return order;
97  	}
98  
99  	public void setOrder(OrderInfo order) {
100 		this.order = order;
101 	}
102 
103 	public int getJobNum() {
104 		return jobNum;
105 	}
106 	
107 	public JobDescriptor getDescriptor() {
108     	return descriptor;
109     }
110 
111 	public int getFailures() {
112     	return failures;
113     }
114 
115 	public JobExecutionInfo getExecution() {
116     	return execution;
117     }
118 
119 	public void setExecution(JobExecutionInfo execution) {
120     	this.execution = execution;
121     }
122 	
123 	public List<JobExecutionInfo> getExecutions() {
124     	return executions;
125     }
126 
127 	public void setExecutions(List<JobExecutionInfo> executions) {
128     	this.executions = executions;
129     }
130 
131 	public boolean computeFinishedExecutionStatus() {
132 		boolean success = !execution.hasFailed();
133 		boolean finished = success || failures >= order.getExecutionProfile().getMaxRetries();
134 		if (!success && !finished) {
135 			execution = null;
136 			failures++;
137 		}
138 		return finished;
139 	}
140 
141 /*
142 	public long getComputeTime() {
143 		long endTime = 0;
144 		if (whenProcessed != null && whenFailed == null) endTime = whenProcessed.getTime();  
145 		if (whenProcessed == null && whenFailed != null) endTime = whenFailed.getTime();  
146 		return (endTime == 0) ? 0 : endTime - whenStarted.getTime(); 
147 	}
148 */
149 
150 	public DaemonRequest<?> getRequest() {
151 		return request;
152 	}
153 
154 	public String getWorkspaceInDaemon(DaemonConfigDesc config) {
155 		return IOUtils.concatenatePaths(order.getWorkspaceInDaemon(config), JOB_WORKSPACE_PATH_PREFIX + getId());
156 	}
157 	
158 	public void preProcess(OrderLive orderLive) {
159 		JobLive jobLive = new JobLive(this, orderLive);
160 		try {
161 			request.setExecId(execution.getId());
162 			request.setKeys(orderLive.getConfig().getKeys());
163 			descriptor.initExecution(jobLive);
164 			descriptor.preProcess();
165 			logExecutionPhase(EventType.JOB_PREPARED, null);
166 		} catch (Throwable e) {
167 			logFailure(e);
168 			descriptor.finalizeExecution(jobLive);
169 		} 
170 	}
171 
172 	public void postProcess(OrderLive orderLive, Serializable result) {
173 		JobLive jobLive = new JobLive(this, orderLive);
174 		try {
175 			if (result instanceof Exception) {
176 				descriptor.postProcess();
177 				logFailure((Exception) result);
178 			} else {
179 				logExecutionPhase(EventType.JOB_PROCESSED, result);
180 				descriptor.postProcess();
181 				Serializable parsedResult = order.createResultParser().parseResult(execution.getRequestResult());
182 				logExecutionPhase(EventType.JOB_COMPLETED, parsedResult);
183 			}
184 		} catch (Throwable e) {
185 			logFailure(e);
186 		} finally {
187 			descriptor.finalizeExecution(jobLive);
188 		}
189 	}
190 
191 	private void logFailure(Throwable e) {
192 	    execution.logFailure(e);
193     }
194 
195 	private void logExecutionPhase(EventType evType, Serializable result) {
196 	    execution.logExecutionPhase(evType, result);
197     }
198 
199 	public void logCreateEvent() {
200 		logEvent(EventType.CREATED, getWhenCreated(), getOrder());
201 	}
202 
203 	public ModelSummary<String> createModelSummaryFor(EventType ev) {
204 		switch (ev) {
205 			case JOB_ASSIGNED:
206 //				return new ModelSummary<String>(this, allocation.getId());
207 				return new ModelSummary<String>(this, execution);
208 	
209 			default:
210 				return super.createModelSummaryFor(ev);
211 		}
212 	}
213 	
214 	public String toString() {
215 		return "jobId:" + getId();
216 	}
217 	
218 	// TODO: Revisit this pseudo hack
219 	public int getOrderId() {
220 		return Integer.parseInt(InfoUtils.getIdPieces(this)[0]); 
221 	}
222 }