SENSSOFT-73 #implemented exit handler

Project: 
http://git-wip-us.apache.org/repos/asf/incubator-senssoft-userale-pyqt5/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-senssoft-userale-pyqt5/commit/ad22f6e2
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-senssoft-userale-pyqt5/tree/ad22f6e2
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-senssoft-userale-pyqt5/diff/ad22f6e2

Branch: refs/heads/master
Commit: ad22f6e2d6b533b3780b998b7a781541bfa4d21e
Parents: ac477ee
Author: mooshu1x2 <mbe...@draper.com>
Authored: Fri Sep 16 16:55:41 2016 -0400
Committer: mooshu1x2 <mbe...@draper.com>
Committed: Fri Sep 16 16:55:41 2016 -0400

----------------------------------------------------------------------
 userale/ale.py | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-senssoft-userale-pyqt5/blob/ad22f6e2/userale/ale.py
----------------------------------------------------------------------
diff --git a/userale/ale.py b/userale/ale.py
index 6aac603..6ddf474 100644
--- a/userale/ale.py
+++ b/userale/ale.py
@@ -20,7 +20,7 @@ from collections import Counter
 import datetime, time
 import logging
 import uuid
-import json
+import atexit
 
 _ = JsonFormatter
 
@@ -122,7 +122,7 @@ class Ale (QObject):
         # self.timer2 = QTimer ()
         # self.timer2.timeout.connect (self.sample2)
         # self.timer2.start (0)
-
+        #self.destroyed.connect (Ale._on_destroyed)
         # Batch transmission of logs
         self.intervalID = self.startTimer (self.interval)
 
@@ -130,6 +130,9 @@ class Ale (QObject):
         self.logs = []
         self.hlogs = []
 
+        # Register Exit hanldler
+        atexit.register (self.cleanup)
+
     def eventFilter (self, object, event):
         '''
         :param object: [QObject] The object being watched.
@@ -166,6 +169,13 @@ class Ale (QObject):
 
         return super (Ale, self).eventFilter (object, event)
 
+    def cleanup (self):
+        '''
+        Clean up any dangling logs in self.logs or self.hlogs
+        '''
+        self.aggregate ()
+        self.dump ()
+
     def timerEvent (self, event):
         '''
         :param object: [list] List of events
@@ -173,8 +183,11 @@ class Ale (QObject):
 
         Routinely dump data to file or send over the network
         '''
+        self.dump ()
+
+    def dump (self):
         if len(self.logs) > 0:
-            #print ("dumping {} logs".format (len (self.logs)))
+            # print ("dumping {} logs".format (len (self.logs)))
             self.logger.info (_(self.logs))
             self.logs = [] # Reset logs
 
@@ -184,7 +197,7 @@ class Ale (QObject):
         to be emitted later
         '''
         if len (self.hlogs) > 0:
-            #print ("agging {} logs".format (len (self.hlogs)))
+            # print ("agging {} logs".format (len (self.hlogs)))
             agg_events = Counter (self.hlogs)
             # Iterate over collapsed collection to generate a single log per 
event
             # Location information is lost due to consolidation. 

Reply via email to