gpt4 book ai didi

f# - 如果它崩溃,如何重新启动异步 Suave 服务器?

转载 作者:行者123 更新时间:2023-12-03 15:31:43 24 4
gpt4 key购买 nike

我有一个用于移动设备的嵌入式服务器,有时可能会崩溃。我需要始终让服务器保持事件状态。现在的问题是我看不到异步时如何重启服务器:

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..

如何清理所有并重启服务器?

最佳答案

捕获异常,像这样调用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()

关于f# - 如果它崩溃,如何重新启动异步 Suave 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com