Whenever a scheduled task in Heroku with Rails

I need to run a scheduled task in Heroku using sometime. My first cron job in Rails! :-)

It works great locally, but how do you make it work for Heroku?

I tried this: heroku starts whenever --update-crontab store

but...

[fail] Could not write crontab; try running `when 'without parameters to ensure that your schedule file is valid.

I also added a Heroku scheduler to my Heroku app.

This is my config / schedule.rb

RAILS_ROOT = File.dirname(__FILE__) + '/..'
require File.expand_path(File.dirname(__FILE__) + "/environment")


every :day, :at => "11:00am" do
  @appointments = Appointment.where('date between ? and ?', Date.today, Date.today + 2.day)
  @appointments.each do |appointment|

    @emails = []

    @informated_people = InformatedPerson.where(person_id: appointment.person_id)
    @users = User.find(Authorization.where(:person_id => appointment.person_id).pluck(:user_id))
    @person = Person.find(appointment.person_id)

    @emails << @person.email

    @informated_people.each do |informated_person|
        @emails << informated_person.email
    end

    @users.each do |user|
        @emails << user.email
    end

    UserEmail.appointment_reminder_email(@emails.uniq, @person , 'Cita para el día ' + appointment.date.strftime("%d/%m/%Y %H:%M") + ' con el doctor ' + appointment.doctor + ' para la especialidad ' + appointment.specialty + ' en el centro ' + appointment.center + '.' ).deliver

  end
end
+4
source share
1 answer

Heroku (https://devcenter.heroku.com/articles/scheduler)

, cron .

.

def appointments_reminder
  @appointments = Appointment.where('date between ? and ?', Date.today, Date.today + 2.day)
  @appointments.each do |appointment|

  @emails = []

  @informated_people = InformatedPerson.where(person_id: appointment.person_id)
  @users = User.find(Authorization.where(:person_id => appointment.person_id).pluck(:user_id))
  @person = Person.find(appointment.person_id)

  @emails << @person.email

  @informated_people.each do |informated_person|
      @emails << informated_person.email
  end

  @users.each do |user|
      @emails << user.email
  end

  UserEmail.appointment_reminder_email(@emails.uniq, @person , 'Cita para el día ' + appointment.date.strftime("%d/%m/%Y %H:%M") + ' con el doctor ' + appointment.doctor + ' para la especialidad ' + appointment.specialty + ' en el centro ' + appointment.center + '.' ).deliver
end

lib/tasks/scheduler.rake

desc "Heroku scheduler tasks"
task :email_appointments_reminder => :environment do
  puts "Sending out email reminders for appointments."
  Appointment.appointments_reminder
  puts "Emails sent!"
end

, -, . rake email_appointments_reminder, 11:00.

, : heroku run rake email_appointments_reminder

+6

All Articles