iexiak
iexiak

Reputation: 311

Rails - Rufus cron event A runs daily but event B does not run

require_relative '../../bin/generate_survey'
require_relative '../../bin/generate_stats_emails'
email_config = YAML.load_file("#{Rails.root}/config/email_config.yml")

def rufus_logger
  @@rufus_logger ||= Logger.new("#{Rails.root}/log/rufus.log")
end

survey_scheduler = Rufus::Scheduler.new
rufus_logger.info('Initializing Survey Rufus Scheduler')

#Job A
survey_scheduler.cron '00 08 * * 1-5' do
  conference_today = read_ics_make_quiz(Date.today)
  rufus_logger.info("Survey scheduling for #{Date.today} returns #{conference_today}")
end

#Job B
survey_scheduler.cron '00 23 * * 1-5' do
  rufus_logger.info("Stats Emails starting")
  survey_settings = Survey.where ['unlock_at BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day] unless @survey_settings[0]
  if survey_settings.present?
    get_latest_stats(email_config)
    UserMailer.presenter_stats_email(email_config[:action_mailer][:education_email_group]).deliver_now
    UserMailer.resident_stats_email(email_config[:action_mailer][:resident_email_group]).deliver_now
    rufus_logger.info("Stats Emails sent")
  else
    rufus_logger.info("Stats Emails not sent")
  end
end

Details: server: Ubuntu 12.04 Apache/mysql/Passenger Ruby 2.1.5 Rails 4.2.0

Job A runs perfectly well, every time. Job B never executes. It never leaves anything in the log.

Rufus.log has: Initializing Survey Rufus Scheduler followed by daily lines of: Survey scheduling for #{Date.today} returns #{conference_today}

It never reaches Job B comments

Any help is appreciated

Upvotes: 0

Views: 170

Answers (2)

jmettraux
jmettraux

Reputation: 3551

Your "split in two files" move is interesting, but you'd better qualify it as "split in two files and two scheduler instances". I'm happy you got it working somehow.

I wonder if the job got scheduled or not in you first "single file" version. Something like

p survey_scheduler.jobs.length

would have told us. But I think it would have told 2 and the problem lies elsewhere, which brings me to the second part of my "answer".

Thanks for adding a sorely missing piece of information by specifying that you are using Passenger.

Passenger vs rufus-scheduler is a classic. The usual symptom is "it doesn't schedule" or "it doesn't schedule after a while". I suspect that you're victim to a variant of this. Can't tell until you report very carefully what's happening.

I urge you to read the 3 last link in the rufus-scheduler README FAQ, they hold information (and solutions) about these problems. A peek at the manual for the Passenger version you're using would be good to. Know your tools.

The last link is more about Unicorn, but it hints at solutions for Passenger too.

Upvotes: 1

iexiak
iexiak

Reputation: 311

I split them in to 2 files and they work now.

scheduled_jobs.rb

require 'rufus-scheduler'
require_relative '../../bin/generate_survey'

def rufus_logger
  @@rufus_logger ||= Logger.new("#{Rails.root}/log/rufus.log")
end

survey_scheduler = Rufus::Scheduler.new
rufus_logger.info('Initializing Survey Rufus Scheduler')

survey_scheduler.cron '00 08 * * 1-5' do
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
  conference_today = read_ics_make_quiz(Date.today)
  rufus_logger.info("Survey scheduling for #{Date.today} returns #{conference_today}")
end

scheduled_email.rb

require 'rufus-scheduler'
require_relative '../../bin/generate_stats_emails'
email_config = YAML.load_file("#{Rails.root}/config/email_config.yml")

def rufus_logger
  @@rufus_logger ||= Logger.new("#{Rails.root}/log/rufus.log")
end

email_scheduler = Rufus::Scheduler.new
rufus_logger.info('Initializing Email Rufus Scheduler')

email_scheduler.cron '00 23 * * 1-5' do
  rufus_logger.info("Stats Emails starting")
  get_latest_stats(email_config)
  UserMailer.presenter_stats_email(email_config[:action_mailer][:education_email_group]).deliver_now
  UserMailer.resident_stats_email(email_config[:action_mailer][:resident_email_group]).deliver_now
  rufus_logger.info("Stats Emails sent")
end

Upvotes: 0

Related Questions