From 2ab29f416911ad8c12ee2cb7aac70dc85daebc40 Mon Sep 17 00:00:00 2001
From: Fabrice Le Goff <fabrice.le.goff@cern.ch>
Date: Wed, 11 May 2022 12:12:22 +0200
Subject: [PATCH] make DB stuff in main thread conditionned to DB config

---
 Script/CastorScript.py | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/Script/CastorScript.py b/Script/CastorScript.py
index 1b8d8dd..b42d2e3 100644
--- a/Script/CastorScript.py
+++ b/Script/CastorScript.py
@@ -115,7 +115,6 @@ if config.EmailLogList:
     LogConfig.enable_mail_logging(config)
 
 logger = LogConfig.enable_file_logging("main", "main.log", config)
-dblogger = LogConfig.enable_file_logging("database", "database.log", config)
 
 # Load filename parser
 try:
@@ -166,9 +165,11 @@ signal.signal(signal.SIGINT, main_exit)
 logger.info(thread_id_string())
 
 # Connect to database
-db = None
-db = checkDB(db, logger, dblogger, filename_parser, config)
-db_last_connection_time = time()
+if config.DBURL:
+    dblogger = LogConfig.enable_file_logging("database", "database.log", config)
+    db = None
+    db = checkDB(db, logger, dblogger, filename_parser, config)
+    db_last_connection_time = time()
 
 # Setup Kerberos if needed
 if config.Keytab:
@@ -193,19 +194,20 @@ if config.DdmMonitoringEnabled:
 
 while not exit_event.is_set():
 
-    logger.debug('checking DB connection')
-    db = checkDB(db, logger, dblogger, filename_parser, config)
-
-    # Always keep a fresh connection
-    if db and (time() - db_last_connection_time) > config.DBReconnectTimeout:
-        logger.info('refreshing database connection')
-        with db_lock:
-            try:
-                db.Reconnect()
-            except Exception as ex:
-                logger.error('error connecting to DB: %s', str(ex))
-                db = None
-        db_last_connection_time = time()
+    if config.DBURL:
+        logger.debug('checking DB connection')
+        db = checkDB(db, logger, dblogger, filename_parser, config)
+
+        # Always keep a fresh connection
+        if db and (time() - db_last_connection_time) > config.DBReconnectTimeout:
+            logger.info('refreshing database connection')
+            with db_lock:
+                try:
+                    db.Reconnect()
+                except Exception as ex:
+                    logger.error('error connecting to DB: %s', str(ex))
+                    db = None
+            db_last_connection_time = time()
 
     # Update KRB token, if needed:
     if config.Keytab:
-- 
GitLab