summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
blob: 4ed7380a406e6aa311ac81a8904d75e2560f9b6a (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
package ControllerWorker;

import java.util.HashMap;
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.JSONValue;

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

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

	@Override
	public GearmanJobResult executeFunction() {
		String data = ByteUtils.fromUTF8Bytes((byte[]) this.data);
		Vector<String> events = new Vector<String>();
		JSONArray jsonArray = (JSONArray) JSONValue.parse(data);

		for (Object eventObj : jsonArray) {
			String event = eventObj.toString();
			events.add(event);
		}

		String[] res = new String[jsonArray.size()];
		int count = 0;

		for (String event : events) {
			Boot boot = BOOTTHREADS.get(event);

			if (boot.isFinished()) {
				res[count] = "Booting of " + event + " finished!";
			} else if (boot.isFinishedWithErrors()) {
				res[count] = "Booting of " + event + " finished with errors!";
			} else {
				res[count] = "Booting of " + event + " not yet finished!";
			}

			count++;
		}

		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;
	}
}