From bb3aa0bf45efbaf98bdf611e26dbdd99a58fd78d Mon Sep 17 00:00:00 2001
From: Jonas <jonas@holm.tech>
Date: Wed, 17 Apr 2019 18:46:35 +0200
Subject: [PATCH] IS working, AFS config now tbed config, partition guide
 improved

---
 ....cfg => tbed_config_IS_ERS_eosstorage.cfg} | 26 +++++++-
 Configs/template.cfg                          |  1 +
 Script/UnitTests/InfoServiceThread_Test.py    | 10 ++-
 Script/cs/Threads/InfoServiceThread.py        | 63 +++++++++++++++----
 TestPartition/readme                          |  1 +
 5 files changed, 88 insertions(+), 13 deletions(-)
 rename Configs/{test_eosstorage.cfg => tbed_config_IS_ERS_eosstorage.cfg} (89%)

diff --git a/Configs/test_eosstorage.cfg b/Configs/tbed_config_IS_ERS_eosstorage.cfg
similarity index 89%
rename from Configs/test_eosstorage.cfg
rename to Configs/tbed_config_IS_ERS_eosstorage.cfg
index 2790b4e..aaa6a38 100644
--- a/Configs/test_eosstorage.cfg
+++ b/Configs/tbed_config_IS_ERS_eosstorage.cfg
@@ -1,3 +1,12 @@
+###### TBED TEST CONFIG ######
+
+# This config file is used for testing on testbed.
+# It is configured to use ERS and IS, as well as EOS for file transfer
+# It is meant to be tested on a custom partition
+# see how to run the custom partition in ../TestPartition/readme
+# 
+# Make sure that the folder references are present in "LogDir, CopyDir, Dirlist"
+
 ########## Common parameters ##########
 
 # Log directory
@@ -34,7 +43,7 @@ pythonPathPrepend: []
 ########## VARIABLES FOR ERS ##############
 
 # Partition to log to
-partition: "initial"
+partition: "part_hlt_jholm"
 
 # Stream ERS messages to
 ers_info: "mts"
@@ -42,6 +51,21 @@ ers_warning: "mts"
 ers_error: "mts"
 ers_debug: "mts"
 
+########## VARIABLES FOR IS  ##########
+
+# On / Off switch for IS
+IS_enabled: True
+ 
+# Partition to publish to
+IS_partition: "part_hlt_jholm"
+ 
+# IS Server to publish to
+# Also called IS repository
+IS_server: "RunParams"
+
+# Publication Period (time between updates)
+IS_publication_period: 5
+
 ########## METADATA DATABASE ##########
 
 # Oracle connection string
diff --git a/Configs/template.cfg b/Configs/template.cfg
index d6e970e..91282bd 100644
--- a/Configs/template.cfg
+++ b/Configs/template.cfg
@@ -59,6 +59,7 @@ IS_enabled: False
 IS_partition: "initial"
 
 # IS Server to publish to
+# Also called IS repository
 IS_server: "RunParams"
 
 # Publication Period (time between updates)
diff --git a/Script/UnitTests/InfoServiceThread_Test.py b/Script/UnitTests/InfoServiceThread_Test.py
index f7062ae..41149d6 100644
--- a/Script/UnitTests/InfoServiceThread_Test.py
+++ b/Script/UnitTests/InfoServiceThread_Test.py
@@ -18,12 +18,20 @@ class TestInfoServiceThread(unittest.TestCase):
     def setUp(self):
         ## we mock the event so that the thread won't be left hanging
         self.mock_event = mock.create_autospec(threading.Event())
+
         kwargs = {"wait.return_value": True}
         self.mock_event.configure_mock(**kwargs)
 
         ## we mock the configuration
         self.mock_cfg = mock.create_autospec(ConfigHolder)
-        self.mock_cfg.configure_mock(partition="initial", LogDir="", LogLevel="", FileName="Conf_test.cfg")
+        #self.mock_cfg.configure_mock(partition="initial", LogDir="", LogLevel="", FileName="Conf_test.cfg")
+        self.mock_cfg.configure_mock(partition="initial", 
+        LogDir="", 
+        LogLevel="", 
+        FileName="Conf_test.cfg", 
+        IS_partition="initial", 
+        IS_server="RunParams", 
+        IS_publication_period="5")
 
         ## we're not interested in any logging for this test
         with mock.patch("cs.Threads.InfoServiceThread.enable_file_logging"):
diff --git a/Script/cs/Threads/InfoServiceThread.py b/Script/cs/Threads/InfoServiceThread.py
index fb9df28..6ec4c55 100644
--- a/Script/cs/Threads/InfoServiceThread.py
+++ b/Script/cs/Threads/InfoServiceThread.py
@@ -13,18 +13,23 @@ from cs.Tools.LogConfig import enable_file_logging, make_tdaq_app_name
 class InfoServiceThread(threading.Thread):
 
     def __init__(self,conf,event):
-        # get partition and IS dictionary
-        self.ipc_partition = IPCPartition(conf.partition)
-        self.is_info_dictionary = ispy.ISInfoDictionary(self.ipc_partition) #pylint: disable=no-member
+        # get partition to publish to
+        self.ipc_partition = IPCPartition(conf.IS_partition)
+
+        # IS Server to publish to
+        # Errors comes up with: IS repository, not server
+        self.IS_server = conf.IS_server
+
+        # Publication Period (seconds between updates)
+        self.IS_publication_period = conf.IS_publication_period
+
         # state fields
         self.tdaq_app_name = make_tdaq_app_name(conf)
         self.uptime_seconds = self.get_process_uptime_in_seconds()
 
         threading.Thread.__init__(self, name="InfoServiceThread")
 
-        self.conf = conf
         self.event = event
-
         self.exit_flag = 0
 
         # get logger to files, (Will also log to ERS)
@@ -45,10 +50,16 @@ class InfoServiceThread(threading.Thread):
             if(self.ipc_partition.isValid()):
                 self.logger.debug("Provided Partition %s is Valid", self.ipc_partition.name())
                 self.send_update()
+
+                ## For manual testing.
+                #self.send_wrong_info_update()
+                #self.cleanup_unwanted_stuff("fake", "Double")
+                #self.cleanup_unwanted_stuff("CastorScript29", "CastorScriptState")
+
             else:
                 self.logger.warning("Provided Partition not Valid, IS Service disabled")
             #wait for next update
-            self.event.wait(5)
+            self.event.wait(self.IS_publication_period)
         # end while
         self.logger.info('InfoServiceThread exited')
     # end def run()
@@ -64,7 +75,7 @@ class InfoServiceThread(threading.Thread):
     def send_update(self):
         try:
             is_data_type = "CastorScriptState"
-            is_data_identifier = "RunParams.{}.State".format(self.tdaq_app_name)
+            is_data_identifier = "{}.{}.State".format(self.IS_server,self.tdaq_app_name)
 
             self.logger.debug("Retrieving: %s, type: %s, from IS", is_data_identifier,is_data_type)
 
@@ -72,15 +83,45 @@ class InfoServiceThread(threading.Thread):
 
             if is_data.exists():
                 self.logger.debug("found: %s", is_data)
-                is_data.checkout()
             else:
                 self.logger.debug("%s does not exist", is_data.name())
 
             is_data.uptime_seconds = self.uptime_seconds
-            #is_data.value = self.uptime_seconds
+            is_data.checkin()
+            #self.logger.debug("Update sent: %s", str(is_data))
+        except Exception as ex:
+            self.logger.error("Error occured in send_update(). Error was: %s", str(ex))
+
+
+    ##### TESTING FUNCTIONS #####
+    def send_wrong_info_update(self, name_of_unwanted_IS_entry, type_of_unwanted_IS_entry):
+        try:
+            is_data_type = type_of_unwanted_IS_entry
+            is_data_identifier = "{}.{}.State".format(self.IS_server,name_of_unwanted_IS_entry)
+
+            self.logger.debug("Retrieving: %s, type: %s, from IS", is_data_identifier,is_data_type)
+
+            is_data = ispy.ISObject(self.ipc_partition, is_data_identifier, is_data_type)
+
+            if is_data.exists():
+                self.logger.debug("found: %s", is_data)
+            else:
+                self.logger.debug("%s does not exist", is_data.name())
 
+            #is_data.uptime_seconds = self.uptime_seconds
+            #is_data.value = self.uptime_seconds
+            #send data entry
             is_data.checkin()
 
-            self.logger.debug("Update sent: %s", str(is_data))
         except Exception as ex:
-            self.logger.error("Error occured in send_update(). Error was: %s", str(ex))
+            self.logger.error("Error occured in send_wrong_info_update(). Error was: %s", str(ex))
+
+    def cleanup_unwanted_stuff(self, name_of_unwanted_IS_entry, type_of_unwanted_IS_entry):
+        '''For deleting old castorscript information in case of change of format'''
+        it = ispy.ISInfoIterator(self.ipc_partition, self.IS_server, ispy.ISCriteria('.*'))
+        is_info_dictionary = ispy.ISInfoDictionary(self.ipc_partition) 
+        self.logger.debug("Removing old objects of with old type %s, with name %s in the %s server:",type_of_unwanted_IS_entry,name_of_unwanted_IS_entry, self.IS_server)
+        while it.next():
+            if str(it.name()).find(name_of_unwanted_IS_entry) is not -1 and str(it.type()).find(type_of_unwanted_IS_entry) is not -1:
+                is_info_dictionary.remove(it.name())
+                #it.name(), it.type().name(), it.time(), it.tag()
\ No newline at end of file
diff --git a/TestPartition/readme b/TestPartition/readme
index 4152e14..235ee34 100644
--- a/TestPartition/readme
+++ b/TestPartition/readme
@@ -16,6 +16,7 @@ HOW TO RUN:
 This repository is named part_hlt_jholm, the data file is cs-part_hlt_jholm.data.xml
 Before running:
 1. copy this directory to you afs-home public folder (lowercase)
+   cp -r CastorScript/TestPartition/. public/
 2. IN THE FILE: cs-part_hlt_jholm.data.xml line 7508
   a. vim cs-part_hlt_jholm.data.xml +7508
   b. change <data val="/afs/.../j/jholm/public/.../CastorScriptState.schema.xml"/> 
-- 
GitLab