From 2136a119999e62b0ac74b4fb205fb86b75027412 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Thu, 4 Aug 2011 16:02:42 +0200 Subject: shutdown action implemented --- .../controllerWorker/ControllerWorker/Boot.java | 12 ++- .../ControllerWorker/Shutdown.java | 106 +++++++++++++++++++-- 2 files changed, 106 insertions(+), 12 deletions(-) (limited to 'gearman') diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java index 5259ed1..cef8193 100644 --- a/gearman/controllerWorker/ControllerWorker/Boot.java +++ b/gearman/controllerWorker/ControllerWorker/Boot.java @@ -184,7 +184,6 @@ public class Boot extends Thread { case 7: GearmanJob pingJobWoL = pingWoLJobs.get(clientID); - if (pingJobWoL != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); @@ -204,7 +203,7 @@ public class Boot extends Thread { if (alive.equals("true")) { System.out.println(ipAddress + " is alive after WoL"); - status.put(clientID, 9); // alive + status.put(clientID, 9); // alive, go in successState pingWoLJobs.remove(clientID); } else { System.out.println("ping again " @@ -216,18 +215,21 @@ public class Boot extends Thread { } else { System.out.println(ipAddress + " is not alive after WoL"); - status.put(clientID, 8); // go in errorState + status.put(clientID, 8); // not alive, go in errorState pingWoLJobs.remove(clientID); } } + break; case 8: - System.out.println("WoL Error"); // errorState + System.out.println("WoL failed after 2min"); // errorState + break; case 9: - System.out.println("WoL Success"); // successState + System.out.println("WoL successful"); // successState + break; } diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java index 6cb8645..738ea68 100644 --- a/gearman/controllerWorker/ControllerWorker/Shutdown.java +++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java @@ -29,7 +29,8 @@ public class Shutdown extends Thread { private final long updatePeriod; private HashMap pingJobs; - private HashMap wolJobs; + private HashMap whoJobs; + private HashMap shutdownJobs; private HashMap sshJobs; private HashMap status; private HashMap errors; @@ -42,7 +43,8 @@ public class Shutdown extends Thread { public Shutdown(String serverAddress, int port, Vector> clients, int updateRate) { this.pingJobs = new HashMap(); - this.wolJobs = new HashMap(); + this.whoJobs = new HashMap(); + this.shutdownJobs = new HashMap(); this.sshJobs = new HashMap(); this.status = new HashMap(); this.errors = new HashMap(); @@ -81,7 +83,7 @@ public class Shutdown extends Thread { } } this.finished = true; - System.out.println("Booting finished"); + System.out.println("Shutdown finished"); } private Boolean update() throws IllegalStateException, IOException, @@ -95,6 +97,64 @@ public class Shutdown extends Thread { int clientStatus = status.get(clientID); switch (clientStatus) { + + case 0: + ping(client); + + break; + + case 1: + GearmanJob pingJob = pingJobs.get(clientID); + + if (pingJob != null) { + GearmanJobStatus jobStatus = gearmanClient + .getJobStatus(pingJob); + + if (!jobStatus.isKnown() && pingJob.isDone()) { + GearmanJobResult pingJobRes = pingJob.get(); + String result = ByteUtils.fromUTF8Bytes(pingJobRes + .getResults()); + + if (result != "") { + JSONObject resultObj = (JSONObject) JSONValue + .parse(result); + String alive = resultObj.get("alive").toString(); + + if (alive.equals("true")) { + System.out.println(ipAddress + " alive"); + status.put(clientID, 3); // alive, check Users + pingJobs.remove(clientID); + } else { + System.out.println(ipAddress + " not alive"); + status.put(clientID, 5); // not alive, not needed, go in successState + pingJobs.remove(clientID); + } + } + } + } + + break; + + case 2: + woh(client); + + break; + + case 3: + shutdown(client); + + break; + + case 4: + System.out.println("Shutdown failed after 2min"); // errorState + + break; + + case 5: + System.out.println("Shutdown successful"); // successState + + break; + } if (clientStatus == 20) { allFinished = true; @@ -126,6 +186,34 @@ public class Shutdown extends Thread { System.out.println("ping " + ipAddress); } + + private void woh(HashMap client) { + String ipAddress = client.get("ip"); + int clientID = Integer.parseInt(client.get("id")); + + GearmanJob job = GearmanJobImpl.createJob("who", ipAddress.getBytes(), + "who" + clientID); + gearmanClient.submit(job); + + status.put(clientID, 1); // who started + pingJobs.put(clientID, job); + + System.out.println("who " + ipAddress); + } + + private void shutdown(HashMap client) { + String ipAddress = client.get("ip"); + int clientID = Integer.parseInt(client.get("id")); + + GearmanJob job = GearmanJobImpl.createJob("shutdown", ipAddress.getBytes(), + "shutdown" + clientID); + gearmanClient.submit(job); + + status.put(clientID, 5); // ping started + pingJobs.put(clientID, job); + + System.out.println("shutdown " + ipAddress); + } private void ssh(HashMap client) { String ipAddress = client.get("ip"); @@ -157,13 +245,17 @@ public class Shutdown extends Thread { return pingJobs; } - public HashMap getWolJobs() { - return wolJobs; - } - public HashMap getSshJobs() { return sshJobs; } + + public HashMap getWhoJobs() { + return whoJobs; + } + + public HashMap getShutdownJobs() { + return shutdownJobs; + } public HashMap getStatus() { return status; -- cgit v1.2.3-55-g7522