delayed_job not logging

2019-01-25 12:47发布

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.

4条回答
Bombasti
2楼-- · 2019-01-25 13:37

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
查看更多
干净又极端
3楼-- · 2019-01-25 13:42

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.

查看更多
Juvenile、少年°
4楼-- · 2019-01-25 13:42

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

查看更多
Anthone
5楼-- · 2019-01-25 13:53

This is working just fine for me in Rails 3.2:

class FiveMinuteAggregateJob < Struct.new(:link, :timestamp)
  def perform
    Rails.logger.info 'yup.'
  end
end
查看更多
登录 后发表回答