summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker
diff options
context:
space:
mode:
authorBjörn Geiger2011-08-10 15:33:36 +0200
committerBjörn Geiger2011-08-10 15:33:36 +0200
commit59f56e36032a258f155c3eeb890f4b67025e9079 (patch)
tree7dbd8c467320bbad4882e827cc8708ee7f2bccdb /gearman/controllerWorker/ControllerWorker
parentverschiedene Korrekturen (diff)
downloadpoolctrl-59f56e36032a258f155c3eeb890f4b67025e9079.tar.gz
poolctrl-59f56e36032a258f155c3eeb890f4b67025e9079.tar.xz
poolctrl-59f56e36032a258f155c3eeb890f4b67025e9079.zip
ShutdownWorker erweitert
Diffstat (limited to 'gearman/controllerWorker/ControllerWorker')
-rw-r--r--gearman/controllerWorker/ControllerWorker/Shutdown.java191
1 files changed, 176 insertions, 15 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java
index f79ca84..470201d 100644
--- a/gearman/controllerWorker/ControllerWorker/Shutdown.java
+++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java
@@ -5,6 +5,7 @@ import java.lang.Thread;
import java.util.concurrent.ExecutionException;
import java.util.Date;
import java.util.HashMap;
+import java.util.StringTokenizer;
import java.util.Vector;
import org.gearman.client.GearmanClient;
@@ -34,6 +35,7 @@ public class Shutdown extends Thread {
private HashMap<Integer, GearmanJob> whoJobs;
private HashMap<Integer, GearmanJob> shutdownJobs;
private HashMap<Integer, GearmanJob> pingShutdownJobs;
+ private HashMap<Integer, GearmanJob> checkStatusJobs;
private HashMap<Integer, Integer> status;
private HashMap<Integer, String> errors;
private HashMap<Integer, Long> pingShutdownTime;
@@ -48,6 +50,8 @@ public class Shutdown extends Thread {
this.pingJobs = new HashMap<Integer, GearmanJob>();
this.whoJobs = new HashMap<Integer, GearmanJob>();
this.shutdownJobs = new HashMap<Integer, GearmanJob>();
+ this.pingShutdownJobs = new HashMap<Integer, GearmanJob>();
+ this.checkStatusJobs = new HashMap<Integer, GearmanJob>();
this.status = new HashMap<Integer, Integer>();
this.errors = new HashMap<Integer, String>();
this.updateRate = updateRate; // updates per second
@@ -96,7 +100,6 @@ public class Shutdown extends Thread {
for (HashMap<String, String> client : clients) {
String ipAddress = client.get("ip");
- String macAddress = client.get("mac");
int clientID = Integer.parseInt(client.get("id"));
int clientStatus = status.get(clientID);
@@ -126,12 +129,12 @@ public class Shutdown extends Thread {
if (alive.equals("true")) {
System.out.println(ipAddress + " alive");
- status.put(clientID, 3); // alive, check Users
+ status.put(clientID, 2); // alive, check Users
pingJobs.remove(clientID);
} else {
System.out.println(ipAddress + " not alive");
- status.put(clientID, 7); // not alive, go in
- // successState
+ // not alive, go in successState
+ status.put(clientID, 13);
pingJobs.remove(clientID);
}
}
@@ -146,16 +149,106 @@ public class Shutdown extends Thread {
break;
case 3:
- shutdown(client);
+ GearmanJob whoJob = whoJobs.get(clientID);
+
+ if (whoJob != null) {
+ GearmanJobStatus jobStatus = gearmanClient
+ .getJobStatus(whoJob);
+
+ if (!jobStatus.isKnown() && whoJob.isDone()) {
+ GearmanJobResult whoJobRes = whoJob.get();
+ String result = ByteUtils.fromUTF8Bytes(whoJobRes
+ .getResults());
+
+ if (result != "") {
+ JSONObject resultObj = (JSONObject) JSONValue
+ .parse(result);
+ String rawoutput = resultObj.get("rawoutput")
+ .toString();
+ StringTokenizer str = new StringTokenizer(
+ rawoutput, " ");
+ String user = "";
+ if (str.hasMoreTokens()) {
+ user = str.nextToken();
+ }
+
+ if (user.isEmpty()) {
+ System.out.println(ipAddress + " right User");
+ status.put(clientID, 4); // right user
+ whoJobs.remove(clientID);
+ } else {
+ System.out.println(ipAddress + " wrong User");
+ status.put(clientID, 8); // wrong user
+ whoJobs.remove(clientID);
+ }
+ } else {
+ System.out.println(ipAddress
+ + " Cannot check if a user is logged in.");
+ this.errors
+ .put(clientID,
+ "The check if a user is logged in has been failed.");
+ /*
+ * cannot check if a user is logged in, go in
+ * errorState
+ */
+ status.put(clientID, 12);
+ whoJobs.remove(clientID);
+ }
+ }
+ }
break;
case 4:
- pingShutdown(client);
+ shutdown(client);
break;
case 5:
+ GearmanJob shutdownJob = shutdownJobs.get(clientID);
+ if (shutdownJob != null) {
+ GearmanJobStatus jobStatus = gearmanClient
+ .getJobStatus(shutdownJob);
+
+ if (!jobStatus.isKnown() && shutdownJob.isDone()) {
+ GearmanJobResult wolJobRes = shutdownJob.get();
+ String result = ByteUtils.fromUTF8Bytes(wolJobRes
+ .getResults());
+ System.out.println(result.toString());
+ if (result.equals("")) {
+ System.out.println(ipAddress
+ + " Shutdown command send");
+ status.put(clientID, 6); // shutdown command send
+ shutdownJobs.remove(clientID);
+ } else {
+ System.out.println(ipAddress
+ + " Cannot send shutdown command");
+ this.errors
+ .put(clientID,
+ "Sending the shutdown command has been failed.");
+ // cannot send shutdown command, go in errorState
+ status.put(clientID, 12);
+ shutdownJobs.remove(clientID);
+ }
+ }
+ }
+
+ break;
+
+ case 6:
+ Date date = new Date();
+ Long timestamp = date.getTime();
+ pingShutdownTime.put(clientID, timestamp);
+ status.put(clientID, 7); // ping after shutdown
+
+ break;
+
+ case 7:
+ pingShutdown(client);
+
+ break;
+
+ case 8:
GearmanJob pingJobShutdown = pingShutdownJobs.get(clientID);
if (pingJobShutdown != null) {
Date currentDate = new Date();
@@ -180,41 +273,95 @@ public class Shutdown extends Thread {
.println(ipAddress
+ " is alive after shutdown. Ping again...");
// alive, ping again
- status.put(clientID, 4);
+ status.put(clientID, 7);
pingShutdownJobs.remove(clientID);
} else {
System.out
.println("not alive after shutdown"
+ ipAddress);
// go in successState
- status.put(clientID, 7);
+ status.put(clientID, 13);
}
}
}
} else {
System.out.println(ipAddress
+ " is alive after shutdown");
- status.put(clientID, 6); // not alive, go in errorState
+ this.errors.put(clientID,
+ "Client is still alive after shutdown.");
+ // still alive, go in errorState
+ status.put(clientID, 12);
pingShutdownJobs.remove(clientID);
}
}
break;
- case 6:
+ case 9:
+ checkStatus(client);
+
+ break;
+
+ case 10:
+ GearmanJob checkStatusJob = checkStatusJobs.get(clientID);
+
+ if (checkStatusJob != null) {
+ GearmanJobStatus jobStatus = gearmanClient
+ .getJobStatus(checkStatusJob);
+
+ if (!jobStatus.isKnown() && checkStatusJob.isDone()) {
+ GearmanJobResult whoJobRes = checkStatusJob.get();
+ String result = ByteUtils.fromUTF8Bytes(whoJobRes
+ .getResults());
+
+ if (result != "") {
+ JSONObject resultObj = (JSONObject) JSONValue
+ .parse(result);
+ String rawoutput = resultObj.get("rawoutput")
+ .toString();
+ if (rawoutput.equals("is working")) {
+ System.out.println(ipAddress + " is working");
+ status.put(clientID, 11); // is working
+ checkStatusJobs.remove(clientID);
+ } else {
+ System.out.println(ipAddress
+ + " is not working");
+ status.put(clientID, 4); // is not working
+ checkStatusJobs.remove(clientID);
+ }
+ } else {
+ System.out.println(ipAddress
+ + " Cannot check if user is working.");
+ this.errors
+ .put(clientID,
+ "The check if a user is working has been failed.");
+ // cannot check if user is working, go in errorState
+ status.put(clientID, 12);
+ checkStatusJobs.remove(clientID);
+ }
+ }
+ }
+
+ break;
+
+ case 11:
+
+ break;
+
+ case 12:
System.out.println("Shutdown failed"); // errorState
break;
- case 7:
+ case 13:
System.out.println("Shutdown successful"); // successState
break;
}
- if (clientStatus == 6) {
+ if (clientStatus == 12) {
allFinished = true;
- } else if (clientStatus == 7) {
+ } else if (clientStatus == 13) {
allFinished = true;
this.error = true;
} else {
@@ -251,7 +398,7 @@ public class Shutdown extends Thread {
"who" + clientID);
gearmanClient.submit(job);
- status.put(clientID, 1); // who started
+ status.put(clientID, 3); // who started
whoJobs.put(clientID, job);
System.out.println("who " + ipAddress);
@@ -279,12 +426,26 @@ public class Shutdown extends Thread {
"ping" + clientID);
gearmanClient.submit(job);
- status.put(clientID, 7); // pingShutdown started
+ status.put(clientID, 8); // pingShutdown started
pingShutdownJobs.put(clientID, job);
System.out.println("ping " + ipAddress);
}
+ private void checkStatus(HashMap<String, String> client) {
+ String ipAddress = client.get("ip");
+ int clientID = Integer.parseInt(client.get("id"));
+
+ GearmanJob job = GearmanJobImpl.createJob("checkStatus", ipAddress
+ .getBytes(), "checkStatus" + clientID);
+ gearmanClient.submit(job);
+
+ status.put(clientID, 9); // checkStatus started
+ checkStatusJobs.put(clientID, job);
+
+ System.out.println("checkStatus " + ipAddress);
+ }
+
public String getStatusText(HashMap<String, String> client) {
int clientID = Integer.parseInt(client.get("id"));
int clientStatus = this.status.get(clientID);