From 26f5964ef7e9c6d9ef038ab4ebcf4a576ddb1e6a Mon Sep 17 00:00:00 2001 From: Benjamin Summers Date: Tue, 17 Sep 2019 19:45:51 -0700 Subject: [PATCH] Move lockfile logic into it's own file. --- pkg/king/app/Main.hs | 16 +--------------- pkg/king/lib/Vere/LockFile.hs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 pkg/king/lib/Vere/LockFile.hs diff --git a/pkg/king/app/Main.hs b/pkg/king/app/Main.hs index 3e329d9cb..2be336242 100644 --- a/pkg/king/app/Main.hs +++ b/pkg/king/app/Main.hs @@ -105,6 +105,7 @@ import System.Environment (getProgName) import System.Posix.Signals (Handler(Catch), installHandler, sigTERM) import Text.Show.Pretty (pPrint) import Urbit.Time (Wen) +import Vere.LockFile (lockFile) import qualified CLI import qualified Data.Set as Set @@ -196,21 +197,6 @@ tryPlayShip shipPath = do rio $ logTrace "SHIP RESUMED" Pier.pier shipPath Nothing sls -lockFile :: HasLogFunc e => FilePath -> RAcquire e () -lockFile pax = void $ mkRAcquire start stop - where - fil = pax <> "/.vere.lock" - - stop handle = do - logInfo $ display @Text $ ("Releasing lock file: " <> pack fil) - io $ Lock.unlock fil handle - - params = def { Lock.retryToAcquireLock = Lock.No } - - start = do - logInfo $ display @Text $ ("Taking lock file: " <> pack fil) - io (Lock.lock params fil) - tryResume :: HasLogFunc e => FilePath -> RIO e () tryResume shipPath = do rwith resumedPier $ \(serf, log, ss) -> do diff --git a/pkg/king/lib/Vere/LockFile.hs b/pkg/king/lib/Vere/LockFile.hs new file mode 100644 index 000000000..68b60837c --- /dev/null +++ b/pkg/king/lib/Vere/LockFile.hs @@ -0,0 +1,24 @@ +module Vere.LockFile (lockFile) where + +import UrbitPrelude + +import Data.Default (def) +import System.IO.LockFile.Internal (LockingParameters(..), RetryStrategy(..), + lock, unlock) + +-------------------------------------------------------------------------------- + +lockFile :: HasLogFunc e => FilePath -> RAcquire e () +lockFile pax = void $ mkRAcquire start stop + where + fil = pax <> "/.vere.lock" + + stop handle = do + logInfo $ display @Text $ ("Releasing lock file: " <> pack fil) + io $ unlock fil handle + + params = def { retryToAcquireLock = No } + + start = do + logInfo $ display @Text $ ("Taking lock file: " <> pack fil) + io (lock params fil)