Project

General

Profile

1
package eu.dnetlib.msro.controllers;
2

    
3
import java.io.IOException;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9
import java.util.stream.Collectors;
10

    
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.dom4j.DocumentHelper;
15
import org.dom4j.Element;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.web.bind.annotation.PathVariable;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
import org.springframework.web.bind.annotation.RestController;
21

    
22
import com.fasterxml.jackson.databind.ObjectMapper;
23

    
24
import eu.dnetlib.clients.msro.MsroWorkerRunningInstance;
25
import eu.dnetlib.clients.msro.TerminatedWfDesc;
26
import eu.dnetlib.enabling.annotations.DnetService;
27
import eu.dnetlib.enabling.annotations.DnetServiceType;
28
import eu.dnetlib.msro.exceptions.MSROException;
29
import eu.dnetlib.msro.workers.aggregation.collect.plugins.CollectorPlugin;
30
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorParam;
31
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorPlugin;
32
import eu.dnetlib.msro.workers.aggregation.collect.plugins.ProtocolParameterType;
33
import eu.dnetlib.msro.workflows.ProcessStatus;
34
import eu.dnetlib.msro.workflows.WorkflowInstance;
35
import eu.dnetlib.msro.workflows.WorkflowProcessInfo;
36
import eu.dnetlib.msro.workflows.nodes.collect.CollectorPluginEnumerator;
37
import eu.dnetlib.msro.workflows.procs.ProcessExecutor;
38
import eu.dnetlib.msro.workflows.procs.ProcessFactory;
39
import eu.dnetlib.msro.workflows.procs.ProcessRegistry;
40
import eu.dnetlib.msro.workflows.procs.WorkflowProcess;
41
import eu.dnetlib.msro.workflows.util.NodeHelper;
42
import eu.dnetlib.services.BaseService;
43
import eu.dnetlib.services.ServiceRunningInstance;
44
import eu.dnetlib.services.ServiceStatus;
45
import eu.dnetlib.services.async.AsyncMethodException;
46
import eu.dnetlib.services.async.AsyncRunnable;
47
import eu.dnetlib.services.async.AsyncServerCallback;
48
import eu.dnetlib.services.async.HasAsyncMethods;
49

    
50
@RestController
51
@RequestMapping("/worker")
52
@DnetService(DnetServiceType.msroWorker)
53
public class MsroWorkerController extends BaseService implements HasAsyncMethods {
54

    
55
	private static final Log log = LogFactory.getLog(MsroWorkerController.class);
56

    
57
	private static final long MAX_NUMBER_OF_TERMINATED_WFS = 5;
58

    
59
	@Autowired
60
	private ProcessFactory processFactory;
61

    
62
	@Autowired
63
	private ProcessRegistry processRegistry;
64

    
65
	@Autowired
66
	private ProcessExecutor processExecutor;
67

    
68
	@Autowired
69
	private NodeHelper nodeHelper;
70

    
71
	@Autowired
72
	private CollectorPluginEnumerator collectorPlugins;
73

    
74
	@RequestMapping(value = "nodes", method = RequestMethod.GET)
75
	public Set<String> nodes() {
76
		return nodeHelper.getValidTypes().keySet();
77
	}
78

    
79
	@RequestMapping(value = "collector/plugins", method = RequestMethod.GET)
80
	public List<String> plugins() {
81
		return collectorPlugins.getAll().stream().map(CollectorPlugin::getProtocol).sorted().collect(Collectors.toList());
82
	}
83

    
84
	@RequestMapping(value = "pause", method = RequestMethod.GET)
85
	public ServiceRunningInstance pause() {
86
		processExecutor.setPaused(true);
87
		return getStatus();
88
	}
89

    
90
	@RequestMapping(value = "resume", method = RequestMethod.GET)
91
	public ServiceRunningInstance resume() {
92
		processExecutor.setPaused(false);
93
		return getStatus();
94
	}
95

    
96
	@RequestMapping(value = "proc/{procId}", method = RequestMethod.GET)
97
	public WorkflowProcessInfo processInfo(@PathVariable final String procId) {
98
		return processRegistry.findProcess(procId);
99
	}
100

    
101
	@RequestMapping(value = "kill/{procId}", method = RequestMethod.GET)
102
	public boolean killProcess(@PathVariable final String procId) {
103
		processRegistry.findProcess(procId).kill();
104
		return true;
105
	}
106

    
107
	@Override
108
	public List<Element> geXmlProfileSections() {
109
		final Element nodes = DocumentHelper.createElement("WORKFLOW_NODES");
110
		final Element collPlugins = DocumentHelper.createElement("COLLECTOR_PLUGINS");
111

    
112
		nodeHelper.getValidTypes().entrySet().forEach(e -> {
113
			final Element n = nodes.addElement("NODE");
114
			n.addAttribute("type", e.getKey());
115
			n.addAttribute("class", e.getValue().getName());
116
		});
117

    
118
		collectorPlugins.getAll().stream()
119
				.map(o -> o.getClass().getAnnotation(DnetCollectorPlugin.class))
120
				.forEach(ann -> {
121
					final Element plugin = collPlugins.addElement("PLUGIN");
122
					plugin.addAttribute("protocol", ann.value());
123
					for (final DnetCollectorParam ap : ann.parameters()) {
124
						final Element p = plugin.addElement("PARAM");
125
						p.addAttribute("name", ap.value());
126
						p.addAttribute("type", ap.type().toString());
127
						p.addAttribute("optional", Boolean.toString(ap.optional()));
128
						if (StringUtils.isNotBlank(ap.regex()) && (ap.type() == ProtocolParameterType.TEXT)) {
129
							p.addAttribute("regex", ap.regex());
130
						}
131
					}
132
				});
133

    
134
		return Arrays.asList(nodes, collPlugins);
135
	}
136

    
137
	@Override
138
	public ServiceRunningInstance getStatus() {
139

    
140
		return MsroWorkerRunningInstance.newInstance(
141
				getProfileId(),
142
				getBaseUrl(),
143
				baseDir(), processExecutor.isPaused() ? ServiceStatus.PAUSED : ServiceStatus.ACTIVE,
144
				processRegistry.countRunningWfs(),
145
				processRegistry.countQueuedWfs(),
146
				processRegistry.listTerminated()
147
						.stream()
148
						.limit(MAX_NUMBER_OF_TERMINATED_WFS)
149
						.map(wf -> new TerminatedWfDesc(wf.getId(), wf.getStatus() == ProcessStatus.SUCCESS, wf.getEndDate()))
150
						.collect(Collectors.toList()));
151
	}
152

    
153
	@Override
154
	public AsyncRunnable createAsyncJob(final String method, final String jsonParams, final AsyncServerCallback callback) throws AsyncMethodException {
155
		final ObjectMapper mapper = new ObjectMapper();
156

    
157
		try {
158
			switch (method) {
159
			case "startWorkflow":
160
				return new AsyncRunnable() {
161

    
162
					private WorkflowProcess proc;
163

    
164
					@Override
165
					public Map<String, String> prepare() throws AsyncMethodException {
166
						try {
167
							proc = processFactory.newProcess(getBaseUrl(), mapper.readValue(jsonParams, WorkflowInstance.class), callback);
168

    
169
							final Map<String, String> map = new HashMap<>();
170
							map.put("procId", proc.getId());
171
							return map;
172
						} catch (final MSROException | IOException e) {
173
							throw new AsyncMethodException(e);
174
						}
175
					}
176

    
177
					@Override
178
					public void execute() throws AsyncMethodException {
179
						try {
180
							processExecutor.execute(proc);
181
						} catch (final MSROException e) {
182
							throw new AsyncMethodException(e);
183
						}
184
					}
185

    
186
				};
187

    
188
			default:
189
				log.warn("Invalid method: " + method);
190
				throw new AsyncMethodException("Invalid method: " + method);
191
			}
192
		} catch (final Throwable e) {
193
			log.warn("Error executing method: " + method, e);
194
			throw new AsyncMethodException("Error executing method: " + method, e);
195
		}
196

    
197
	}
198

    
199
}
    (1-1/1)