{-# LANGUAGE ScopedTypeVariables #-}

{- |

This is the only other module aside from "Config.Dyre" which needs
to be imported specially. It contains functions for restarting the
program (which, usefully, will cause a recompile if the config has
been changed), as well as saving and restoring state across said
restarts.

The impossibly simple function arguments are a consequence of a
little cheating we do using the "System.IO.Storage" library. Of
course, we can't use the stored data unless something else put
it there, so this module will probably explode horribly if used
outside of a program whose recompilation is managed by Dyre.

The functions for saving and loading state come in two variants:
one which uses the 'Read' and 'Show' typeclasses, and one which
uses "Data.Binary" to serialize it. The 'Read' and 'Show' versions
are much easier to use thanks to automatic deriving, but the
binary versions offer more control over saving and loading, as
well as probably being a bit faster.

-}
module Config.Dyre.Relaunch
  ( relaunchMaster
  , relaunchWithTextState
  , relaunchWithBinaryState
  , saveTextState
  , saveBinaryState
  , restoreTextState
  , restoreBinaryState
  ) where

import Data.Maybe           ( fromMaybe )
import System.IO            ( writeFile, readFile )
import Data.Binary          ( Binary, encodeFile, decodeFile )
import Control.Exception    ( try, SomeException )
import System.FilePath      ( (</>) )
import System.Directory     ( getTemporaryDirectory )

import System.IO.Storage    ( putValue )
import Config.Dyre.Options  ( getMasterBinary, getStatePersist )
import Config.Dyre.Compat   ( customExec, getPIDString )

-- | Just relaunch the master binary. We don't have any important
--   state to worry about. (Or, like when @relaunchWith\<X\>State@ calls
--   it, we're managing state on our own). It takes an argument which
--   can optionally specify a new set of arguments. If it is given a
--   value of 'Nothing', the current value of 'System.Environment.getArgs' will be used.
relaunchMaster :: Maybe [String] -> IO ()
relaunchMaster :: Maybe [FilePath] -> IO ()
relaunchMaster Maybe [FilePath]
otherArgs = do
    FilePath
masterPath <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => FilePath -> a
error FilePath
"'dyre' data-store doesn't exist (in Config.Dyre.Relaunch.relaunchMaster)") IO (Maybe FilePath)
getMasterBinary
    forall a. FilePath -> Maybe [FilePath] -> IO a
customExec FilePath
masterPath Maybe [FilePath]
otherArgs

-- | Relaunch the master binary, but first preserve the program
--   state so that we can use the 'restoreTextState' functions to
--   get it back again later.
relaunchWithTextState :: Show a => a -> Maybe [String] -> IO ()
relaunchWithTextState :: forall a. Show a => a -> Maybe [FilePath] -> IO ()
relaunchWithTextState a
state Maybe [FilePath]
otherArgs = do
    forall a. Show a => a -> IO ()
saveTextState a
state
    Maybe [FilePath] -> IO ()
relaunchMaster Maybe [FilePath]
otherArgs

-- | Serialize the state for later restoration with 'restoreBinaryState',
--   and then relaunch the master binary.
relaunchWithBinaryState :: Binary a => a -> Maybe [String] -> IO ()
relaunchWithBinaryState :: forall a. Binary a => a -> Maybe [FilePath] -> IO ()
relaunchWithBinaryState a
state Maybe [FilePath]
otherArgs = do
    forall a. Binary a => a -> IO ()
saveBinaryState a
state
    Maybe [FilePath] -> IO ()
relaunchMaster Maybe [FilePath]
otherArgs

-- | Calculate the path that will be used for saving the state.
--   The path used to load the state, meanwhile, is passed to the
--   program with the '--dyre-persist-state=<path>' flag.
genStatePath :: IO FilePath
genStatePath :: IO FilePath
genStatePath = do
    FilePath
pidString <- IO FilePath
getPIDString
    FilePath
tempDir   <- IO FilePath
getTemporaryDirectory
    let statePath :: FilePath
statePath = FilePath
tempDir FilePath -> FilePath -> FilePath
</> FilePath
pidString forall a. [a] -> [a] -> [a]
++ FilePath
".state"
    forall a. Typeable a => FilePath -> FilePath -> a -> IO ()
putValue FilePath
"dyre" FilePath
"persistState" FilePath
statePath
    forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
statePath

-- | Serialize a state as text, for later loading with the
--   'restoreTextState' function.
saveTextState :: Show a => a -> IO ()
saveTextState :: forall a. Show a => a -> IO ()
saveTextState a
state = do
    FilePath
statePath <- IO FilePath
genStatePath
    FilePath -> FilePath -> IO ()
writeFile FilePath
statePath forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show forall a b. (a -> b) -> a -> b
$ a
state

-- | Serialize a state as binary data, for later loading with
--   the 'restoreBinaryState' function.
saveBinaryState :: Binary a => a -> IO ()
saveBinaryState :: forall a. Binary a => a -> IO ()
saveBinaryState a
state = do
    FilePath
statePath <- IO FilePath
genStatePath
    forall a. Binary a => FilePath -> a -> IO ()
encodeFile FilePath
statePath forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ a
state

-- | Restore state which has been serialized through the
--   'saveTextState' function. Takes a default which is
--   returned if the state doesn't exist.
restoreTextState :: Read a => a -> IO a
restoreTextState :: forall a. Read a => a -> IO a
restoreTextState a
d = do
    Maybe FilePath
statePath <- IO (Maybe FilePath)
getStatePersist
    case Maybe FilePath
statePath of
         Maybe FilePath
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return a
d
         Just FilePath
sp -> do
             FilePath
stateData <- FilePath -> IO FilePath
readFile FilePath
sp
             Either SomeException a
result <- forall e a. Exception e => IO a -> IO (Either e a)
try forall a b. (a -> b) -> a -> b
$ forall a. Read a => FilePath -> IO a
readIO FilePath
stateData
             case Either SomeException a
result of
                  Left  (SomeException
_ :: SomeException) -> forall (m :: * -> *) a. Monad m => a -> m a
return a
d
                  Right                    a
v -> forall (m :: * -> *) a. Monad m => a -> m a
return a
v

-- | Restore state which has been serialized through the
--   'saveBinaryState' function. Takes a default which is
--   returned if the state doesn't exist.
restoreBinaryState :: Binary a => a -> IO a
restoreBinaryState :: forall a. Binary a => a -> IO a
restoreBinaryState a
d = do
    Maybe FilePath
statePath <- IO (Maybe FilePath)
getStatePersist
    case Maybe FilePath
statePath of
         Maybe FilePath
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return a
d
         Just FilePath
sp -> do Maybe a
state <- forall a. Binary a => FilePath -> IO a
decodeFile FilePath
sp
                       forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe a
d Maybe a
state