Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whenever Scheduled Task in Heroku with Rails

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

It works fine locally, but how do you make it work in Heroku?

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

but...

[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid.

I also added Heroku Scheduler to my app in Heroku.

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
like image 810
user3402629 Avatar asked Mar 18 '23 23:03

user3402629


1 Answers

Heroku provides a Scheduler for these kinds of tasks (https://devcenter.heroku.com/articles/scheduler)

Scheduler is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment.

I would suggest that you move this code to a function within your appointments model.

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

Next, you need to create the lib/tasks/scheduler.rake file

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

Lastly, using the web-interface you can schedule the it using the task name. In this example it is rake email_appointments_reminder, and you can pick the frequency from the drop-down options to run it daily at 11:00am.

I would also suggest to test the task manually before you schedule it using this console command: heroku run rake email_appointments_reminder

like image 101
AytanLeibowitz Avatar answered Mar 25 '23 07:03

AytanLeibowitz