summaryrefslogblamecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
blob: bb69b3479e7faf1d47d4cf5b4db55d9496dccd58 (plain) (tree)
1
2
3
4
5
6
7
8
9

                         
                         
                               

                        

                                               
                                  
                                                  
                                 
                                  
                                 
 

                                 
                                                           
                                                                                 
 

                                                                                                                  
 






                                                   
                                                                          
                                                                           




















                                                                              






                                                                      
                                                                           



                                                           

                                                                                                                                      




                                                                           
                                                                                    













                                                                                                                         
                                                 
                                         



                                                                                    
                                        








                                                                                                                         
                                                                
                                 




                                                                                                      
                         








































                                                                                                                                      
                                                 
                                         



                                                                                    
                                        








                                                                                                                         
                                                                
                                 





                                                                                                                          
                         
                 
                                                                




                                                                                     

                                                                                       

                           













































                                                                                       
                                                                                    






                                                                                 
                                                                               













                                                                                       
                                                                                    






                                                                                     
                                                                                  


















































                                                                                       
 
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 static final HashMap<String, BootState> BOOTSTATES = new HashMap<String, BootState>();
	private static final 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);
		if (jsonObject != null) {
			String type = jsonObject.get("type").toString();
			if (type.equals("getBootState")) {
				return getBootState(jsonObject);
			} else if (type.equals("getShutdownState")) {
				return getShutdownState(jsonObject);
			} else if (type.equals("updateClientStateBoot")) {
				return updateClientStateBoot(jsonObject);
			} else if (type.equals("updateClientStateShutdown")) {
				return updateClientStateShutdown(jsonObject);
			} else if (type.equals("createBootState")) {
				return createBootState(jsonObject);
			} else if (type.equals("createShutdownState")) {
				return createShutdownState(jsonObject);
			} else if (type.equals("finishBootState")) {
				return finishBootState(jsonObject);
			} else if (type.equals("finishShutdownState")) {
				return finishShutdownState(jsonObject);
			} else {
				return null;
			}
		} 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", "succeeded");
					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();
		Boolean force = Boolean.valueOf(jsonObject.get("force").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, force, 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();
		Boolean force = Boolean.valueOf(jsonObject.get("force").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, force, 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;
	}
}