{-
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.
-}moduleConfig.Dyre.Relaunch(relaunchMaster,relaunchWithTextState,relaunchWithBinaryState,saveTextState,saveBinaryState,restoreTextState,restoreBinaryState)whereimportData.Maybe(fromMaybe,fromJust)importSystem.IO(writeFile,readFile)importData.Binary(Binary,encodeFile,decodeFile)importSystem.IO.Error(try)importSystem.FilePath((</>))importSystem.Directory(getTemporaryDirectory,removeFile)importSystem.IO.Storage(putValue,delValue)importConfig.Dyre.Options(customOptions,getMasterBinary,getStatePersist)importConfig.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 'getArgs' will be used.relaunchMaster::Maybe[String]->IO()relaunchMasterotherArgs=domasterPath<-fmapfromJustgetMasterBinarycustomExecmasterPathotherArgs-- | 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::Showa=>a->Maybe[String]->IO()relaunchWithTextStatestateotherArgs=dosaveTextStatestaterelaunchMasterotherArgs-- | Serialize the state for later restoration with 'restoreBinaryState',-- and then relaunch the master binary.relaunchWithBinaryState::Binarya=>a->Maybe[String]->IO()relaunchWithBinaryStatestateotherArgs=dosaveBinaryStatestaterelaunchMasterotherArgs-- | 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::IOFilePathgenStatePath=dopidString<-getPIDStringtempDir<-getTemporaryDirectoryletstatePath=tempDir</>pidString++".state"putValue"dyre""persistState"statePathreturnstatePath-- | Serialize a state as text, for later loading with the-- 'restoreTextState' function.saveTextState::Showa=>a->IO()saveTextStatestate=dostatePath<-genStatePathwriteFilestatePath.show$state-- | Serialize a state as binary data, for later loading with-- the 'restoreBinaryState' function.saveBinaryState::Binarya=>a->IO()saveBinaryStatestate=dostatePath<-genStatePathencodeFilestatePath.Just$state-- | Restore state which has been serialized through the-- 'saveTextState' function. Takes a default which is-- returned if the state doesn't exist.restoreTextState::Reada=>a->IOarestoreTextStated=dostatePath<-getStatePersistcasestatePathofNothing->returndJustsp->dostateData<-readFilespresult<-try$readIOstateDatacaseresultofLeft_->returndRightv->returnv-- | Restore state which has been serialized through the-- 'saveBinaryState' function. Takes a default which is-- returned if the state doesn't exist.restoreBinaryState::Binarya=>a->IOarestoreBinaryStated=dostatePath<-getStatePersistcasestatePathofNothing->returndJustsp->dostate<-decodeFilespreturn$fromMaybedstate