A peer-to-peer stack for code collaboration
daemon: Use 'ensureMachineLoaded' for 'send' handler
Thomas Scholtes3a27ba5a5461
diff --git a/exe/Daemon.hs b/exe/Daemon.hsindex 078803a..88de77c 100644--- a/exe/Daemon.hs+++ b/exe/Daemon.hs@@ -205,7 +205,7 @@ newMachine = do-- fixed amount of time.query :: MachineId -> Api.QueryRequest -> Daemon Api.QueryResponsequery id (Api.QueryRequest v) = do- m <- checkMachineLoaded id+ m <- ensureMachineLoaded idbumpPolling idcase fst <$> runIdentity $ runLang (machineState m) $ eval v ofLeft err -> throwError $ MachineError id (InvalidInput err)@@ -224,21 +224,15 @@ query id (Api.QueryRequest v) = do-- ack.send :: MachineId -> Api.SendRequest -> Daemon Api.SendResponsesend id (Api.SendRequest expressions) = do- cm_ <- lookupMachine id- case cm_ of- Nothing -> do- m <- newReader id- requestInput (machineSubscription m)- Just (UninitialisedReader _) -> do- m <- initAsReader id- requestInput (machineSubscription m)- Just (Cached Machine{ machineMode = Reader, ..}) -> requestInput machineSubscription- Just (Cached Machine{ machineMode = Writer }) -> do- logInfo "Applying input"- [ ("machine-id", getMachineId id)- ]- results <- writeInputs id expressions Nothing- pure Api.SendResponse{..}+ Machine{machineMode, machineSubscription} <- ensureMachineLoaded id+ case machineMode of+ Reader -> requestInput machineSubscription+ Writer -> do+ logInfo "Applying input"+ [ ("machine-id", getMachineId id)+ ]+ results <- writeInputs id expressions Nothing+ pure Api.SendResponse{..}wherematchMessage nonce = \case(New InputsApplied{results, nonce = Just nonce'}) | nonce' == nonce -> Just results@@ -266,23 +260,23 @@ send id (Api.SendRequest expressions) = doprValues = T.intercalate "," . (renderCompactPretty <$>)--- * Helpers-lookupMachine :: MachineId -> Daemon (Maybe CachedMachine)-lookupMachine id = do+-- | Given an 'MachineId', makes sure the machine is loaded and+-- fetch the latest inputs.+--+-- If the machine is not yet loaded into memory we fetch its inputs, load it+-- into memory and start following its changes.+ensureMachineLoaded :: MachineId -> Daemon Machine+ensureMachineLoaded id = domsCMap <- asks machines- liftIO $ CMap.lookup id (getMachines msCMap)---- | Given an 'MachineId', makes sure the machine is in the cache and--- updated.-checkMachineLoaded :: MachineId -> Daemon Machine-checkMachineLoaded id = do- m_ <- lookupMachine id+ m_ <- liftIO $ CMap.lookup id (getMachines msCMap)case m_ ofNothing -> do-- In this case we have not seen the machine before so we act as-- a reader.- newReader id+ m <- initAsReader id+ writeMachineConfig+ pure mJust (UninitialisedReader _) ->-- We try to initialise the reader again.initAsReader id@@ -421,14 +415,6 @@ initAsReader id = dobumpPolling id_ -> pure ()--- | Initiate a reader the daemon wasn't following before (adds it to the--- follow-file).-newReader :: MachineId -> Daemon Machine-newReader id = do- m <- initAsReader id- writeMachineConfig- pure m--- | Try to initiate a reader, but don't fail in case of errors; just log a-- message.initReaderNoFail :: MachineId -> Daemon ()