Rufus scheduler not logging in production

2019-05-25 06:08发布

问题:

My rails app kicks off a process with rufus-scheduler in an initializer. Here's a stripped-down version of the initializer's code:

# config.logger isn't available here, so we have to grab it from the Rails object
logger = RAILS_DEFAULT_LOGGER

logger.warn(Time.now.to_s + ": Starting Rufus Scheduler")

# run every Wednesday at 10 AM 
cron_string = '0 10 * * 3'

scheduler = Rufus::Scheduler.start_new
scheduler.cron cron_string do
  logger.warn(Time.now.to_s + ": Starting Background Process")
  (do work here)
  logger.warn(Time.now.to_s + ": Finished Background Process")
end

logger.warn(Time.now.to_s + ": Rufus Scheduler set Background Process to run with the following cron string: [#{cron_string}]")

In all environments, the code runs like a champ. The populate process does its thing and finishes gracefully. The problem, however, is with logging. When RAILS_ENV is set to "production", the messages inside the cron block don't log at all.

I'm using Passenger 2.2.9 and Rails 2.3.5. I figure one of these two things is preventing the process from logging. Can anyone tell me which it is and how to get it to log in production?

回答1:

OK, found the problem, thanks to this article: http://earthcode.com/blog/2009/05/rails_script_runner_logging_cron.html

Turns out the logger will not auto-flush in production. So, I just added

logger.flush 

to the end of the process and BANG everything worked.