Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delayed_job not logging

I cannot log messages from my delayed_job process. Here is the job that is being run.

class MyJob
  def initialize(blahblah)
    @blahblah = blahblah
    @logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
  end
  def perform
    @logger.add Logger::INFO, "logging from delayed_job"
    #do stuff
  end
end

I've tried various logging levels, and I have config.log_level = :debug in my environment configuration. I run delayed_job from monit. I'm using delayed_job 3.0.1 with ruby 1.9.3 and rails 3.0.10.

like image 612
freedrull Avatar asked Feb 29 '12 22:02

freedrull


3 Answers

An explation could be that the job gets initialized only once on producer side. Then it gets serialized, delivered through the queue (database for example) and unserialized in the worker. But the initialize method is not being called in the worker process again. Only the perform method is called via send.

However you can reuse the workers logger to write to the log file:

class MyJob
  def perform
    say "performing like hell"
  end

  def say(text)
    Delayed::Worker.logger.add(Logger::INFO, text)
  end
end

Don't forget to restart the workers.

like image 66
iltempo Avatar answered Oct 20 '22 17:10

iltempo


In RAILS_ROOT/config/initializers have a file called delayed_job_config.rb with these lines:

Delayed::Worker.logger = Rails.logger
Delayed::Worker.logger.auto_flushing = true

Remember to re-start your workers after doing this.

Let me know if this helps

like image 25
raghus Avatar answered Oct 20 '22 17:10

raghus


I don't see why you would set the logger in the job. When I've done this I set the worker to to use a specific file on start e.g. Logger.new("log/worker_#{worker_number}") which ensures that each worker outputs to it's own file and you don't have to worry about multiple workers writing to the same file at the same time (messy).

Also, in plain ol' ruby you can call @logger.info "logging from delayed_job".

Finally, i'm pretty sure that 'perform' is called directly by your worker and instantiated, so you can refactor to:

class MyJob
 def perform(blahblah)
  @logger.add Logger::INFO, "logging from delayed_job"
  @blahblah = blahblah
  #do stuff
 end
end
like image 40
TomDunning Avatar answered Oct 20 '22 15:10

TomDunning