The following description and code example was tested with the following gem spec:
rails (3.2.11) resque (1.24.1) resque-scheduler (2.0.1)
Since the default Resque.logger does not immediately flush all log messages to the log file, we want to use our own logger instance and a separate log file. All we have to do are the following steps:
- If you do not have a
resque.rakerake file then go ahead and create one in yourlib/tasksfolder. - To initialize a new logger we will use the Resque.before_fork hook inside the
setuptask. - Open a new log file in your preferred location and activate the synchronization flag to avoid block buffering.
- Create a new instance of
ActiveResource::BufferedLoggerand set it as the defaultResque.logger. - Set the appropriate log level for your needs.
- That’s it!
The complete code will be show below.
require "resque/tasks"
require 'resque_scheduler/tasks'
task "resque:setup" => :environment do
Resque.before_fork = Proc.new {
ActiveRecord::Base.establish_connection
# Open the new separate log file
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')
# Activate file synchronization
logfile.sync = true
# Create a new buffered logger
Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
Resque.logger.level = Logger::INFO
Resque.logger.info "Resque Logger Initialized!"
}
end
Leave a Reply