A peer-to-peer stack for code collaboration
daemon: Wait for IPFS daemon
Thomas Scholtes7ab1df903d1f
diff --git a/exe/Daemon.hs b/exe/Daemon.hsindex c83b1cd..717febc 100644--- a/exe/Daemon.hs+++ b/exe/Daemon.hs@@ -7,8 +7,9 @@-- the RFC>.module Daemon (main) where-import Protolude hiding (fromStrict, option, poll)+import Protolude hiding (catch, fromStrict, option, poll, tryJust)+import Control.Exception.Safe hiding (Handler)import qualified Data.Map.Strict as Mapimport qualified Data.Text as Timport qualified Data.Time.Clock.System as Time@@ -80,6 +81,38 @@ opts defaultMachineConfigFile = do)pure $ Opts{..}+-- | Repeatedly tries to connect to the Radicle IPFS daemon API until+-- succesfull. Times out after five seconds and throws 'IpfsDaemonNotReachable'.+waitForIpfsDaemon :: Daemon ()+waitForIpfsDaemon = do+ isOnline <- checkIpfsIsOnline+ if isOnline+ then pure ()+ else do+ logInfo "Waiting for Radicle IPFS daemon" []+ go 5+ where+ go :: Int -> Daemon ()+ go 0 = throwError IpfsDaemonNotReachable+ go n = do+ isOnline <- checkIpfsIsOnline+ if isOnline+ then pure ()+ else do+ liftIO $ threadDelay (1000 * 1000)+ go (n - 1)++ checkIpfsIsOnline :: Daemon Bool+ checkIpfsIsOnline = liftIO $ do+ result <- tryJust isIpfsExceptionNoDaemon Ipfs.version+ pure $ case result of+ Left _ -> False+ Right _ -> True++ isIpfsExceptionNoDaemon :: Ipfs.IpfsException -> Maybe ()+ isIpfsExceptionNoDaemon Ipfs.IpfsExceptionNoDaemon = Just ()+ isIpfsExceptionNoDaemon _ = Nothing+-- We use 'Void' to enforce the use of 'exitFailure'main :: IO Voidmain = do@@ -89,7 +122,9 @@ main = domachines <- CachedMachines <$> CMap.emptylet env = Env{ logLevel = if debug then LogDebug else LogInfo, ..}machineConfig <- readMachineConfigIO machineConfigFileLock machineConfigFile- initRes <- runDaemon env (init machineConfig)+ initRes <- runDaemon env $ do+ waitForIpfsDaemon+ init machineConfigcase initRes ofLeft err -> dologDaemonError err@@ -136,7 +171,7 @@ server env = hoistServer Api.daemonApi nt daemonServerInvalidInput _ -> err400AckTimeout -> err504_ -> err500- CouldNotCreateMachine _ -> err500+ _ -> err500-- * Initdiff --git a/package.yaml b/package.yamlindex 7d471a8..899e1a6 100644--- a/package.yaml+++ b/package.yaml@@ -155,6 +155,7 @@ executables:- mtl- containers- optparse-applicative+ - safe-exceptions- servant-server- text- warpdiff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hsindex cb49e7f..adc0fbc 100644--- a/src/Radicle/Daemon/Monad.hs+++ b/src/Radicle/Daemon/Monad.hs@@ -61,10 +61,12 @@ data MachineErrordata Error= MachineError MachineId MachineError| CouldNotCreateMachine IpfsException+ | IpfsDaemonNotReachabledisplayError :: Error -> (Text, [(Text,Text)])displayError = \caseCouldNotCreateMachine (ipfsErr -> (msg, infos)) -> ("Could not create IPFS machine", ("message", msg) : infos)+ IpfsDaemonNotReachable -> ("Could not connect to Radicle IPFS daemon", [])MachineError id e -> let mid = ("machine-id", getMachineId id) incase e ofInvalidInput err -> ("Invalid radicle input", [mid, ("radicle-error", renderCompactPretty err)])diff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hsindex 6124fa1..3a18ee2 100644--- a/src/Radicle/Ipfs.hs+++ b/src/Radicle/Ipfs.hs@@ -8,6 +8,9 @@ module Radicle.Ipfs, addressToText, addressFromText+ , VersionResponse(..)+ , version+, KeyGenResponse(..), keyGen@@ -145,6 +148,16 @@ addressFromText t =-- * IPFS node API--------------------------------------------------------------------------+newtype VersionResponse = VersionResponse Text++instance FromJSON VersionResponse where+ parseJSON = Aeson.withObject "VersionResponse" $ \o -> do+ v <- o .: "Version"+ pure $ VersionResponse v++version :: IO VersionResponse+version = ipfsHttpGet "version" []+data PubsubMessage = PubsubMessage{ messageTopicIDs :: [Text], messageData :: ByteString