summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
diff options
context:
space:
mode:
Diffstat (limited to 'gearman/controllerWorker/ControllerWorker/StatusWorker.java')
-rwxr-xr-xgearman/controllerWorker/ControllerWorker/StatusWorker.java335
1 files changed, 250 insertions, 85 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/StatusWorker.java b/gearman/controllerWorker/ControllerWorker/StatusWorker.java
index 1743a2d..75bca69 100755
--- a/gearman/controllerWorker/ControllerWorker/StatusWorker.java
+++ b/gearman/controllerWorker/ControllerWorker/StatusWorker.java
@@ -17,8 +17,8 @@ import org.json.simple.JSONValue;
public class StatusWorker extends AbstractGearmanFunction {
// private static final Logger logger = ControllerWorkerMain.getLogger();
- static HashMap<String, Boot> BOOTTHREADS = new HashMap<String, Boot>();
- static HashMap<String, Shutdown> SHUTDOWNTHREADS = new HashMap<String, Shutdown>();
+ private HashMap<String, BootState> BOOTSTATES = new HashMap<String, BootState>();
+ private HashMap<String, ShutdownState> SHUTDOWNSTATES = new HashMap<String, ShutdownState>();
@Override
public String getName() {
@@ -28,9 +28,31 @@ public class StatusWorker extends AbstractGearmanFunction {
@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();
+ 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();
@@ -38,99 +60,115 @@ public class StatusWorker extends AbstractGearmanFunction {
}
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<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);
- BOOTTHREADS.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);
- BOOTTHREADS.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());
+ 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 states", clientStatus);
- res.put(event, content);
}
+ content.put("clients errors", clientErrors);
+ res.put(event, content);
+ BOOTSTATES.remove(event);
+ boot = null;
} else {
- content.put("result shortcut", "not founded");
- content
- .put("result text",
- "The Boot process for the event has not been found.");
+ 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);
}
- } else if (type.equals("shutdown")) {
- for (String event : events) {
- if (SHUTDOWNTHREADS.containsKey(event)) {
- Shutdown shutdown = SHUTDOWNTHREADS.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);
- SHUTDOWNTHREADS.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);
- SHUTDOWNTHREADS.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());
+ }
+ 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 states", clientStatus);
- res.put(event, content);
}
+ content.put("clients errors", clientErrors);
+ res.put(event, content);
+ SHUTDOWNSTATES.remove(event);
+ shutdown = null;
} else {
- content.put("result shortcut", "not founded");
- content
- .put("result text",
- "The Shutdown process for the event has not been found.");
+ 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);
@@ -143,4 +181,131 @@ public class StatusWorker extends AbstractGearmanFunction {
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;
+ }
} \ No newline at end of file