View Resque Log Report in Rails Server Logs
I have a Rails 4 application on a Puma server where the Resque / Resque-Scheduler is running background jobs. I would like to know how to merge the log output of my two Resque workers into the log of my server, or, which is not possible, how can I view the log output of my Resque workers. Currently, I have not been able to figure out how to view the log output for workers, so I have no idea what is going on under the hood. I found this blogpost which suggests adding the following files 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
It didn't work. I also tried the suggestion in the comments, which was supposed to be replaced Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
by Resque.logger = ActiveSupport::Logger.new(logfile)
, however that didn't work either. With the second option, I still get the error NoMethodError: undefined method 'logger=' for Resque:Module
when I try to load the 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 went through the Resque docs on registration , but I'm not sure how to use what's in there, as I admittedly don't know much about registering in Rails. I have had no luck finding other helpful resources on this subject.
source to share
How I fix this, it is not perfect, but it just works.
my environment: rails 5.0.1, resque: 1.26.0
the first time, I install Resque.logger
and Resque.logger.level
in config/initializers/resque.rb
as most docs suggest:
# config/initializers/resque.rb
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::DEBUG
then in the task I go to the log Resque.logger.info
:
# update_xxx_job.rb
class UpdateXxxJob
def self.perform
Resque.logger.info 'Job starts'
...
end
end
it doesn't work, i can't see anything in log/resque.log
.
then someone said that they should always set log sync, no buffer, so I update config/initializers/resque.rb
according to fooobar.com/questions/1592605 / ... :
# config/initializers/resque.rb
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')
logfile.sync = true
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO
still not working.
I also tried config resque logger in lib/tasks/resque.rake
:
# lib/tasks/resque.rake
require 'resque'
require 'resque/tasks'
require 'resque/scheduler/tasks'
require 'resque-scheduler'
require 'resque/scheduler/server'
namespace :resque do
task setup: :environment do
Resque.schedule = YAML.load_file(Rails.root + "config/resque_scheduler_#{Rails.env}.yml")
Resque.redis.namespace = "xxx_#{Rails.env}"
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::INFO
end
end
does not work.
Finally, I decided to move the logger configuration from the initializer to the job, so the work now looks like this:
# update_xxx_job.rb
class UpdateXxxJob
def self.perform
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::DEBUG
Resque.logger.info 'Job starts'
...
end
end
then I can get what I want in log/resque.log
.
you may try.
source to share