I've got a Rails 4 app on a Puma server with Resque/Resque-Scheduler running background jobs. What I'd like to know is how I merge the log output of my two Resque workers into my server log, or, of that is not possible, how I can view the log output of my Resque workers. Currently I have not been able to figure out how to view the log output for the workers, so I have no idea what's happening under the hood. I found this blogpost, which suggests adding the following likes to my resque.rake
file:
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
That didn't work. I also tried the suggestion in the comments, which was to replace Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
with Resque.logger = ActiveSupport::Logger.new(logfile)
, however that didn't work either. With the second option, I still get a NoMethodError: undefined method 'logger=' for Resque:Module
error when I try to boot up a worker.
Here is my current resque.rake
file:
require 'resque/tasks'
require 'resque_scheduler/tasks'
namespace :resque do
puts "Loading Rails environment for Resque"
task :setup => :environment do
require 'resque'
require 'resque_scheduler'
require 'resque/scheduler'
require 'postman'
end
end
I've looked at the Resque docs on logging, but am not sure how to use what's there as I admittedly don't know very much about logging in Rails. I haven't had any luck finding other useful resources on the subject.
I've had the same problem while setting up mine. Here's what I did:
It seems
before_fork
accepts a block as an argument rather than assigning a block to it.How I fix it, it is not perfect but just works.
my environment: rails 5.0.1, resque: 1.26.0
at the first time, I set the
Resque.logger
andResque.logger.level
inconfig/initializers/resque.rb
as most docs suggest:then in the job, I output log by
Resque.logger.info
:it doesn't work, I can see nothing in
log/resque.log
.then someone said should set the logfile sync always, no buffer, so I update the
config/initializers/resque.rb
according a question from stackoverflow:still doesn't work.
I also tried config resque logger in
lib/tasks/resque.rake
:doesn't work.
finally, I decide to move the logger configuration from initializer to the job, so the job now looks like:
then I can get what I want in the
log/resque.log
.you can try it.