Entering forkIO / catch block

I am creating a servant application that generates reports using the shaking started with the GETting url. Until the request for registration of the material works, and now I am faced with a problem.

One handler startstarts generating a report - since it can take quite a while - I return 200 immediately and create a process to do the real work, logging is done logging-effect.

A simplified version of my handler startlooks like this:

start :: IO () -> App ()
start mkReport = do
  logInfo $ PP.text "Report generation started"
  _ <- liftIO . forkIO $ catch mkReport caught
  where caught e =  do logError $ PP.text "Report generation crashed: " <>
                     {-^^^^^^^-} (PP.text . T.pack $ displayException e)
                       cleanup
  return ()

Appis an instance MonadLog (WithTimestamp (WithSeverity Doc))- therefore it logInfoworks fine.

To be precise, Appthis is the following type of newtype:

newtype App a =
    App { _runApp :: ReaderT Environment (ExceptT ServantErr
                     (LoggingT (WithTimestamp (WithSeverity Doc)) IO)) a
        } deriving ( Functor, Applicative, Monad, MonadReader Environment,
                   , MonadError ServantErr, MonadLog (WithTimestamp (WithSeverity Doc)))

logError caught - cleanup, / , . ✘

No instance for MonadLog (WithSeverity Doc) IO arising from a use of 'logError'...

, IO, ghc , , !

, - , - :

  • forkIO ?
  • - forkIOish :: MonadIO m => m () -> m ThreadId catchIsh :: (MonadIO m, Exception e) => m a -> (e -> m a) -> m a, ? - , - liftIO mkReport logError App .
  • "" ?
+4

Source: https://habr.com/ru/post/1671915/


All Articles