Hello,
for a Resque-job like this:
class TestWorker
@queue = :test_worker_queue
@fatal_exceptions = [TestException]
def self.perform(message)
puts message
end
end
I get the following exception when enqueuing a job:
*** (Job{test_worker_queue} | TestWorker | [1]) failed: #<NoMethodError: undefined method `perform' for TestWorker:Class>
It works with the latest 1.x.x version, but not with 2.0.0. Apparently what happens is, that the Excpetion class TestException
can't be found and therefore the job class is not loaded correctly. TestException
is defined in another (model) file of the Rails app.
I know that the condition for eager_loading changed in 2.0 and indeed this seems to be related to that.
So here's the weird part: This is an issue even if config.eager_load
is set to true! It never executes this branch, no matter what I set in the config: https://github.com/resque/resque/blob/adb633a0f6b98b1eb5a5a85bb36ebac9309978fd/lib/resque/tasks.rb#L44
I tested this by putting some logs in the preload rake-task. No matter if I set eager_load
to true or false, the eager-loading lines are not executed. I printed the value of eager_load
in the task and it is always false.
If I remove the condition, or change it e.g. to the old condition, it works again.
_Other changes to my config are picked up by the rake task though_.
Do you have an idea what could be causing this behaviour?
I can also provide a minimal example repository if necessary.
As you've discovered, config.eager_load
is always false
in a rake
environment.
This condition (introduced in 2.0.0) will never be true (until Rails changes how they set this flag):
https://github.com/resque/resque/blob/master/lib/resque/tasks.rb#L43
I think resque needs to supply another way for its users to enable eager loading, and if it could default to true
it would make the upgrade migration smoother because that was the behavior in v1.
In the meantime, one option for working around this would be to run the eager load code yourself in your resque:setup
task, for example:
# lib/tasks/resque.rake
namespace :resque do
task :setup => :environment do
# force eager loading
ActiveSupport.run_load_hooks(:before_eager_load, Rails.application)
Rails.application.config.eager_load_namespaces.each(&:eager_load!)
end
end
Here's a blog post I found with some additional context:
https://www.codegram.com/blog/rake-ignores-eager-loading-rails-config/
I see now, that is unfortunate :-/ Thank you for the information and workaround! There's also a PR in the Rails repo to add an option that controls eager-loading behavior for rake tasks: https://github.com/rails/rails/pull/28209
So this is a restriction of the Rails environment, but upgrading to v2.0 of this gem breaks jobs that where previously running fine (with the forced eager_load). Should this behavior be documented somewhere at least? With the proposed workaround for example?
Wanted to let folks know that an option for using eager loading in Rake tasks was recently merged to Rails with rails/rails#28209
Most helpful comment
Wanted to let folks know that an option for using eager loading in Rake tasks was recently merged to Rails with rails/rails#28209