Commits

nakamura committed 08e22a1

save logger's run file

Comments (0)

Files changed (2)

     , masterSock        :: FilePath
     , masterLog         :: FilePath
     , daemonsDir        :: FilePath
+    , loggersDir        :: FilePath
     , logDir            :: FilePath
     , fastspawnCount    :: Int
     , fastspawnInterval :: Double
         , masterSock = defaultRcPath' </> "master.sock"
         , masterLog  = defaultRcPath' </> "master.log"
         , daemonsDir = defaultRcPath' </> "run"
+        , loggersDir = defaultRcPath' </> "run" </> "logger"
         , logDir     = defaultRcPath' </> "log"
 
         , fastspawnCount = 3
         }
 
 createRcDirs :: Config -> IO ()
-createRcDirs Config { rcPath = rp, daemonsDir = dp, logDir = lp } = do
+createRcDirs Config { rcPath = rp, daemonsDir = dp, loggersDir = lgp, logDir = lp } = do
     omask <- setFileCreationMask 0o0077
-    forM_ [rp, dp, lp] $ createDirectoryIfMissing True
+    forM_ [rp, dp, lgp, lp] $ createDirectoryIfMissing True
     setFileCreationMask omask
     return ()
 
 import System.Directory (getDirectoryContents)
 import System.Environment (getProgName, getEnvironment)
 import System.Exit (exitSuccess, ExitCode(ExitSuccess, ExitFailure))
+import System.FilePath (takeBaseName)
 import System.IO (Handle, IOMode(AppendMode, ReadWriteMode), openFile, hSetBuffering, BufferMode(LineBuffering), hGetLine, hPutStrLn, hClose, openTempFile, hPrint, hSeek, SeekMode(AbsoluteSeek), stdin)
 import System.FilePath ((</>))
 import System.Posix.Directory (getWorkingDirectory)
 
     p "[master] Loading daemons ..."
     blkSigs
-    mapM_ startDaemonManager =<< loadDaemons
+    mapM_ (uncurry startDaemonManager) =<< loadDaemons
     updateHandler
     unblkSigs
     p "[master] Done!"
     p "[setOutputToLog] done"
     return ()
 
-loadDaemons :: Master [Daemon]
+loadDaemons :: Master [(Daemon, Logger)]
 loadDaemons = do
     dd <-  asks daemonsDir
+    ld <-  asks loggersDir
     files <- io $ getDirectoryContents dd
     mds <- forM files $ \f -> do
               if ".run" `isSuffixOf` f
                 then do
-                    cs <- io $ readFile $ dd </> f
-                    io $ readMaybeIO cs
+                    md <- io $ readMaybeIO =<< readFile (dd </> f)
+                    case md of
+                        Nothing -> do
+                            p $ "[loadDaemons] cannot readMaybeIO " ++ (dd </> f)
+                            return Nothing
+                        Just d -> do
+                            let lf = ld </> takeBaseName f ++ "-log.run"
+                            -- because udon-master < 0.2 has not save loggers
+                            -- we should check whether file exists or not
+                            fe <- io $ fileExist lf
+                            ml <- if fe
+                                    then io $ readMaybeIO =<< readFile lf
+                                    else return Nothing
+                            l <- maybe (mkDefaultLogger d) return ml
+                            return $ Just (d, l)
                 else return Nothing
     return $ catMaybes mds
 
     p $ "[deleteDaemon] rename: " ++ df ++ " to " ++ df'
     io $ rename df df'
 
+mkDefaultLogger :: Daemon -> Master Logger
+mkDefaultLogger Daemon { dName = dn } = do
+    Config { loggerCmd = (lcmd, largs) } <- ask
+    cwd <- io getWorkingDirectory
+    env <- io getEnvironment
+    logd <- asks logDir
+    let logf = logd </> escapeFName (dn ++ ".log")
+        largs' = largs ++ [logf]
+        l = Daemon (dn ++ "-log") lcmd largs' cwd env True
+    return l
+
+saveLogger :: Logger -> Master ()
+saveLogger l = do
+    cfg <- ask
+    saveDaemon' l cfg { daemonsDir = loggersDir cfg }
+
 mkMasterSock :: Master Socket
 mkMasterSock = do
     sp <- asks masterSock
     if not exist
       then do
         saveDaemon d
-        startDaemonManager d
+        l <- mkDefaultLogger d
+        saveLogger l
+        startDaemonManager d l
         hPrintM h OK
       else hPrintM h $ NG "Daemon Already Exists"
 
     tid <- io $ forkIO $ runManager mgr cfg tds
     return (tds, tid)
 
-startDaemonManager :: Daemon -> Master ()
-startDaemonManager d@(Daemon { dName = dn }) = do
+startDaemonManager :: Daemon -> Logger -> Master ()
+startDaemonManager d@(Daemon { dName = dn }) l = do
     (pipe_r, pipe_w) <- io createPipe
 
     p "[startDaemonManager] start"
     tid <- io $ forkIO $ runManager daemonManager cfg tds
     p "[startDaemonManager] Done"
 
-    cwd <- io getWorkingDirectory
-    env <- io getEnvironment
-    logd <- asks logDir
-    -- TODO: per daemon logger command setting
-    let (lcmd, largs) = loggerCmd cfg
-        logf = logd </> escapeFName (dn ++ ".log")
-        largs' = largs ++ [logf]
-        l = Daemon (dn ++ "-log") lcmd largs' cwd env True
-        ls = DaemonState l NotStarted (Just pipe_r) (Just stdError)
+    let ls = DaemonState l NotStarted (Just pipe_r) (Just stdError)
 
     tls <- io $ newTMVarIO ls
 
     p "[startDaemonManager] forkIOing daemonManager for logger ..."
-    tid' <- io $ forkIO $ runManager daemonManager cfg tls
+    -- override daemonsDir for saveDaemon' to save logger's run file under loggersDir
+    tid' <- io $ forkIO $ runManager daemonManager (cfg { daemonsDir = loggersDir cfg }) tls
     p "[startDaemonManager] Done"
 
     p "[startDaemonManager] modifying state ..."