root / src / Ganeti / Utils / Livelock.hs @ 2dcba67f
History | View | Annotate | Download (1.8 kB)
1 |
{-| Utilities related to livelocks and death detection |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2014 Google Inc. |
8 |
|
9 |
This program is free software; you can redistribute it and/or modify |
10 |
it under the terms of the GNU General Public License as published by |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, but |
15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with this program; if not, write to the Free Software |
21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 |
02110-1301, USA. |
23 |
|
24 |
-} |
25 |
|
26 |
module Ganeti.Utils.Livelock |
27 |
( Livelock |
28 |
, mkLivelockFile |
29 |
) where |
30 |
|
31 |
import qualified Control.Exception as E |
32 |
import Control.Monad |
33 |
import Control.Monad.Error |
34 |
import System.Directory (doesFileExist) |
35 |
import System.IO |
36 |
import System.Posix.IO |
37 |
import System.Posix.Types (Fd) |
38 |
import System.Time (ClockTime(..), getClockTime) |
39 |
|
40 |
import Ganeti.BasicTypes |
41 |
import Ganeti.Path (livelockFile) |
42 |
import Ganeti.Utils (lockFile) |
43 |
|
44 |
type Livelock = FilePath |
45 |
|
46 |
-- | Appends the current time to the given prefix, creates |
47 |
-- the lockfile in the appropriate directory, and locks it. |
48 |
-- Returns its full path and the file's file descriptor. |
49 |
mkLivelockFile :: (Error e, MonadError e m, MonadIO m) |
50 |
=> FilePath -> m (Fd, Livelock) |
51 |
mkLivelockFile prefix = do |
52 |
(TOD secs _) <- liftIO getClockTime |
53 |
lockfile <- liftIO . livelockFile $ prefix ++ "_" ++ show secs |
54 |
fd <- liftIO (lockFile lockfile) >>= \r -> case r of |
55 |
Bad msg -> failError $ "Locking the livelock file " ++ lockfile |
56 |
++ ": " ++ msg |
57 |
Ok fd -> return fd |
58 |
return (fd, lockfile) |