summaryrefslogblamecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/StatusWorker.java
blob: 16981292017a41b53becda37122b183e865fff7a (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;

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);
		}
		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", "succeeded");
						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());
						}
						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());
						}
						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;
	}
}