Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful exit with an API call, or better ? #736

Open
flotrek opened this issue Sep 26, 2019 · 3 comments
Open

Graceful exit with an API call, or better ? #736

flotrek opened this issue Sep 26, 2019 · 3 comments

Comments

@flotrek
Copy link

flotrek commented Sep 26, 2019

I'm trying to gracefully exit the webServer with a GET or POST to the the webServer itself.

Is there a way to execute code after a HTTP response has been given to the client to help me do that properly ?

Is there a nicer way to handle shutting down suave server from a CD pipeline ?

let app = 
    choose [
        path "/gracefulExit" >=> 
            request(fun r -> 
                fun () ->
                    Threading.Thread.Sleep(1000)
                    Log.Information("graceful exit...")
                    cancellationTokenSource.Cancel()                                         
                |> doAsyncTask |> Async.Start 
                OK "Initiating graceful exit"
                                       )]
@ademar
Copy link
Member

ademar commented Sep 26, 2019

Not perfect but this may work.

open Suave.Sockets

let flush (ctx:HttpContext) =
  async{
    match! AsyncSocket.flush(ctx.connection) with
    | Choice1Of2 cn ->
      return Some { ctx with connection = cn }
    | _ -> return Some ctx //don't care we are shutting down anyways
  }

let shutdown (cts:CancellationTokenSource) (ctx:HttpContext) =
  async{
    do cts.Cancel()
    return Some ctx }

let test (cts:CancellationTokenSource) : WebPart = 
  path "/gracefulExit" >=> Successful.OK "Shutdown inititated" >=> flush >=> shutdown cts

It wouldn't be so graceful though, other requests being processed will be interrupted. Currently there is no built-in way of doing a truly graceful shutdown.

@flotrek
Copy link
Author

flotrek commented Sep 26, 2019

The exit is graceful enough (with or without the flush by the way), but client still receives HTTP 503 instead of 200.

@ademar
Copy link
Member

ademar commented Sep 26, 2019

Ah I see, it actually doesn't send any data down the pipe (at least here). I'll have to play more with it to see if it is possible,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants