Project

General

Profile

1
package eu.dnetlib.data.mdstore.modular.action;
2

    
3
import java.util.Map;
4
import java.util.concurrent.Executors;
5

    
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
7
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
8
import eu.dnetlib.rmi.data.MDStoreServiceException;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.beans.factory.annotation.Autowired;
12

    
13

    
14
class StatusThread extends Thread {
15
	boolean shouldUpdate = false;
16
	final BlackboardServerHandler handler;
17
	final BlackboardJob job;
18
	final long timeout;
19
	final MDStorePlugin currentPlugin;
20

    
21
	public StatusThread(final BlackboardServerHandler handler, final BlackboardJob job, final long timeout, final  MDStorePlugin currentPlugin) {
22
		this.job = job;
23
		this.handler = handler;
24
		this.timeout = timeout;
25
		this.currentPlugin = currentPlugin;
26
	}
27

    
28
	@Override
29
	public void run() {
30
		while (shouldUpdate) {
31
			job.getParameters().put("ongoingPercentage",currentPlugin.getStatus());
32
			handler.ongoing(job);
33
			try {
34
				sleep(timeout);
35
			} catch (InterruptedException e) {
36

    
37
			}
38
		}
39
	}
40

    
41
	public boolean isShouldUpdate() {
42
		return shouldUpdate;
43
	}
44

    
45
	public void setShouldUpdate(boolean shouldUpdate) {
46
		this.shouldUpdate = shouldUpdate;
47
	}
48
}
49

    
50
public class PluginAction extends AbstractMDStoreAction {
51

    
52
	private static final Log log = LogFactory.getLog(PluginAction.class);
53

    
54
	@Autowired
55
	private MDStorePluginEnumerator pluginEnumerator;
56

    
57
	@Override
58
	protected void executeAsync(final BlackboardServerHandler handler, final BlackboardJob job) throws MDStoreServiceException {
59

    
60
		final String name = job.getParameters().get("plugin.name");
61

    
62
		final Map<String, MDStorePlugin> plugins = pluginEnumerator.getAll();
63

    
64
		if ((plugins == null) || plugins.isEmpty() || !plugins.containsKey(name)) throw new MDStoreServiceException("Unable to find plugin: " + name);
65

    
66
		log.info("running MDStore plugin: " + name);
67

    
68
		final StatusThread st = new StatusThread(handler, job,20000, plugins.get(name));
69
		st.setShouldUpdate(true);
70
		st.start();
71
		plugins.get(name).run(getDao(), job.getParameters(), params -> {
72
			st.setShouldUpdate(false);
73

    
74
			job.getParameters().putAll(params);
75
			handler.done(job);
76
		});
77
	}
78

    
79
}
(11-11/11)