urbit/pkg/hs/urbit-king/lib/Urbit/Vere/LockFile.hs

48 lines
1.5 KiB
Haskell
Raw Normal View History

2020-01-23 07:16:09 +03:00
{-|
Acquire and release the vere lockfile.
-}
module Urbit.Vere.LockFile (lockFile) where
2020-01-23 07:16:09 +03:00
import Urbit.Prelude
2021-02-03 02:58:30 +03:00
import Urbit.King.App.Class
import Data.Default (def)
import RIO.Directory (createDirectoryIfMissing)
import System.IO.LockFile.Internal (LockingParameters(..), RetryStrategy(..),
2021-02-03 02:58:30 +03:00
LockingException(..), lock, unlock)
--------------------------------------------------------------------------------
2021-02-03 02:58:30 +03:00
lockFile :: (HasLogFunc e, HasStderrLogFunc e) => FilePath -> RAcquire e ()
lockFile pax = void $ mkRAcquire start stop
where
fil = pax <> "/.vere.lock"
stop handle = do
2021-02-03 02:58:30 +03:00
logInfo $ display @Text $ ("Releasing lock file: " <> pack fil)
io $ unlock fil handle
params = def { retryToAcquireLock = No }
start = do
createDirectoryIfMissing True pax
logInfo $ display @Text $ ("Taking lock file: " <> pack fil)
2021-02-03 02:58:30 +03:00
handle failure $ io (lock params fil)
failure (e :: LockingException) = do
logStderr $ logError $ display @Text $
"Cannot acquire lock file " <> pack fil <> "."
logStderr $ logError $
"Please make sure there are no other instances of this ship running, "
<> "then try again."
logStderr $ logError $
"If you are sure, you can delete the file and try again."
throwIO e
logStderr :: HasStderrLogFunc e => RIO LogFunc a -> RIO e a
logStderr action = do
logFunc <- view stderrLogFuncL
runRIO logFunc action