1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.persistence.model.order.p2el;
18
19 import java.io.File;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.gwe.drivers.fileSystems.staging.FilesStager;
29 import org.gwe.p2elv2.P2ELDependentVariableNotResolvedException;
30 import org.gwe.p2elv2.P2ELFunctionNotSupported;
31 import org.gwe.p2elv2.PPermutation;
32 import org.gwe.p2elv2.PProcessorType;
33 import org.gwe.p2elv2.PStatementContext;
34 import org.gwe.p2elv2.model.PVariable;
35 import org.gwe.persistence.model.DaemonConfigDesc;
36 import org.gwe.persistence.model.JobInfo;
37 import org.gwe.persistence.model.JobInfoIdGenerator;
38 import org.gwe.persistence.model.live.JobLive;
39 import org.gwe.persistence.model.order.JobDescriptor;
40 import org.gwe.persistence.model.order.JobSideWorker;
41 import org.gwe.persistence.model.order.JobSideWorkerType;
42 import org.gwe.persistence.model.order.OSCommandDaemonRequest;
43 import org.gwe.utils.IOUtils;
44
45
46
47
48
49 public class PJobDescriptor extends JobDescriptor {
50
51 private static Log log = LogFactory.getLog(PJobDescriptor.class);
52
53 private static final String VAR_USER_HOME = "USER_HOME";
54 private static final String VAR_ORDER_ID = "ORDER_ID";
55 private static final String VAR_JOB_NUM = "JOB_NUM";
56 private static final String VAR_JOB_ID = "JOB_ID";
57
58 private PPermutation permutation;
59 private PPermutation compiledPermutation;
60 private String template;
61
62 public PJobDescriptor() {}
63
64 public PJobDescriptor(PPermutation permutation, String template) {
65 this.permutation = permutation;
66 this.template = template;
67 }
68
69 public void processSystemDependencies(DaemonConfigDesc config, JobInfo job) {
70 PStatementContext ctx = new PStatementContext(null, config.getKeys(), null);
71 ctx.addSystemVar(VAR_ORDER_ID, job.getOrder().getId() + "");
72 ctx.addSystemVar(VAR_JOB_NUM, job.getJobNum() + "");
73 ctx.addSystemVar(VAR_JOB_ID, new JobInfoIdGenerator().generateId(job));
74 try {
75 permutation.processSystemDependencies(ctx);
76 } catch (Exception e) {
77
78 log.warn("Problems processing variables with system value dependencies", e);
79 }
80 }
81
82 protected Map<JobSideWorkerType, JobSideWorker> initExecutionInternal(JobLive jobLive) throws P2ELDependentVariableNotResolvedException, P2ELFunctionNotSupported {
83 IOUtils.createLocalFolder(jobLive.getWorkspacePath());
84
85 compiledPermutation = permutation.clone();
86 compiledPermutation.processRuntimedependencies(createP2ELContext(jobLive));
87 final String command = compiledPermutation.merge(template);
88
89 Map<JobSideWorkerType, JobSideWorker> result = new HashMap<JobSideWorkerType, JobSideWorker>();
90
91 result.put(JobSideWorkerType.PRE, new PJobSideWorker(compiledPermutation, PProcessorType.PRE) {
92 protected void beforeExecuteProcessors(JobLive jobLive) throws Exception {
93 OSCommandDaemonRequest<?> osRequest = (OSCommandDaemonRequest<?>)jobLive.getInfo().getRequest();
94 osRequest.setWorkspacePath(jobLive.getWorkspacePath());
95 osRequest.setOSCommand(command);
96 }
97 });
98
99 result.put(JobSideWorkerType.POST, new PJobSideWorker(compiledPermutation, PProcessorType.POST) {
100 protected void beforeExecuteProcessors(JobLive jobLive) throws Exception {
101 JobInfo jobInfo = jobLive.getInfo();
102 Serializable requestResult = jobInfo.getExecution().getRequestResult();
103 if (requestResult instanceof String)
104 requestResult = (requestResult != null) ? IOUtils.readFile(new File((String)requestResult)) : "";
105 jobInfo.getExecution().setRequestResult(requestResult);
106 }
107 });
108
109 return result;
110 }
111
112 private PStatementContext createP2ELContext(JobLive jobLive) {
113 PStatementContext result = new PStatementContext(jobLive.getWorkspacePath(), jobLive.getOrderLive().getConfig().getKeys(), getFileStager(jobLive));
114 result.addRuntimeVar(VAR_USER_HOME, jobLive.getOrderLive().getUserHomePath());
115 return result;
116 }
117
118 public void finalizeExecution(JobLive jobLive) {
119 if (jobLive.canCleanUp())
120 getFileStager(jobLive).cleanUp(jobLive.getWorkspacePath());
121 }
122
123 private FilesStager getFileStager(JobLive jobLive) {
124 POrderDescriptor orderDesc = jobLive.getOrderDescriptor();
125 return orderDesc.getFileStager();
126 }
127
128 public Map<String, Object> getPermutationValues() {
129 return getPermutation().asFriendlyTreeMap();
130 }
131
132 public String toStringDetailed() {
133 StringBuffer result = new StringBuffer("TEMPLATE: " + template + "\nPERMUTATION VALUES: \n");
134 for (String entryStr : getPermutation().getEntriesStr()) {
135 result.append("\t");
136 result.append(entryStr);
137 result.append("\n");
138 }
139 return result.toString();
140 }
141
142 public String toString() {
143 return getPermutation().merge(template);
144 }
145
146 public String getTemplate() {
147 return template;
148 }
149
150 public PPermutation getPermutation() {
151 return (compiledPermutation == null) ? permutation : compiledPermutation;
152 }
153
154 public List<String> getUploads() {
155 List<String> result = new ArrayList<String>();
156 if (compiledPermutation != null) {
157 Map<String, Object> values = compiledPermutation.asFriendlyTreeMap();
158 for (PVariable var : compiledPermutation.keySet()) {
159 if (var.getFunctionInvocation().getFunctionName().equals("out"))
160 result.add(values.get(var.getFullName()).toString());
161 }
162 }
163 return result;
164 }
165 }
166