有时 Resque 会“卸载”无明显原因的 Rails 环境
我有一个简单的堆栈,包含 Ruby on Rails 和 resque。我以正常方式对作业进行排队,并且有一组正在执行的工作人员。没什么疯狂的。
我的问题是,如果我让工作人员运行足够长的时间,他们将停止对应用程序模型的可见性,并且每次调用此类方法都会导致 undefined_method
。
这很奇怪,因为它可能会完美工作几天,然后突然开始失败。重新启动工作程序可以解决问题,但通常会在一段时间后再次出现。
我不知道会发生什么,所以任何指示将不胜感激。
I have a simple stack with Ruby on Rails and resque. I enqueue jobs in a normal way and I have a pool of workers performing. Nothing crazy.
My problem is that if I leave the workers running for long enought they will stop to have visibility on the app's models, and every call to such a method will result in an undefined_method
.
This is very weird since it could be working perfectly for days and then suddenly it starts failing. Restarting the worker fixes the problem, but it usually come back after a while.
I have no clue what could be going on, so any pointers would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Resque 工作人员分叉新流程来完成工作。您的模型的类可能未加载到分叉的子进程中。由于类的加载顺序,也可能存在命名空间冲突。
如果您在开发中更改类文件而不重新启动 resque 工作人员,我怀疑它没有正确重新加载您的类。
为了确保您的类在派生前加载,请在 resque 设置任务中引用类。分叉前加载的类将被复制到子进程。下面,我将它们放入一个数组中以强制它们加载。这也更快,因为每个子进程都已经加载了类。如果您使用 AR,您还应该在 after_fork 块中重新建立 ActiveRecord 连接。
lib/tasks/resque.rake:
Resque workers fork new processes to do the work. It's possible that the classes for your models are not loaded in the forked child process. It's also possible that there is a namespace collision because of the order that classes are loaded.
If you are changing class files in development without restarting your resque workers, I suspect it's not reloading your classes correctly.
To make sure that your classes are loaded pre-fork, reference the classes in the resque setup task. The classes that are loaded pre-fork will be copied to the child process. Below, I put them in an array to force them to load. This is also faster, since each child process will already have the classes loaded. Also you should re-establish your ActiveRecord connection in an after_fork block if you're using AR.
lib/tasks/resque.rake: