gpt4 book ai didi

ruby-on-rails - 每当 Heroku 与 Rails 中的计划任务时

转载 作者:行者123 更新时间:2023-12-02 05:26:25 27 4
gpt4 key购买 nike

我需要在 Heroku 中使用“whenever”运行计划任务。我在 Rails 中的第一个 cron 工作! :-)

它在本地运行良好,但如何使其在 Heroku 中运行?

我尝试过这个:heroku 每当 --update-crontab 存储时运行

但是...

[失败] 无法写入 crontab;尝试运行不带任何选项的“whenever”,以确保您的计划文件有效。

我还在 Heroku 中将 Heroku Scheduler 添加到了我的应用程序中。

这是我的 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

最佳答案

Heroku 为此类任务提供了一个调度程序 ( 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.

我建议您将此代码移至约会模型中的函数中。

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

关于ruby-on-rails - 每当 Heroku 与 Rails 中的计划任务时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24915904/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com