changeset 224:56aadbbb9156

Add agent running state to AgentInformation reviewed-by: vanaltj review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000763.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 12 Apr 2012 19:27:05 +0200
parents d69e2b144cf0
children ddc853ede286
files agent/src/main/java/com/redhat/thermostat/agent/Agent.java common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/src/main/java/com/redhat/thermostat/common/storage/Storage.java common/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java
diffstat 5 files changed, 52 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Apr 12 19:10:56 2012 +0200
+++ b/agent/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Apr 12 19:27:05 2012 +0200
@@ -61,6 +61,8 @@
     private final BackendRegistry backendRegistry;
     private final AgentStartupConfiguration config;
 
+    private AgentInformation agentInfo;
+    
     private Storage storage;
     private Thread configWatcherThread = null;
 
@@ -101,7 +103,8 @@
     public synchronized void start() throws LaunchException {
         if (configWatcherThread == null) {
             startBackends();
-            storage.addAgentInformation(createAgentInformation());
+            agentInfo = createAgentInformation();
+            storage.addAgentInformation(agentInfo);
             configWatcherThread = new Thread(new ConfigurationWatcher(storage, backendRegistry), "Configuration Watcher");
             configWatcherThread.start();
         } else {
@@ -119,6 +122,7 @@
             backendInfo.setObserveNewJvm(backend.getObserveNewJvm());
             agentInfo.addBackend(backendInfo);
         }
+        agentInfo.setAlive(true);
         return agentInfo;
     }
 
@@ -133,13 +137,19 @@
                 }
             }
             configWatcherThread = null;
-            storage.removeAgentInformation();
+
             stopBackends();
             if (config.purge()) {
                 System.out.println("purging database");
                 logger.info("purging database");
+                storage.removeAgentInformation();
                 storage.purge();
+            } else {
+                agentInfo.setStopTime(System.currentTimeMillis());
+                agentInfo.setAlive(false);
+                storage.updateAgentInformation(agentInfo);
             }
+            
         } else {
             logger.warning("Attempt to stop agent which is not active");
         }
--- a/common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Thu Apr 12 19:10:56 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Thu Apr 12 19:27:05 2012 +0200
@@ -43,6 +43,10 @@
 public class AgentInformation {
 
     private long startTime;
+    private long stopTime;
+
+    private boolean alive;
+    
     private List<BackendInformation> backends = new ArrayList<BackendInformation>();
 
     public long getStartTime() {
@@ -53,10 +57,26 @@
         this.startTime = startTime;
     }
 
+    public void setStopTime(long stopTime) {
+        this.stopTime = stopTime;
+    }
+    
+    public long getStopTime() {
+        return stopTime;
+    }
+    
     public List<BackendInformation> getBackends() {
         return Collections.unmodifiableList(backends);
     }
 
+    public boolean isAlive() {
+        return alive;
+    }
+    
+    public void setAlive(boolean alive) {
+        this.alive = alive;
+    }
+    
     public void addBackend(BackendInformation backend) {
         backends.add(backend);
     }
--- a/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Apr 12 19:10:56 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Apr 12 19:27:05 2012 +0200
@@ -234,11 +234,15 @@
     private DBObject createConfigDBObject(AgentInformation agentInfo) {
         BasicDBObject result = getAgentDBObject();
         result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, agentInfo.getStartTime());
+        result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_STOP_TIME, agentInfo.getStopTime());
+        result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_ALIVE, agentInfo.isAlive());
+        
         BasicDBObject backends = new BasicDBObject();
         for (BackendInformation backend : agentInfo.getBackends()) {
             backends.put(backend.getName(), createBackendConfigDBObject(backend));
         }
         result.put(StorageConstants.KEY_AGENT_CONFIG_BACKENDS, backends);
+        
         return result;
     }
 
@@ -332,6 +336,17 @@
         toInsert.putAll((BSONObject) getAgentDBObject());
         configCollection.insert(toInsert, WriteConcern.SAFE);
     }
+    
+    @Override
+    public void updateAgentInformation(AgentInformation agentInfo) {
+        BasicDBObject queryObject = getAgentDBObject();
+
+        DBObject updated = createConfigDBObject(agentInfo);
+        updated.putAll((BSONObject) queryObject);
+
+        DBCollection configCollection = db.getCollection(StorageConstants.CATEGORY_AGENT_CONFIG);
+        configCollection.update(queryObject, updated);
+    }
 
     @Override
     public void removeAgentInformation() {
--- a/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Apr 12 19:10:56 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Apr 12 19:27:05 2012 +0200
@@ -77,6 +77,8 @@
 
     public abstract void removeAgentInformation();
 
+    public abstract void updateAgentInformation(AgentInformation agentInfo);
+
     /**
      * @return {@code null} if the value is invalid or missing
      */
--- a/common/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java	Thu Apr 12 19:10:56 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java	Thu Apr 12 19:27:05 2012 +0200
@@ -48,4 +48,7 @@
     public static final String KEY_AGENT_CONFIG_BACKEND_ACTIVE = "active";
     public static final String KEY_AGENT_CONFIG_BACKEND_NEW = "new";
     public static final String KEY_AGENT_CONFIG_BACKEND_PIDS = "pids";
+
+    public static final String KEY_AGENT_CONFIG_AGENT_ALIVE = "alive";
+    public static final String KEY_AGENT_CONFIG_AGENT_STOP_TIME = "stop-time";
 }