在Heroku上运行连续运行的工作进程 [英] Running a continuous running worker process on Heroku

查看:125
本文介绍了在Heroku上运行连续运行的工作进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Heroku上运行连续工作进程的最佳方式是什么?我的应用程序需要这个过程全天候运行,所以delayed_job或其他队列看起来像是过度杀伤。我可以通过Heroku控制台手动触发这个过程,并且我可以将它分开运行。

最好,我只想将这个单一方法调用分配给dyno,这样当我的应用程序启动时,它会启动额外的工人代码,然后调用我的单一方法并开始嗡嗡作响。但不知道如何分配该方法调用?



我可以使用延迟作业,它可以解决所有问题,但似乎我需要的是一个单独的即时调用当被调用时)而不是预定的。但也许这是唯一的方法。

解决方案

在您的Procfile中,您可以为您的额外工作作业添加一个单独的进程

 #procfile 
web:...
worker:...
collector:bundle exec ruby stats_collector.rb -p $ PORT#或任何其他命令

然后可以使用 $ heroku ps:scale collector = 1 。它会单独运行,并在每次部署时停止并重新启动。我会说,你的应用程序因此必须安全地随时中断使用这样一个长时间运行的过程。 如果中断可能导致问题(例如,如果重新启动后只做了一次,那么传统后台工作者将使用例如排队消息的可能是更安全的解决方案。



Update



如果要在Rails应用程序的上下文中非交互式地执行长时间运行的代码,可以使用 rails runner 命令。

 #procfile 
web:...
worker:...
collector:bundle exec rails runner'MyModel.my_long_running_job'


What is the best way for running a continuous worker process on Heroku? My application needs this process running 24/7, so delayed_job or other queues seem like overkill. I can trigger the process manually through the Heroku console, and I could detach it would run by itself.

Preferably, I'd like to assign just this single method call to a dyno, such that when my app boots, it launches the extra worker-dyno and then calls my single method and starts humming away. But not sure how to assign that method call?

I could use delayed job and it would solve everything, but it seems my need is for a singularly activity that is immediate (when called) and not scheduled. But perhaps this is the only way.

解决方案

In your Procfile you could add a seperate process for your extra worker job

# Procfile
web: ...
worker: ...
collector: bundle exec ruby stats_collector.rb -p $PORT # or any other command

You could then scale this process with $ heroku ps:scale collector=1. It would run seperately and be stopped and restarted at every deploy. I'd say that what your app does thus has to be safe to be interrupted any time to use such a long running process.

If interruptions might lead to problem (e.g. stuff you want only done once is done twice if restarted) the a 'traditional' background worker using e.g. Sidekiq that queues messages might be a safer solution.

Update

If you want to execute long-running code non-interactively in the context of your Rails app, you can use the rails runner command.

# Procfile
web: ...
worker: ...
collector: bundle exec rails runner 'MyModel.my_long_running_job'

这篇关于在Heroku上运行连续运行的工作进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆