Question

I have a question about delayed_job in Rails that doesn't seem to be mentioned much.

When you run a delayed job, it doesn't seem to load anything from ApplicationController. We have some code in ApplicationController to use a custom logger:

def setup_logger
  logfile = File.open("#{RAILS_ROOT}/log/audit.log", 'a')
  @audit_log = Logger.new(logfile)
  $audit_log = @audit_log
end

We then reference $audit_log all through our code. But because DelayedJob doesn't load the ApplicationController this variable is nil and we get errors.

So Delayed_job is just running the specified method blindly, which could also be dangerous if you rely on before_filters for checking data or validating things.

How can we fix our problem of getting DelayedJob to know about our global logging variable? We don't want to explicitly define the logger all through our code. How else are people dealing with this problem, as it seems like it should be common, but its not talked about much.

Thanks

Was it helpful?

Solution

Why you think, a Job must run ApplicationController ?

A Job, precisely the worker running that job, loads the environment, sure, but not a controller...

If you don't want have it in a initializer, why not using a kind of config object for storing such data and reference to it in the job ?

And instead of do it in each job individually, I would suggest set up the logger in the BaseJob, and use it in the inherited jobs for what you need.

And another suggestion, please if you can, don't use delayed job, please use http://mperham.github.io/sidekiq/ or at least https://github.com/resque/resque

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top