2

Ruby on Rails とresqueを使用した単純なスタックがあります。私は通常の方法でジョブをキューに入れ、実行中のワーカーのプールを持っています。クレイジーなことは何もありません。

私の問題は、ワーカーを長時間実行したままにしておくと、アプリのモデルを表示できなくなり、そのようなメソッドを呼び出すたびにundefined_method.

数日間は完全に機能していたのに、突然機能しなくなる可能性があるため、これは非常に奇妙です。ワーカーを再起動すると問題は解決しますが、通常はしばらくすると元に戻ります。

何が起こっているのかわからないので、どんな指針でも大歓迎です。

4

1 に答える 1

2

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:

namespace :resque do
  task :setup => :environment do
    [User, Monkey, Banana] # force these classes to load

    Resque.after_fork { ActiveRecord::Base.establish_connection }
  end
end
于 2011-12-19T17:55:29.177 に答える