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

                         


                         

                                               
                                  
                                                  
                                 
                                  
                                 

                                                           

                                                                                           
 






                                                   

                                                                          


                                                                           








                                                            


                                                                   

                                                                                                    
 


































                                                                                                                       
                                        
                                        
                                 
                                




                                                                               
                                                                                                

                                                            
                                                                                                    



                                                                                   













                                                                                                               


                                        
                         
                 
 
                                                                





                                                                                     

                                                                                       


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

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

		if (type.equals("boot")) {
			for (String event : events) {
				Boot boot = BOOTTHREADS.get(event);
				if (boot != null) {
					Vector<HashMap<String, String>> clients = boot.getClients();

					if (boot.isFinished()) {
						res[count] = "Booting of " + event
								+ " has been finished!";
						boot = null;
					} else if (boot.isFinishedWithErrors()) {
						res[count] = "Booting of " + event
								+ " has not been finished, due to errors!";
						String[] clientErrors = new String[clients.size()];
						int statusCount = 0;
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String errorText = boot.getError(client);
							clientErrors[statusCount] = "Error of the client with the ip "
									+ ipAddress + ": " + errorText;
							statusCount++;
						}
						String jsonClientErrors = JSONValue
								.toJSONString(clientErrors);
						res[count] += jsonClientErrors;
					} else {
						res[count] = "Booting of " + event
								+ " has not yet been finished!";
						String[] clientStatus = new String[clients.size()];
						int statusCount = 0;
						for (HashMap<String, String> client : clients) {
							String ipAddress = client.get("ip");
							String statusText = boot.getStatusText(client);
							clientStatus[statusCount] = "Status of the client with the ip "
									+ ipAddress + ": " + statusText;
							statusCount++;
						}
						String jsonClientStatus = JSONValue
								.toJSONString(clientStatus);
						res[count] += jsonClientStatus;
					}	
				} else {
					
				}
				
				count++;
			}
		} else if (type.equals("shutdown")) {
			for (String event : events) {
				Shutdown shutdown = SHUTDOWNTHREADS.get(event);
				Vector<HashMap<String, String>> clients = shutdown.getClients();

				if (shutdown.isFinished()) {
					res[count] = "Shutdown of " + event + " has been finished!";
				} else if (shutdown.isFinishedWithErrors()) {
					res[count] = "Shutdown of " + event
							+ " finished with errors!";
				} else {
					res[count] = "Shutdown of " + event
							+ " has not yet been finished!";
					String[] clientStatus = new String[clients.size()];
					int statusCount = 0;
					for (HashMap<String, String> client : clients) {
						String ipAddress = client.get("ip");
						String statusText = shutdown.getStatusText(client);
						clientStatus[statusCount] = "Status of the client with the ip "
								+ ipAddress + ": " + statusText;
						statusCount++;
					}
					String jsonClientStatus = JSONValue
							.toJSONString(clientStatus);
					res[count] += jsonClientStatus;
				}

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