summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
blob: e1450fe14d2375ebfa6376f0fbf82e8a8504f9a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package ControllerWorker;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Vector;

import org.gearman.client.GearmanJobResult;
import org.gearman.client.GearmanJobResultImpl;
import org.gearman.util.ByteUtils;
import org.gearman.worker.AbstractGearmanFunction;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class StatusWorker extends AbstractGearmanFunction {
	static HashMap<String, Boot> BOOTTHREADS = new HashMap<String, Boot>();
	static HashMap<String, Shutdown> SHUTDOWNTHREADS = new HashMap<String, Shutdown>();

	@Override
	public String getName() {
		return "status";
	}

	@Override
	public GearmanJobResult executeFunction() {
		String data = ByteUtils.fromUTF8Bytes((byte[]) this.data);
		Vector<String> events = new Vector<String>();
		JSONObject jsonObject = (JSONObject) JSONValue.parse(data);
		String type = jsonObject.get("type").toString();
		JSONArray jsonArray = (JSONArray) jsonObject.get("events");
		for (Object eventObj : jsonArray) {
			String event = eventObj.toString();
			events.add(event);
		}
		LinkedHashMap<String, LinkedHashMap<String, Object>> res = new LinkedHashMap<String, LinkedHashMap<String, Object>>();
		LinkedHashMap<String, Object> content = new LinkedHashMap<String, Object>();
		if (type.equals("boot")) {
			for (String event : events) {
				Boot boot = BOOTTHREADS.get(event);
				if (BOOTTHREADS.containsKey(event)) {
					Vector<HashMap<String, String>> clients = boot.getClients();
					if (boot.isFinished()) {
						content.put("result", "Booting of " + event
								+ " has been finished!");
						boot = null;
						res.put(event, content);
					} else if (boot.isFinishedWithErrors()) {
						content.put("result", "Booting of " + event
								+ " has not been finished, due to errors!");
						LinkedHashMap<String, String> clientErrors = new LinkedHashMap<String, String>();
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String errorText = boot.getError(client);
							clientErrors.put("Client " + ipAddress, errorText);
						}
						content.put("errors", clientErrors);
						res.put(event, content);
						boot = null;
					} else {
						content.put("result", "Booting of " + event
								+ " has not yet been finished!");
						LinkedHashMap<String, String> clientStatus = new LinkedHashMap<String, String>();
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String statusText = boot.getStatusText(client);
							clientStatus.put("Client " + ipAddress, statusText);
						}
						content.put("status", clientStatus);
						res.put(event, content);
					}
				} else {
					content
							.put("result",
									"The Boot process for the event has not been found.");
					res.put(event, content);
				}
			}
		} else if (type.equals("shutdown")) {
			for (String event : events) {
				if (SHUTDOWNTHREADS.containsKey(event)) {
					Shutdown shutdown = SHUTDOWNTHREADS.get(event);
					Vector<HashMap<String, String>> clients = shutdown
							.getClients();
					if (shutdown.isFinished()) {
						content.put("result", "Shutdown of " + event
								+ " has been finished!");
						res.put(event, content);
						shutdown = null;
					} else if (shutdown.isFinishedWithErrors()) {
						content.put("result", "Shutdown of " + event
								+ " has not been finished, due to errors!");
						LinkedHashMap<String, String> clientErrors = new LinkedHashMap<String, String>();
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String errorText = shutdown.getError(client);
							clientErrors.put("Client " + ipAddress, errorText);
						}
						content.put("errors", clientErrors);
						res.put(event, content);
						shutdown = null;
					} else {
						content.put("result", "Shutdown of " + event
								+ " has not yet been finished!");
						LinkedHashMap<String, String> clientStatus = new LinkedHashMap<String, String>();
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String statusText = shutdown.getStatusText(client);
							clientStatus.put("Client " + ipAddress, statusText);
						}
						content.put("status", clientStatus);
						res.put(event, content);
					}
				} else {
					content
							.put("result",
									"The Shutdown process for the event has not been found.");
					res.put(event, content);
				}
			}
		}
		String jsonResult = JSONValue.toJSONString(res);
		byte[] warnings = new byte[0];
		byte[] exceptions = new byte[0];
		int numerator = 0;
		int denominator = 0;
		GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, true,
				jsonResult.getBytes(), warnings, exceptions, numerator,
				denominator);
		return gjr;
	}
}