From f5f71aaf90717ff560318382b7d5cdd21c690cf3 Mon Sep 17 00:00:00 2001
From: Dinyar Rabady <dinyar.rabady@cern.ch>
Date: Sat, 24 Sep 2022 13:09:50 +0200
Subject: [PATCH 1/2] Quit more gracefully if configuration incomplete

Belongs to #15.
---
 etc/scdaq/scdaq.conf |  6 +++---
 scripts/runSCdaq.sh  |  4 ++++
 src/config.cc        | 14 +++++++++++---
 src/config.h         |  6 +++---
 src/scdaq.cc         |  5 ++++-
 5 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/etc/scdaq/scdaq.conf b/etc/scdaq/scdaq.conf
index 63fabdf3..36f37f4f 100644
--- a/etc/scdaq/scdaq.conf
+++ b/etc/scdaq/scdaq.conf
@@ -59,7 +59,7 @@ enable_stream_processor:yes
 # Note: When changing the processing type, change also "output_filename_prefix"
 # in the file output section.
 #
-#processor_type:GMT
+processor_type:
 
 # Enable software zero-supression
 doZS:yes
@@ -70,9 +70,9 @@ doZS:yes
 ##
 ################################################################################
 
-#output_filename_prefix:scout_GMT
+output_filename_prefix:
 
-#output_filename_base:/fff/BU0/ramdisk/scdaq
+output_filename_base:
 
 max_file_size:8589934592
 
diff --git a/scripts/runSCdaq.sh b/scripts/runSCdaq.sh
index ef52faf6..a247598e 100755
--- a/scripts/runSCdaq.sh
+++ b/scripts/runSCdaq.sh
@@ -15,6 +15,10 @@ while true
 do
     echo "Starting scdaq..."
     /opt/scdaq/bin/scdaq --config /etc/scdaq/scdaq.conf 2>&1
+    if [[ ${PIPESTATUS[0]} == 78 ]]; then # Numerical value of EX_CONFIG.
+      # If the configuration is incomplete there's no point in us retrying forever..
+      exit 1
+    fi
     echo "Resetting the board..."
     ../scripts/reset-firmware.sh
     echo "Clearing caches..."
diff --git a/src/config.cc b/src/config.cc
index 5e3a1482..b292669b 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -3,6 +3,8 @@
 #include "log.h"
 
 config::config(std::string filename){
+  bool valid = true;
+
   std::ifstream in(filename.c_str(),std::ios_base::in);
   std::string line;
   while ( !std::getline(in, line).eof() ) {
@@ -31,19 +33,25 @@ config::config(std::string filename){
     std::string key = line.substr(0, delim);
     std::string value = line.substr(delim + 1);
 
-    if (key.empty() || value.empty()) {
+    if (key.empty()) {
       // Skip ill formated lines
        continue;      
     }
 
-    //std::cout << "key: " << key << ", value: " << value << "\n";
+    if (value.empty()) {
+      LOG(ERROR) << "Configuration entry " << key << " has no value!";
+      valid &= false;
+    }
 
     vmap[key] = value;
   }
+
+  if (!valid) {
+    throw std::invalid_argument("Configuration error: Keys with no value detected!");
+  }
 }
 void config::print()const {
   for (std::map<std::string,std::string>::const_iterator it = vmap.begin(); it!=vmap.end(); it++){
     LOG(INFO) << "key " << it->first << " value " << it->second;
   }
-
 }
diff --git a/src/config.h b/src/config.h
index e25680e5..f54c5f92 100644
--- a/src/config.h
+++ b/src/config.h
@@ -10,7 +10,7 @@
 
 class config{
 public:
-  
+
   enum class InputType { WZDMA, DMA, FILEDMA, MICRONDMA, FILE };
 
   config(std::string filename);
@@ -144,9 +144,9 @@ public:
     return boost::lexical_cast<uint32_t>(v.c_str());
   }
 private:
-  
+
   std::map<std::string,std::string> vmap;
-  
+
 
 };
 #endif
diff --git a/src/scdaq.cc b/src/scdaq.cc
index 21f4c28a..9377feba 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -9,6 +9,7 @@
 #include <cctype>
 #include <string>
 #include <iostream>
+#include <sysexits.h>
 
 #include <boost/bind.hpp>
 #include <boost/asio.hpp>
@@ -198,7 +199,9 @@ if(argc < 2){
 
     //    utility::report_elapsed_time((tbb::tick_count::now() - mainStartTime).seconds());
     return retval;
-
+  } catch (std::exception& e) {
+    LOG(FATAL) << "Configuration invalid! Error text is \"" << e.what() << "\" Bailing out.";
+    return EX_CONFIG;
   } catch(std::exception& e) {
     LOG(ERROR) << "Error occurred. error text is: \"" << e.what() << "\"";
     return 1;
-- 
GitLab


From 74090015669040420c3605247b39340ff26642f4 Mon Sep 17 00:00:00 2001
From: Dinyar Rabady <dinyar.rabady@cern.ch>
Date: Sat, 24 Sep 2022 13:23:02 +0200
Subject: [PATCH 2/2] Limit the number of restarts via systemd

We allow 5 restarts within a 100 second interval to avoid persisting in
a hopeless situation.
Belongs to #15.
---
 init.d/runSCdaq.service | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/init.d/runSCdaq.service b/init.d/runSCdaq.service
index 9f8b823c..2eb2a800 100644
--- a/init.d/runSCdaq.service
+++ b/init.d/runSCdaq.service
@@ -9,7 +9,9 @@ User=scouter
 Group=scouter
 WorkingDirectory=/opt/scdaq/scripts
 Restart=always
-RestartSec=10
+RestartSec=5
+StartLimitInterval=100
+StartLimitBurst=5
 
 [Install]
 WantedBy=multi-user.target
-- 
GitLab