Delayed_Job Not Logging

delayed_job not logging

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.

Logging in delayed_job?

When I needed log output from Delayed Jobs, I found this question to be fairly helpful.

In config/initializers/delayed_job.rb I add the line:

Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'dj.log'))

Then, in my job, I output to this log with:

Delayed::Worker.logger.info("Log Entry")

This results in the file log/dj.log being written to. This works in development, staging, and production environments.

Delayed Job not logging in Production

DelayedJob maintains it's own logger so you'll need to point that to your specific log file. So, in your initializer file (either environment.rb or, better, a specific delayed_job.rb file in config/initializers) add the following:

Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'dj.log'))

Delayed_job is no longer logging

I finally got this to work. All thanks to Seamus Abshere's answer to the question here. I put what he posted below in an initializer file. This got delayed_job to log to my development.rb file (huzzah!).

However, delayed_job still isn't logging into my console (for reasons I still don't understand). I solved that by opening a new console tab and entering tail -f logs/development.log.

Different from what Seamus wrote, though, auto-flushing=true is deprecated in Rails 4 and my Heroku app crashed. I resolved this by removing it from my initializer file and placing it in my environments/development.rb file as config.autoflush_log = true. However, I found that neither of the two types of flushing were necessary to make this work.

Here is his code (without the auto-flushing):

file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing
file_handle.sync = 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.



Related Topics



Leave a reply



Submit