Commit 2dcba67f authored by Petr Pudlak's avatar Petr Pudlak

Add a module for livelock related functions

Currently it exports a function for creating livelock files.
Signed-off-by: default avatarPetr Pudlak <pudlak@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 53eea5bb
......@@ -837,6 +837,7 @@ HS_LIB_SRCS = \
src/Ganeti/Utils.hs \
src/Ganeti/Utils/Atomic.hs \
src/Ganeti/Utils/AsyncWorker.hs \
src/Ganeti/Utils/Livelock.hs \
src/Ganeti/VCluster.hs \
src/Ganeti/WConfd/ConfigState.hs \
src/Ganeti/WConfd/ConfigWriter.hs \
......
{-| Utilities related to livelocks and death detection
-}
{-
Copyright (C) 2014 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
-}
module Ganeti.Utils.Livelock
( Livelock
, mkLivelockFile
) where
import qualified Control.Exception as E
import Control.Monad
import Control.Monad.Error
import System.Directory (doesFileExist)
import System.IO
import System.Posix.IO
import System.Posix.Types (Fd)
import System.Time (ClockTime(..), getClockTime)
import Ganeti.BasicTypes
import Ganeti.Path (livelockFile)
import Ganeti.Utils (lockFile)
type Livelock = FilePath
-- | Appends the current time to the given prefix, creates
-- the lockfile in the appropriate directory, and locks it.
-- Returns its full path and the file's file descriptor.
mkLivelockFile :: (Error e, MonadError e m, MonadIO m)
=> FilePath -> m (Fd, Livelock)
mkLivelockFile prefix = do
(TOD secs _) <- liftIO getClockTime
lockfile <- liftIO . livelockFile $ prefix ++ "_" ++ show secs
fd <- liftIO (lockFile lockfile) >>= \r -> case r of
Bad msg -> failError $ "Locking the livelock file " ++ lockfile
++ ": " ++ msg
Ok fd -> return fd
return (fd, lockfile)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment