Getting delayed job to log

badnaam picture badnaam · Aug 17, 2010 · Viewed 20.8k times · Source
#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!

Answer

Seamus Abshere picture Seamus Abshere · Jan 10, 2011

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.