英文:
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 error
s it's the task of your application to handle them correctly and restart the appropriate parts. It get's harder in case of panic
s. 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论