Skip to content

A peer-to-peer stack for code collaboration

daemon: Wait for IPFS daemon

Thomas Scholtes7ab1df903d1f
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index 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 Map
import qualified Data.Text as T
import 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 Void
main = do
@@ -89,7 +122,9 @@ main = do
machines <- CachedMachines <$> CMap.empty
let env = Env{ logLevel = if debug then LogDebug else LogInfo, ..}
machineConfig <- readMachineConfigIO machineConfigFileLock machineConfigFile
- initRes <- runDaemon env (init machineConfig)
+ initRes <- runDaemon env $ do
+ waitForIpfsDaemon
+ init machineConfig
case initRes of
Left err -> do
logDaemonError err
@@ -136,7 +171,7 @@ server env = hoistServer Api.daemonApi nt daemonServer
InvalidInput _ -> err400
AckTimeout -> err504
_ -> err500
- CouldNotCreateMachine _ -> err500
+ _ -> err500
-- * Init
diff --git a/package.yaml b/package.yaml
index 7d471a8..899e1a6 100644
--- a/package.yaml
+++ b/package.yaml
@@ -155,6 +155,7 @@ executables:
- mtl
- containers
- optparse-applicative
+ - safe-exceptions
- servant-server
- text
- warp
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index cb49e7f..adc0fbc 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -61,10 +61,12 @@ data MachineError
data Error
= MachineError MachineId MachineError
| CouldNotCreateMachine IpfsException
+ | IpfsDaemonNotReachable
displayError :: Error -> (Text, [(Text,Text)])
displayError = \case
CouldNotCreateMachine (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) in
case e of
InvalidInput err -> ("Invalid radicle input", [mid, ("radicle-error", renderCompactPretty err)])
diff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hs
index 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