使用 Rails 堆栈,如何创建一个后台进程,通过生成实时工作的线程来处理事件?
使用 Rails 堆栈,如何创建一个后台进程,通过生成实时工作的线程来处理事件?
Heroku 上的工作人员每 5 秒就会拾取一份工作。我需要实时。理想情况下,我希望在 Heroku 上实现此功能,但如果需要,我会放弃它。
With a Rails stack, how can I create a background process that handles events by spawning threads that are worked in real time?
The workers on Heroku pick up jobs every 5 seconds. I need real time. Ideally I'd like to get this working on Heroku, but if I need to, I will move away from it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有一长串后台工作人员列表:Background Job Manager for Rails 3但是不清楚你的问题是否具体
This has a long list of background workers: Background Job Manager for Rails 3 but it is not clear if your question heroku specific or not
我认为您正在寻找类似“run_later”的东西,它不是对作业进行排队,而是实际返回请求并在单独的进程中运行一个块。
这里有一个Rails 3+版本的链接,你可以跟随fork网络找到许多其他的实现:
https:// github.com/Zelnox/run_later
(我不使用 Heroku,所以我不知道它是否可以在其上运行)
I think you are looking for something like "run_later" which instead of queueing a job actually returns the request and runs a block in a separate process.
Here is a link to the Rails 3+ version, you can follow the fork network to find many other implementations:
https://github.com/Zelnox/run_later
(I don't use Heroku so I don't know if it runs on it)
Heroku 运行 rake jobs:work,因此您可以将其替换为您自己的 rake 任务,或者以小于 5 秒的超时运行elasto_job,或者只执行您自己的任务。在那里保留睡眠声明可能是个好主意。
新的 cedar 堆栈将运行您想要的任何内容,因此可能值得检查一下。
对于 run_later 或从当前请求生成,这确实有效,但如果后台进程没有在 30 秒请求超时内完成,那么 heroku 将杀死它。
Heroku runs rake jobs:work, so you can replace that with your own rake task, either running delayed_job with a shorter than 5 second timeout, or just performing your own task. Probably a good idea to keep a sleep statement in there.
The new cedar stack will run anything you want, so it might be worth checking that out.
With regards to run_later, or spawning from the current request, this does work but if the background process doesn't complete within the 30 second request timeout then heroku will kill it.
我认为你需要 delay_job。请检查这个宝石
I think you need delay_job. please checkout this gem