1
|
package eu.dnetlib.msro.workflows.procs;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.Map;
|
6
|
import java.util.concurrent.Executors;
|
7
|
import java.util.concurrent.TimeUnit;
|
8
|
|
9
|
import javax.annotation.PostConstruct;
|
10
|
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.apache.commons.lang3.math.NumberUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.Element;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
22
|
import eu.dnetlib.enabling.common.Stoppable;
|
23
|
import eu.dnetlib.enabling.common.StoppableDetails;
|
24
|
import eu.dnetlib.msro.exceptions.MSROException;
|
25
|
import eu.dnetlib.msro.workflows.graph.Graph;
|
26
|
import eu.dnetlib.msro.workflows.graph.GraphLoader;
|
27
|
import eu.dnetlib.msro.workflows.util.ProcessCallback;
|
28
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
29
|
|
30
|
@Component
|
31
|
public class WorkflowExecutor implements Stoppable {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(WorkflowExecutor.class);
|
34
|
|
35
|
@Autowired
|
36
|
private InformationServiceClient isLookup;
|
37
|
@Autowired
|
38
|
private GraphLoader graphLoader;
|
39
|
@Autowired
|
40
|
private ProcessRegistry processRegistry;
|
41
|
@Autowired
|
42
|
private ProcessFactory processFactory;
|
43
|
@Autowired
|
44
|
private ProcessEngine processEngine;
|
45
|
|
46
|
private boolean paused = false;
|
47
|
|
48
|
@PostConstruct
|
49
|
public void init() {
|
50
|
|
51
|
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
|
52
|
if (isPaused() || (processRegistry.countRunningWfs() >= WorkflowsConstants.MAX_RUNNING_PROCS_SIZE)) { return; }
|
53
|
|
54
|
final WorkflowProcess process = processRegistry.nextProcessToStart();
|
55
|
if (process != null) {
|
56
|
processEngine.startProcess(process);
|
57
|
} else {
|
58
|
log.debug("WorkflowProcess queue is empty");
|
59
|
}
|
60
|
}, 10, 10, TimeUnit.SECONDS);
|
61
|
}
|
62
|
|
63
|
public String startRepoHiWorkflow(final String profileId, final String dsId, final String iface, final ProcessCallback processCallback, final String parent)
|
64
|
throws Exception {
|
65
|
|
66
|
if (isPaused()) {
|
67
|
log.warn("Wf " + profileId + " not launched, because WorkflowExecutor is preparing for shutdown");
|
68
|
throw new MSROException("WorkflowExecutor is preparing for shutdown");
|
69
|
}
|
70
|
|
71
|
try {
|
72
|
final String profile = isLookup.getProfile(profileId);
|
73
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
74
|
|
75
|
final String name = doc.valueOf("//WORKFLOW_NAME");
|
76
|
final String family = doc.valueOf("//WORKFLOW_FAMILY");
|
77
|
final int priority = NumberUtils.toInt("//WORKFLOW_PRIORITY", WorkflowsConstants.DEFAULT_WF_PRIORITY);
|
78
|
final boolean isReady = doc.valueOf("//CONFIGURATION/@status").equals(WorkflowsConstants.WorkflowStatus.EXECUTABLE.toString());
|
79
|
final boolean isDisabled = doc.valueOf("//CONFIGURATION/@start").equals("disabled");
|
80
|
|
81
|
if (!isReady || isDisabled) {
|
82
|
log.warn("Wf " + profileId + " not launched, because it is not ready to start or it is disabled");
|
83
|
throw new MSROException("Workflow " + profileId + " is not ready to start");
|
84
|
}
|
85
|
|
86
|
final Map<String, String> globalParams = new HashMap<String, String>();
|
87
|
for (final Object o : doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM")) {
|
88
|
final Element p = (Element) o;
|
89
|
globalParams.put(p.valueOf("@name"), p.getTextTrim());
|
90
|
}
|
91
|
|
92
|
final Graph graph = graphLoader.loadGraph(doc, globalParams);
|
93
|
|
94
|
final WorkflowProcess process =
|
95
|
processFactory.newProcess(name, family, dsId, iface, graph, priority, profileId, false, globalParams, processCallback, parent);
|
96
|
|
97
|
return processRegistry.registerProcess(process, profileId);
|
98
|
} catch (final Exception e) {
|
99
|
log.error("Error parsing workflow: " + profileId, e);
|
100
|
throw new MSROException("Error parsing workflow");
|
101
|
}
|
102
|
}
|
103
|
|
104
|
public String startWorkflow(final String profileId, final ProcessCallback processCallback, final String parent) throws Exception {
|
105
|
|
106
|
if (isPaused()) {
|
107
|
log.warn("Wf " + profileId + " not launched, because WorkflowExecutor is preparing for shutdown");
|
108
|
throw new MSROException("WorkflowExecutor is preparing for shutdown");
|
109
|
}
|
110
|
|
111
|
try {
|
112
|
final String profile = isLookup.getProfile(profileId);
|
113
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
114
|
|
115
|
final String name = doc.valueOf("//WORKFLOW_NAME");
|
116
|
final String family = doc.valueOf("//WORKFLOW_FAMILY");
|
117
|
final int priority = NumberUtils.toInt("//WORKFLOW_PRIORITY", WorkflowsConstants.DEFAULT_WF_PRIORITY);
|
118
|
final boolean isReady = doc.valueOf("//CONFIGURATION/@status").equals(WorkflowsConstants.WorkflowStatus.EXECUTABLE.toString());
|
119
|
final boolean isDisabled = doc.valueOf("//CONFIGURATION/@start").equals("disabled");
|
120
|
final String dsId = doc.valueOf("//DATASOURCE/@id");
|
121
|
final String iface = doc.valueOf("//DATASOURCE/@interface");
|
122
|
|
123
|
if (!isReady || isDisabled) {
|
124
|
log.warn("Wf " + profileId + " not launched, because it is not ready to start or it is disabled");
|
125
|
throw new MSROException("Workflow " + profileId + " is not ready to start");
|
126
|
}
|
127
|
|
128
|
final Map<String, String> globalParams = new HashMap<String, String>();
|
129
|
for (final Object o : doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM")) {
|
130
|
final Element p = (Element) o;
|
131
|
globalParams.put(p.valueOf("@name"), p.getTextTrim());
|
132
|
}
|
133
|
|
134
|
final Graph graph = graphLoader.loadGraph(doc, globalParams);
|
135
|
|
136
|
final WorkflowProcess process =
|
137
|
processFactory.newProcess(name, family, dsId, iface, graph, priority, profileId, false, globalParams, processCallback, parent);
|
138
|
|
139
|
return processRegistry.registerProcess(process, profileId);
|
140
|
} catch (final Exception e) {
|
141
|
log.error("Error parsing workflow: " + profileId, e);
|
142
|
throw new MSROException("Error parsing workflow");
|
143
|
}
|
144
|
}
|
145
|
|
146
|
public String startWorkflowTemplate(final String profileId,
|
147
|
final String name,
|
148
|
final String family,
|
149
|
final int priority,
|
150
|
final String dsId,
|
151
|
final String iface,
|
152
|
final Map<String, String> params,
|
153
|
final ProcessCallback processCallback,
|
154
|
final String parent) throws Exception {
|
155
|
|
156
|
if (isPaused()) {
|
157
|
log.warn("Wf template " + profileId + " not launched, because WorkflowExecutor is preparing for shutdown");
|
158
|
throw new MSROException("WorkflowExecutor is preparing for shutdown");
|
159
|
}
|
160
|
|
161
|
try {
|
162
|
final String profile = isLookup.getProfile(profileId);
|
163
|
final Document doc = new SAXReader().read(new StringReader(profile));
|
164
|
|
165
|
final Map<String, String> globalParams = new HashMap<String, String>();
|
166
|
for (final Object o : doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM")) {
|
167
|
final Element p = (Element) o;
|
168
|
final String pname = p.valueOf("@name");
|
169
|
if (StringUtils.isNotBlank(params.get(pname))) {
|
170
|
globalParams.put(pname, params.get(pname));
|
171
|
} else if (p.selectSingleNode("@default") != null) {
|
172
|
globalParams.put(pname, p.valueOf("@default"));
|
173
|
} else if (!StringUtils.equalsIgnoreCase(p.valueOf("@required"), "true")) {
|
174
|
globalParams.put(pname, "");
|
175
|
} else {
|
176
|
throw new MSROException("A required parameter is missing in wf template:" + pname);
|
177
|
}
|
178
|
}
|
179
|
|
180
|
final Graph graph = graphLoader.loadGraph(doc, globalParams);
|
181
|
|
182
|
final WorkflowProcess process =
|
183
|
processFactory.newProcess(name, family, dsId, iface, graph, priority, profileId, true, globalParams, processCallback, parent);
|
184
|
|
185
|
return processRegistry.registerProcess(process);
|
186
|
} catch (final Exception e) {
|
187
|
log.error("Error starting workflow template: " + profileId, e);
|
188
|
throw new MSROException("Error starting workflow template", e);
|
189
|
}
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public void stop() {
|
194
|
paused = true;
|
195
|
}
|
196
|
|
197
|
@Override
|
198
|
public void resume() {
|
199
|
paused = false;
|
200
|
}
|
201
|
|
202
|
@Override
|
203
|
public StoppableDetails getStopDetails() {
|
204
|
final int count = processRegistry.countRunningWfs();
|
205
|
|
206
|
final StoppableDetails.StopStatus status =
|
207
|
isPaused() ? (count == 0 ? StoppableDetails.StopStatus.STOPPED : StoppableDetails.StopStatus.STOPPING) : StoppableDetails.StopStatus.RUNNING;
|
208
|
|
209
|
return new StoppableDetails("D-NET workflow manager", "Running workflows: " + count, status);
|
210
|
}
|
211
|
|
212
|
public boolean isPaused() {
|
213
|
return paused;
|
214
|
}
|
215
|
|
216
|
public void setPaused(final boolean paused) {
|
217
|
this.paused = paused;
|
218
|
}
|
219
|
|
220
|
}
|