简体   繁体   English

如何让用户在Heroku上部署的Rails应用程序中创建计划任务

[英]How to have users create scheduled tasks in rails app deployed on Heroku

I have a rails app deployed on Heroku. 我在Heroku上部署了Rails应用程序。 I want to add a feature that enables users of the app to set a reminder. 我想添加一个功能,使应用程序的用户可以设置提醒。 I need some way for the app to schedule sending an email at the time specified by the user. 我需要某种方法让应用程序安排在用户指定的时间发送电子邮件。

I have found numerous posts referring to using delayed_job for this, but none of the write-ups / tutorials / etc. that I have found directly address what I am trying to accomplish (the descriptions I have found seem more geared towards managing long-running jobs that are to be run "whenever"). 我发现有很多帖子都提到要为此使用delay_job,但是我发现的任何文章/教程/等都没有直接解决我要完成的工作(我发现的描述似乎更适合于管理长期运行的内容“无论何时”运行的作业。

Am I on the right track looking at delayed_job for this? 我在正确的轨道上寻找这个的delay_job吗? If so, can somebody point me towards a tutorial that might help me? 如果是这样,有人可以指出我的指导对我有帮助吗?

If delayed_job is not quite right for the job, does anybody have a suggestion for how I might approach this? 如果delay_job不太适合这份工作,那么有人对我该如何处理提出建议吗?

The most typical way of handling this is to use a cron job. 解决此问题的最典型方法是使用cron作业。 You schedule a job to run every 15 minutes or so and deliver any reminders that come up in that time. 您将作业安排为每15分钟左右运行一次,并提供该时间内出现的任何提醒。 Unfortunately, heroku only allows cron jobs to run every hour, which usually isn't often enough. 不幸的是,heroku只允许cron作业每小时运行一次,这通常是不够的。

In this case, I'd use delayedjob and trick it into setting up a recurring task that delivers the notifications as often as necessary. 在这种情况下,我将使用delayjob并诱使它设置一个定期执行的任务,该任务会根据需要频繁地发送通知。 For example, you could create a function that begins by rescheduling itself to run in 10 minutes and then goes on to send any reminders that popped up in the previous 10 minutes. 例如,您可以创建一个函数,该函数首先将其自身重新安排为在10分钟内运行,然后继续发送在前10分钟内弹出的所有提醒。

To view delayedjobs send_at syntax to schedule future jobs check here: https://github.com/tobi/delayed_job/wiki 要查看delayjobs send_at语法以安排将来的工作,请查看此处: https : //github.com/tobi/delayed_job/wiki

ADDED after comments: To send the reminder, you would need to create a function that searches for pending reminders and sends them. 在注释后添加:要发送提醒,您需要创建一个函数来搜索待处理的提醒并将其发送。 For example, let's say you have a model called Reminder (rails 3 syntax cause I like it better): 例如,假设您有一个称为提醒的模型(Rails 3语法使我更喜欢它):

def self.find_and_send_reminders
 reminders = Reminder.where("send_at < ? AND sent = ?", Time.now, false).all
 reminders.each do |r|
   #the following delayed_job syntax is apparently new, and I haven't tried it.  came from the collective_idea fork of delayed_job on github
   Notifier.delay.deliver_reminder_email(r)
   #I'm not checking to make sure that anything actually sent successfully here, just assuming they did.  may want to address this better in your real app
   r.update_attributes!(:sent => true)
 end
 #again using the new syntax, untested.  heroku may require the old "send_at" and "send_later" syntax
 Reminder.delay(:run_at => 15.minutes.from_now).find_and_send_reminders
end

This syntax assumes you decided to use the single reminder entry for every occurence method. 该语法假定您决定对每种发生方式都使用单个提醒条目。 If you decide to use a single entry for all recurring reminders, you could create a field like "last_sent" instead of a boolean for "sent" and use that. 如果您决定对所有重复提醒使用单个条目,则可以创建一个字段,例如“ last_sent”,而不是“ sent”的布尔值,然后使用该字段。 Keep in mind these are all just ideas, I haven't actually taken the time to implement anything like this yet so I probably haven't considered all the options/problems. 请记住,这些全都是想法,我实际上还没有花时间去实现类似的东西,所以我可能没有考虑所有的选择/问题。

查看欠缺的宝石,可能对您有用: http : //runt.rubyforge.org/

You can use delayed_job's run_at to schedule at a specific time instead of whenever. 您可以使用delay_job的run_at在特定时间而不是任何时间进行计划。

If your application allows the users to change the time of the reminders you need to remember the delayed_job to be able to update it or delete it when required. 如果您的应用程序允许用户更改提醒时间,则需要记住delay_job以便能够对其进行更新或在需要时将其删除。

Here is more details. 是更多细节。

It's good to avoid polling if you can. 如果可以的话,最好避免轮询。 The worker thread will poll at the database level, you don't want to add polling on top of polling. 工作线程将在数据库级别进行轮询,您不想在轮询之上添加轮询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails app中缺少模板/部署到Heroku时创建POST操作 - Missing Template in Rails app on /create POST action when deployed to Heroku 部署到 Heroku 的 Rails 应用程序出现 NoMethodError(在模型上调用.create 时) - NoMethodError with Rails app deployed to Heroku (When calling .create on model) 将Rails应用程序部署到Heroku无法正常工作 - Deployed rails app to Heroku not working 可以在不使用Heroku Toolbelt的情况下部署Rails应用程序吗? 如果是这样,怎么样? - Can a Rails app be deployed without using Heroku Toolbelt? If so, how? 如何删除部署在heroku上的Rails应用程序中的缓存页面 - how to delete cached page in rails app deployed on heroku 如何使用git复制rails app并部署到heroku - How to copy rails app using git and deployed to heroku 如何让rufus-scheduler使用部署到Heroku的Rails应用程序? - How to get rufus-scheduler working with a Rails app deployed to Heroku? 如何防止滥用抓取工具抓取部署在Heroku上的rails应用程序? - How to prevent abusive crawlers from crawling a rails app deployed on Heroku? 如何将部署的 rails 应用程序连接到现有的 Heroku 数据库 - How connect deployed rails app to exist Heroku database 在Heroku上使用config.time_zone的Rails,Rake计划的任务和DateTime - Rails, Rake scheduled tasks and DateTime with config.time_zone on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM