Как перезапустить асинхронный сервер Suave, если он падает? - PullRequest
0 голосов
/ 18 декабря 2018

У меня есть встроенный сервер для мобильного устройства, который может иногда зависать.Я должен всегда иметь живой сервер.Теперь проблема в том, что я не вижу, как перезагрузить сервер, когда он асинхронный:

let startServer(rootPath) =
    let cf = serverConfig rootPath
    printfn "%A" cf
    startWebServerAsync cf app
    |> snd
    |> Async.StartAsTask 

type App() =
    inherit Application()

    let mutable task:System.Threading.Tasks.Task = null

    do
        let t = startServer(...)
        task <- t //The task is hold here to avoid it being GC..

Как очистить все и перезагрузить сервер?

1 Ответ

0 голосов
/ 19 февраля 2019

перехватить исключение, вызвать метод start следующим образом:

// server sample stub
let startServer(_rootPath):Async<_>=
    async {
        printfn "server started"
        do! Async.Sleep 500
        return "blah"
    }
// stubs for example code to compile
type Application() = class end
type App() =
    inherit Application()

    let mutable task:System.Threading.Tasks.Task= null
    let rec startRestartable() = 
        // catch the async to avoid GC
        task <- 
                async{
                    let! serverResult = startServer(".") |> Async.Catch
                    match serverResult with
                    | Choice1Of2 x ->
                        // no exception? whatever you would do if the server terminates normally
                        // I'll assume you want a restart
                        // all paths lead to restart, do nothing here
                        printfn "Server finished? why? %A" x
                        ()
                    | Choice2Of2 ex ->
                        // log exception, poor logging example stub
                        eprintfn "server crash: %A" ex
                    startRestartable()
                }
                |> Async.StartAsTask 




    do
        printfn "Starting"
        startRestartable()
...