]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/Main.hs
Wrap the entire daemonization process in an exception handler.
[dead/htsn.git] / src / Main.hs
index a06bd2338e62a0371fb90215d43a366478719347..3f0974346d38389c20b00fdb8bd4b4d02eeb4860 100644 (file)
@@ -5,7 +5,7 @@ module Main
 where
 
 import Control.Concurrent ( threadDelay )
-import Control.Exception.Base ( bracket )
+import Control.Exception ( bracket, throw )
 import Control.Monad ( when )
 import Data.List ( isPrefixOf )
 import Data.Maybe ( isNothing )
@@ -296,7 +296,7 @@ main = do
 
   -- If we were asked to daemonize, do that; otherwise just run the thing.
   if (daemonize cfg)
-  then full_daemonize cfg run_program
+  then try_daemonize cfg run_program
   else run_program
 
   where
@@ -313,3 +313,16 @@ main = do
       catchIOError (connect_and_parse cfg host) (report_error . show)
       thread_sleep 5 -- Wait 5s before attempting to reconnect.
       round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)
+
+
+    -- | A exception handler around full_daemonize. If full_daemonize
+    --   doesn't work, we report the error and crash. This is fine; we
+    --   only need the program to be resilient once it actually starts.
+    --
+    try_daemonize :: Configuration -> IO () -> IO ()
+    try_daemonize cfg program =
+      catchIOError
+        (full_daemonize cfg program)
+        (\e -> do
+          report_error (show e)
+          throw e)