ActiveRecord :: StatementInvalid在进程收到SIGTERM时有效吗? [英] ActiveRecord::StatementInvalid when process receives SIGTERM?

查看:95
本文介绍了ActiveRecord :: StatementInvalid在进程收到SIGTERM时有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,我有一个脚本来更新数据库中的某些记录.当我发送SIGTERM杀死脚本时,在ActiveRecord执行查询时,它偶尔会收到该信号.这导致引发ActiveRecord :: StatementInvalid异常.

In my Rails app, I have a script that updates some records in the database. When I send a SIGTERM to kill the script, it occasionally receives that signal while ActiveRecord is executing a query. This leads to an ActiveRecord::StatementInvalid exception being raised.

我想捕获当它们是SIGTERM的结果时发生的StatementInvalid异常,并退出脚本.我该如何判断StatementInvalid是由于信号而不是出于其他原因而发生的?

I'd like to catch StatementInvalid exceptions that occur when they're they're the result of a SIGTERM and exit the script. How can I tell that a StatementInvalid is occurring because of a signal and not for some other reason?

推荐答案

如果您捕获TERM信号,我相信您会避免该异常.您可以在脚本开始时执行此操作(或在任何情况下都可以执行此操作,但只需要执行一次).

If you trap the TERM signal, I believe you will avoid the exception. You can do this at the beginning of your script (or really anywhere for that matter, but you only need to do it once).

 Signal.trap("TERM") do
   Kernel.exit!
 end

出现StatementInvalid错误的原因是Ruby通过在当前执行位置引发SIGTERM异常来处理信号. ActiveRecord正在捕获异常并将其重新抛出为StatementInvalid.通过设置Signal处理程序,Ruby将执行您的处理程序,而不引发异常.

The reason you get the StatementInvalid error is Ruby handles the signal by raising a SIGTERM exception at the place of current execution. ActiveRecord is catching the exception and rethrowing it as StatementInvalid. By setting a Signal handler, Ruby will execute your handler instead of raising an exception.

有关更多信息,请参见 Ruby Signal文档.

See the Ruby Signal documentation for more information.

这篇关于ActiveRecord :: StatementInvalid在进程收到SIGTERM时有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆