简体   繁体   English

如何在Heroku实例上运行循环进程(Rails 4)?

[英]How can I run a loop process on a Heroku instance (Rails 4)?

I have a Heroku Rails 4 app where I want something to run continuously. 我有一个Heroku Rails 4应用程序,我想让它连续运行。 It is processing data from Amazon SQS. 它正在处理来自Amazon SQS的数据。

Right now, I use clock.rb to continuously add a delayed job to run a worker. 现在,我使用clock.rb连续添加延迟的作业来运行工作程序。 The worker is set to run for 10m, then finishes. 工人准备奔跑10m,然后完成。 Since the clock.rb is running every 30 seconds, it should restart a new process pretty quickly. 由于clock.rb每30秒运行一次,它应该很快重新启动一个新进程。

My Procfile is: 我的Procfile是:

myworker: QUEUE=myworker bundle exec rake jobs:work

I'm wondering if there is a way to change the Procfile perhaps, so that I could say "Every instance of myworker should run this process while it is alive. If it dies for whatever reason, restart it immediately and start processing again" 我想知道是否有一种方法可以更改Procfile,以便我可以说“ myworker的每个实例都应在运行时运行此过程。如果它因某种原因而死,请立即重新启动并再次开始处理”

Is there a good way to do that with Rails 4 and Heroku? 使用Rails 4和Heroku可以做到这一点吗?

There must be... I don't really know what this is called but surely there's a name for this. 一定有……我真的不知道这叫什么,但是肯定有一个名字。

Also, once I have it running, would there be a good way to say "close the process every 10m, and restart, just to clear your memory/restart fresh"? 另外,一旦我运行它,是否会有一个好方法说“每隔10m关闭该过程,然后重新启动,只是为了清除内存/重新开始”?

You don't have to do it with Delayed Job or anything like that; 您不必使用“延迟工作”之类的方法进行处理; you just have to create a custom Rake task. 您只需要创建一个自定义Rake任务。

For instance, at my work we have this line in our Procfile: 例如,在我的工作中,我们的Procfile中有这一行:

jobs_worker: bundle exec rake poll_jobs

We added a file_jobs.rake file in lib/tasks , which does this: 我们在lib/tasks添加了一个file_jobs.rake文件,该文件执行以下操作:

task poll_jobs: :environment do while true puts "polling jobs" FileJob.poll_status! sleep 10 end end

We put in the extra sleep 10 so that a replacement task doesn't spin up right away. 我们增加了额外的睡眠10,以使替换任务不会立即启动。 But you can take that out. 但是你可以把它拿出来。 The moment your task completes, it will automatically be restarted again. 任务完成后,它将自动重新启动。 Keep in mind that if you keep restarting too often, Heroku might get mad at you and not spin your task right up again. 请记住,如果您经常重启,Heroku可能会生您的气,而不会重新启动您的任务。

You could just as easily put a timer in there, or a 'number of times you make it around the loop' - and exit when the timer expires, or the number of times you've been through the loop is greater than 'x'. 您可以轻松地在其中放置一个计时器,或者“在循环中放置它的次数”-在计时器到期时退出,或者您经过循环的次数大于“ x”,然后退出。

Does that make sense? 那有意义吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM