From 55f49487fbdd2c83e680ab2c4b1a37cac76fc50f Mon Sep 17 00:00:00 2001
From: Lukas Fritz Marti <lukas.fritz.marti@cern.ch>
Date: Fri, 16 Sep 2011 12:17:25 +0000
Subject: [PATCH] Added on/off switch for ERS-stream. Fixed a bug that crashed
 the script during partition shut-down.

---
 Script/CastorScript.py  | 17 ++++-------------
 Script/Conf.cfg         |  3 +++
 Script/Conf.py          |  3 +++
 Script/CopyThread.py    | 11 +++++------
 Script/DeleteThread.py  | 10 +++++-----
 Script/ManagerThread.py | 10 +++++-----
 6 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/Script/CastorScript.py b/Script/CastorScript.py
index e906082..0fe309a 100755
--- a/Script/CastorScript.py
+++ b/Script/CastorScript.py
@@ -93,22 +93,13 @@ def main(conf):
 
     ##### Setup Environment for ERS #####
     # check if there is a partition to log to
-    import ers
-    from ispy import IPCPartition
-    if IPCPartition(conf.partition).isValid():
-        logInfo = {'file':logger.findCaller()[0],'line':logger.findCaller()[1]}
-        logger.debug("CastorScript found partition '%s'" % conf.partition,extra=logInfo)
-    else:
-        logInfo = {'file':logger.findCaller()[0],'line':logger.findCaller()[1]}
-        logger.error("CastorScript could not find partition '%s', not logging to mrs" 
-                     % conf.partition, extra=logInfo)
+
 
     ##### Setup ERS #####
-    #only if there is a partition, try to add ers backend
-    if IPCPartition(conf.partition).isValid():
+    if conf.ERS_enabled:
         try:
-            #exceptions do not get caught when ers is imported after ispy
-            #import ers
+            import ers
+            from ispy import IPCPartition
             #ers.addLoggingHandler("main")
             ERS_handler=ers.LoggingHandler()
             set_log_level(conf.ERSLogLevel,ERS_handler)
diff --git a/Script/Conf.cfg b/Script/Conf.cfg
index 575e5f6..67c4f35 100755
--- a/Script/Conf.cfg
+++ b/Script/Conf.cfg
@@ -12,6 +12,9 @@ LogLevel: 'debug'
 # ERS severity level (DEBUG,INFO,WARNING,ERROR,CRITICAL)
 ERSLogLevel: 'debug'
 
+# On / Off switch for ERS
+ERS_enabled: True
+
 # Email list which will receive error messages (list of strings)
 mailList: []
 
diff --git a/Script/Conf.py b/Script/Conf.py
index 6a4e9b3..9234b0a 100755
--- a/Script/Conf.py
+++ b/Script/Conf.py
@@ -36,6 +36,9 @@ class Conf:
         # ERS severity level (DEBUG,INFO,WARNING,ERROR,CRITICAL)
         self.ERSLogLevel = cfg.ERSLogLevel
 
+        # On / Off switch for ERS
+        self.ERS_enabled = cfg.ERS_enabled
+
         # Email list which will receive error messages (list of strings)
         self.mailList = cfg.mailList
 
diff --git a/Script/CopyThread.py b/Script/CopyThread.py
index c2a2d3d..42ca275 100755
--- a/Script/CopyThread.py
+++ b/Script/CopyThread.py
@@ -35,6 +35,7 @@ class CopyThread(threading.Thread):
         self.LogDir = self.conf.LogDir
         self.LogLevel = self.conf.LogLevel
         self.ERSLogLevel = self.conf.ERSLogLevel
+        self.ERS_enabled = self.conf.ERS_enabled
         
         self.exitFlag = False
         self.CopyList = []
@@ -50,13 +51,11 @@ class CopyThread(threading.Thread):
         self.logger = logging.getLogger('CopyThread')
         self.logger.addHandler(self.CopyLog)
 
-        ##### Setup ERS #####
-        #only if there is a partition, try to add ers backend
-        import ers
-        from ispy import IPCPartition
-        if IPCPartition(conf.partition).isValid():
+        ##### Add ERS handler #########
+        if self.ERS_enabled:
             try:
-                #import ers
+                import ers
+                from ispy import IPCPartition
                 #ers.addLoggingHandler('CopyThread')
                 self.Copy_ERS_handler=ers.LoggingHandler()
                 set_log_level(self.ERSLogLevel,self.Copy_ERS_handler)
diff --git a/Script/DeleteThread.py b/Script/DeleteThread.py
index c0abe9a..f85c269 100755
--- a/Script/DeleteThread.py
+++ b/Script/DeleteThread.py
@@ -42,6 +42,7 @@ class DeleteThread(threading.Thread):
         self.LogDir = self.conf.LogDir
         self.LogLevel = self.conf.LogLevel
         self.ERSLogLevel = self.conf.ERSLogLevel
+        self.ERS_enabled = self.conf.ERS_enabled
 
         #### Import the function to resolve the merged file names #####
         self.mergedChecker = None
@@ -65,12 +66,11 @@ class DeleteThread(threading.Thread):
         self.logger = logging.getLogger('DeleteThread')
         self.logger.addHandler(self.DeleteLog)
 
-        #only if there is a partition, try to add ers backend
-        import ers
-        from ispy import IPCPartition
-        if IPCPartition(conf.partition).isValid():
+        ##### Add ERS handler #########
+        if self.ERS_enabled:
             try:
-                #import ers
+                import ers
+                from ispy import IPCPartition
                 #ers.addLoggingHandler('DeleteThread')
                 self.Del_ERS_handler=ers.LoggingHandler()
                 set_log_level(self.ERSLogLevel,self.Del_ERS_handler)
diff --git a/Script/ManagerThread.py b/Script/ManagerThread.py
index 6a61c14..04aa15b 100755
--- a/Script/ManagerThread.py
+++ b/Script/ManagerThread.py
@@ -51,6 +51,7 @@ class ManagerThread(threading.Thread):
         self.LogDir = self.conf.LogDir
         self.LogLevel = self.conf.LogLevel
         self.ERSLogLevel = self.conf.ERSLogLevel
+        self.ERS_enabled = self.conf.ERS_enabled
                 
         self.CopyFileList = []
         self.ProblDict = {}
@@ -68,12 +69,11 @@ class ManagerThread(threading.Thread):
         self.logger = logging.getLogger('ManagerThread')
         self.logger.addHandler(self.ManagerLog)
 
-        ##### Add ERS handler in case a partion is found #########
-        import ers
-        from ispy import IPCPartition
-        if IPCPartition(conf.partition).isValid():
+        ##### Add ERS handler #########
+        if self.ERS_enabled:
             try:
-                #import ers
+                import ers
+                from ispy import IPCPartition
                 #ers.addLoggingHandler('ManagerThread')
                 self.manage_ERS_handler=ers.LoggingHandler()
                 set_log_level(self.ERSLogLevel,self.manage_ERS_handler)
-- 
GitLab