当出现错误时重新启动Go应用程序。

huangapple go评论87阅读模式
英文:

Restart the go application when the error

问题

我有一个全天运行的Go应用程序。有没有人有一个关于查看主应用程序是否正在运行的独立应用程序的想法?如果主应用程序有错误,附加应用程序应该关闭主应用程序并重新运行主应用程序。或者也许有一种类似析构函数的东西,可以成为主应用程序的一部分?

英文:

I have an go application running 24 hours a day. Does anyone have an idea for a separate application that looks up whether the main application is running? If the main application has a bug, an additional application should close the main application and re-run the main application. Or maybe there is something in the style of the destructor, which could be the main application?

答案1

得分: 1

在返回error的情况下,你的应用程序需要正确处理并重新启动适当的部分。在出现panic的情况下,情况会变得更加困难。在Go语言中,提供了recover函数,类似于异常处理中的catch

在https://github.com/tideland/goas中,我提供了一个名为loop的包,用于以受控的方式运行goroutine。除了传统的方法可以停止goroutine并在其死亡时检索错误值之外,你还可以使用GoRecoverable来启动一个goroutine。它提供了一种在发生panic时调用的函数,并且还了解计数和频率。因此,它可以根据需要采取行动或决定goroutine是否应该继续工作(例如,通过重置/重新初始化受故障影响的代码部分)。

英文:

In case of returned errors it's the task of your application to handle them correctly and restart the appropriate parts. It get's harder in case of panics. Here Go provides recover. It's like a catch of exceptions.

In https://github.com/tideland/goas I provide loop, a package to run goroutines in a controlled way. Beside a traditional approach with the ability to stop a goroutine and/or retrieve an error value in case it died you can also start a goroutine with GoRecoverable. It provides a way to pass a function that's called in case of a panic and also knows about count and frequency. So it can act or decide if a goroutine shall continue work (e.g. by resetting/re-initializing those parts of your code that are covered by the failure).

huangapple
  • 本文由 发表于 2014年12月17日 18:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/27523077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定