catch the execption during buildSensors

If there any sensor is being created or deleted,
the buildSenosrs will throw exceptions and the application will crash.
Add a retry logic to catch such exception and try again,
will quit the process finally if failed more than 5 times.

Tested:
Changing the PSU sensor threshold using ipmi commands,
no pid service crash log.

Signed-off-by: Yong Li <yong.b.li@linux.intel.com>
Change-Id: Icf96bfb01bce4a3b5c22095f0bef0d793fd8430d
diff --git a/dbus/dbusconfiguration.cpp b/dbus/dbusconfiguration.cpp
index 1ed13b0..02903e0 100644
--- a/dbus/dbusconfiguration.cpp
+++ b/dbus/dbusconfiguration.cpp
@@ -254,7 +254,7 @@
         }
 
         std::cout << "New configuration detected, reloading\n.";
-        restartControlLoops();
+        tryRestartControlLoops();
     });
 
     return 1;
diff --git a/main.cpp b/main.cpp
index be0691d..2ab3fc4 100644
--- a/main.cpp
+++ b/main.cpp
@@ -123,6 +123,32 @@
     }
 }
 
+void tryRestartControlLoops()
+{
+    int count = 0;
+    for (count = 0; count <= 5; count++)
+    {
+        try
+        {
+            restartControlLoops();
+            break;
+        }
+        catch (const std::exception& e)
+        {
+            std::cerr << count
+                      << " Failed during restartControlLoops, try again: "
+                      << e.what() << "\n";
+            if (count >= 5)
+            {
+                throw std::runtime_error(e.what());
+            }
+        }
+        std::this_thread::sleep_for(std::chrono::seconds(10));
+    }
+
+    return;
+}
+
 int main(int argc, char* argv[])
 {
     loggingPath = "";
@@ -155,7 +181,7 @@
      * it.
      */
 
-    restartControlLoops();
+    tryRestartControlLoops();
 
     io.run();
     return 0;
diff --git a/util.hpp b/util.hpp
index e40b61f..5d9ab0d 100644
--- a/util.hpp
+++ b/util.hpp
@@ -30,7 +30,7 @@
 
 IOInterfaceType getReadInterfaceType(const std::string& path);
 
-void restartControlLoops(void);
+void tryRestartControlLoops(void);
 
 /*
  * Given a configuration structure, fill out the information we use within the