Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting delayed job to log

Tags:

#Here is how I have delayed job set up.  Delayed::Worker.backend = :active_record #Delayed::Worker.logger = Rails.logger Delayed::Worker.logger = ActiveSupport::BufferedLogger.new("log/ ##{Rails.env}_delayed_jobs.log", Rails.logger.level) Delayed::Worker.logger.auto_flushing = 1 class Delayed::Job     def logger         Delayed::Worker.logger     end end if JobsCommon::check_job_exists("PeriodicJob").blank?     Delayed::Job.enqueue PeriodicJob.new(), 0, 30.seconds.from_now end #end   #Here is my simple job.  class PeriodicJob     def perform         Rails.logger.info "Periodic job writing #{Time.now}"             Delayed::Job.enqueue PeriodicJob.new(), 0, 30.seconds.from_now     end end 

I don't see any log messages from delayed job in my rails logs or delayed job log file, the only messages I see are jobs starting/success/failure in the delayed_jobs.log file.

this is causing big problems, including detecting bugs and memory leaks in workers almost impossible! Please help!

like image 753
badnaam Avatar asked Aug 17 '10 07:08

badnaam


People also ask

How does a delayed job work?

Delayed::Job works by serializing a Ruby object as YAML and storing it in a database table so that a worker in a different process can retrieve, deserialize, and perform the requested work. Some operations that we use Delayed::Job for are longer-running data processing, sending emails, and updating search indicies.

How do I check if my job is delayed?

The most simple way to check whether delayed_job is running or not, is to check at locked_by field. This field will contain the worker or process locking/processing the job. Running Delayed::Job. where('locked_by is not null') will give you some results, if there are jobs running.


1 Answers

We've gotten it to work on Rails 3/Delayed Job 2.0.3 by hacking Rails.logger itself to use a different log file (the one we want for delayed_job entries) and also setting the delayed job logger to use the exact same object:

file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT)) # Be paranoid about syncing, part #1 file_handle.sync = true # Be paranoid about syncing, part #2 Rails.logger.auto_flushing = true # Hack the existing Rails.logger object to use our new file handle Rails.logger.instance_variable_set :@log, file_handle # Calls to Rails.logger go to the same object as Delayed::Worker.logger Delayed::Worker.logger = Rails.logger 

If the above code doesn't work, try replacing Rails.logger with RAILS_DEFAULT_LOGGER.

like image 112
Seamus Abshere Avatar answered Sep 20 '22 02:09

Seamus Abshere