haskell - How do I cause a WARP server to terminate? -




i have http application server needs exit when handling request under conditions (in order restarted supervisor).

given main like:

import network.wai.handler.warp (run)  main :: io () main =   config <- readconfig   run (portnumber config) (makeapp config) 

and handler like:

livenessserver1 :: utctime -> filepath -> server livenessprobeapi1 livenessserver1 initialmodificationtime monitorpath =   mtime <- liftio $ getmodificationtime monitorpath   case mtime == initialmodificationtime of     true  -> return $ liveness initialmodificationtime mtime     false -> throwerror $ err500 { errbody = "file modified." } 

how cause process end after delivering 500 response?

i'm on phone right now, can't type exact code you. basic idea throw warp thread async exception. may sound complicated, easiest way approach use race function async library. this:

toexitvar <- newemptymvar race warp (takemvar toexitvar) 

and in handler, when want warp exit:

putmvar toexitvar () 

edit day later , i'm @ computer, here's worked example:

#!/usr/bin/env stack -- stack --resolver lts-9.0 script {-# language overloadedstrings #-}  module main  import network.wai import network.wai.handler.warp import network.http.types import control.concurrent.async import control.concurrent.mvar  main :: io () main = todie <- newemptymvar race_ (takemvar todie) $ run 3000 $ \req send ->     if pathinfo req == ["die"]             putmvar todie ()         send $ responselbs status200 [] "goodbye!"     else send $ responselbs status200 [] "still alive!" 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -