summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
blob: 75bca69504c31fb9b794257ce46c15b264da48f9 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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;

//import org.apache.log4j.Logger;

public class StatusWorker extends AbstractGearmanFunction {
	// private static final Logger logger = ControllerWorkerMain.getLogger();

	private HashMap<String, BootState> BOOTSTATES = new HashMap<String, BootState>();
	private HashMap<String, ShutdownState> SHUTDOWNSTATES = new HashMap<String, ShutdownState>();

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

	@Override
	public GearmanJobResult executeFunction() {
		String data = ByteUtils.fromUTF8Bytes((byte[]) this.data);
		JSONObject jsonObject = (JSONObject) JSONValue.parse(data);
		String type = jsonObject.get("type").toString();
		if (type == "getBootState") {
			return getBootState(jsonObject);
		} else if (type == "getShutdownState") {
			return getShutdownState(jsonObject);
		} else if (type == "updateClientStateBoot") {
			return updateClientStateBoot(jsonObject);
		} else if (type == "updateClientStateShutdown") {
			return updateClientStateShutdown(jsonObject);
		} else if (type == "createBootState") {
			return createBootState(jsonObject);
		} else if (type == "createShutdownState") {
			return createShutdownState(jsonObject);
		} else if (type == "finishBootState") {
			return finishBootState(jsonObject);
		} else if (type == "finishShutdownState") {
			return finishShutdownState(jsonObject);
		} else {
			return null;
		}
	}

	private GearmanJobResult getBootState(JSONObject jsonObject) {
		Vector<String> events = new Vector<String>();
		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>();
		for (String event : events) {
			BootState boot = BOOTSTATES.get(event);
			if (BOOTSTATES.containsKey(event)) {
				Vector<Client> clients = boot.getClients();
				if (boot.isFinished()) {
					content.put("result shortcut", "successful");
					content.put("result text", "Booting of " + event
							+ " has been finished!");
					res.put(event, content);
					BOOTSTATES.remove(event);
					boot = null;
				} else if (boot.isFinishedWithErrors()) {
					content.put("result shortcut", "failed");
					content.put("result text", "Booting of " + event
							+ " has not been finished, due to errors!");
					LinkedHashMap<String, String> clientErrors = new LinkedHashMap<String, String>();
					for (Client client : clients) {
						if (!client.getError().isEmpty()) {
							clientErrors.put(Integer.toString(client.getId()),
									client.getError());
						}
					}
					content.put("clients errors", clientErrors);
					res.put(event, content);
					BOOTSTATES.remove(event);
					boot = null;
				} else {
					content.put("result shortcut", "not finished");
					content.put("result text", "Booting of " + event
							+ " has not yet been finished!");
					LinkedHashMap<String, String> clientStatus = new LinkedHashMap<String, String>();
					for (Client client : clients) {
						clientStatus.put(Integer.toString(client.getId()),
								client.getStateText());
					}
					content.put("clients states", clientStatus);
					res.put(event, content);
				}
			} else {
				content.put("result shortcut", "not founded");
				content.put("result text",
						"The Boot 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;
	}

	private GearmanJobResult getShutdownState(JSONObject jsonObject) {
		Vector<String> events = new Vector<String>();
		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>();
		for (String event : events) {
			if (SHUTDOWNSTATES.containsKey(event)) {
				ShutdownState shutdown = SHUTDOWNSTATES.get(event);
				Vector<Client> clients = shutdown.getClients();
				if (shutdown.isFinished()) {
					content.put("result shortcut", "succeeded");
					content.put("result text", "Shutdown of " + event
							+ " has been finished!");
					res.put(event, content);
					SHUTDOWNSTATES.remove(event);
					shutdown = null;
				} else if (shutdown.isFinishedWithErrors()) {
					content.put("result shortcut", "failed");
					content.put("result text", "Shutdown of " + event
							+ " has not been finished, due to errors!");
					LinkedHashMap<String, String> clientErrors = new LinkedHashMap<String, String>();
					for (Client client : clients) {
						if (!client.getError().isEmpty()) {
							clientErrors.put(Integer.toString(client.getId()),
									client.getError());
						}
					}
					content.put("clients errors", clientErrors);
					res.put(event, content);
					SHUTDOWNSTATES.remove(event);
					shutdown = null;
				} else {
					content.put("result shortcut", "not finished");
					content.put("result text", "Shutdown of " + event
							+ " has not yet been finished!");
					LinkedHashMap<String, String> clientStatus = new LinkedHashMap<String, String>();
					for (Client client : clients) {
						clientStatus.put(Integer.toString(client.getId()),
								client.getStateText());
					}
					content.put("clients states", clientStatus);
					res.put(event, content);
				}
			} else {
				content.put("result shortcut", "not founded");
				content
						.put("result text",
								"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;
	}

	private GearmanJobResult updateClientStateBoot(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		String newState = jsonObject.get("newState").toString();
		String error = jsonObject.get("error").toString();
		int clientID = Integer.parseInt(jsonObject.get("clientID").toString());
		BootState boot = BOOTSTATES.get(event);
		boot.setClientState(clientID, ClientState.valueOf(newState));
		if (!error.isEmpty()) {
			boot.setClientError(clientID, error);
		}
		String jsonResult = "{client state successfully updated}";
		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;
	}

	private GearmanJobResult updateClientStateShutdown(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		String newState = jsonObject.get("newState").toString();
		String error = jsonObject.get("error").toString();
		int clientID = Integer.parseInt(jsonObject.get("clientID").toString());
		ShutdownState shutdown = SHUTDOWNSTATES.get(event);
		shutdown.setClientState(clientID, ClientState.valueOf(newState));
		if (!error.isEmpty()) {
			shutdown.setClientError(clientID, error);
		}
		String jsonResult = "{client state successfully updated}";
		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;
	}

	private GearmanJobResult createBootState(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		String eventOS = jsonObject.get("eventOS").toString();
		JSONArray jsonArray = (JSONArray) jsonObject.get("clients");
		Vector<Client> clients = new Vector<Client>();
		for (Object clientObj : jsonArray) {
			JSONObject clientJsonObj = (JSONObject) clientObj;
			Client client = new Client(clientJsonObj, "boot", event);
			clients.add(client);
		}
		BootState boot = new BootState(event, clients, eventOS);
		BOOTSTATES.put(event, boot);
		String jsonResult = "{boot state successfully created}";
		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;
	}

	private GearmanJobResult createShutdownState(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		JSONArray jsonArray = (JSONArray) jsonObject.get("clients");
		Vector<Client> clients = new Vector<Client>();
		for (Object clientObj : jsonArray) {
			JSONObject clientJsonObj = (JSONObject) clientObj;
			Client client = new Client(clientJsonObj, "shutdown", event);
			clients.add(client);
		}
		ShutdownState shutdown = new ShutdownState(event, clients);
		SHUTDOWNSTATES.put(event, shutdown);
		String jsonResult = "{shutdown state successfully created}";
		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;
	}

	private GearmanJobResult finishBootState(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		Boolean error = Boolean
				.parseBoolean(jsonObject.get("error").toString());
		BootState boot = BOOTSTATES.get(event);
		boot.finish();
		if (error) {
			boot.error();
		}
		String jsonResult = "{boot state successfully finished}";
		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;
	}

	private GearmanJobResult finishShutdownState(JSONObject jsonObject) {
		String event = jsonObject.get("eventName").toString();
		Boolean error = Boolean
				.parseBoolean(jsonObject.get("error").toString());
		ShutdownState shutdown = SHUTDOWNSTATES.get(event);
		shutdown.finish();
		if (error) {
			shutdown.error();
		}
		String jsonResult = "{shutdown state successfully finished}";
		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;
	}
}