Project

General

Profile

1
package eu.dnetlib.msro.workflows.graph;
2

    
3
import java.util.*;
4

    
5
import com.google.common.collect.BiMap;
6
import com.google.common.collect.HashBiMap;
7
import eu.dnetlib.miscutils.datetime.DateUtils;
8
import eu.dnetlib.msro.rmi.MSROException;
9
import eu.dnetlib.msro.workflows.util.ProcessUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Required;
13

    
14
/**
15
 * Created by michele on 20/11/15.
16
 */
17
public class ProcessRegistry {
18

    
19
	private static final Log log = LogFactory.getLog(ProcessRegistry.class);
20
	private BiMap<String, Process> procs = HashBiMap.create();
21
	private Map<String, Collection<Process>> byOtherId = new HashMap<String, Collection<Process>>();
22

    
23
	private int maxSize;
24

    
25
	synchronized public int countRunningWfs() {
26
		int count = 0;
27
		for (Map.Entry<String, Process> e : procs.entrySet()) {
28
			final Process proc = e.getValue();
29
			if (!proc.isTerminated()) {
30
				count++;
31
			}
32
		}
33
		return count;
34
	}
35

    
36
	public Process findProcess(final String procId) {
37
		return procs.get(procId);
38
	}
39

    
40
	public Set<Process> listProcesses() {
41
		return procs.values();
42
	}
43

    
44
	public Collection<Process> findProcsByOtherId(final String id) {
45
		synchronized (this) {
46
			final Collection<Process> res = byOtherId.get(id);
47
			return res != null ? res : new ArrayList<Process>();
48
		}
49
	}
50

    
51
	public void registerProcess(final Process process, final String... ids) throws MSROException {
52
		if (procs.containsValue(process) || procs.containsKey(process.getId())) {
53
			log.error("Already registerd process: " + process);
54
			throw new MSROException("Already registerd process: " + process);
55
		}
56

    
57
		if (procs.size() >= maxSize) {
58
			removeOldestProcess();
59
		}
60

    
61
		procs.put(process.getId(), process);
62
		for (String id : ids) {
63
			synchronized (this) {
64
				if (!byOtherId.containsKey(id)) {
65
					byOtherId.put(id, new ArrayList<Process>());
66
				}
67
				byOtherId.get(id).add(process);
68
			}
69
		}
70

    
71
		log.info("Registered process " + process);
72
	}
73

    
74
	private void removeOldestProcess() {
75
		long oldDate = DateUtils.now();
76
		String oldId = null;
77

    
78
		for (Map.Entry<String, Process> e : procs.entrySet()) {
79
			final Process proc = e.getValue();
80

    
81
			if (proc.isTerminated()) {
82
				final long date = ProcessUtils.calculateLastActivityDate(proc);
83
				if (date < oldDate) {
84
					oldDate = date;
85
					oldId = e.getKey();
86
				}
87
			}
88
		}
89

    
90
		if (oldId != null) {
91
			unregisterProcess(oldId);
92
		}
93

    
94
	}
95

    
96
	public void unregisterProcess(final String procId) {
97
		synchronized (this) {
98
			final Process process = procs.remove(procId);
99
			if (process != null) {
100
				for (final Collection<Process> processes : byOtherId.values()) {
101
					processes.remove(process);
102
				}
103
			}
104
		}
105
	}
106

    
107
	public int getMaxSize() {
108
		return maxSize;
109
	}
110

    
111
	@Required
112
	public void setMaxSize(final int maxSize) {
113
		this.maxSize = maxSize;
114
	}
115

    
116
}
(10-10/12)