简体   繁体   English

如何使用Rails和Heroku处理服务器上的数据

[英]How to process data on the server with rails and heroku

I am developing a website using Ruby on Rails and I am doing a bit of rough planning. 我正在使用Ruby on Rails开发网站,并且正在做一些粗略的计划。 I can and have deployed rails websites before, just adding to the database and retrieving from database based on my use case, but this time around, its a bit different. 我可以并且以前已经部署过Rails网站,只是将其添加到数据库中并根据用例从数据库中进行检索,但是这次却有所不同。 I am adding to database but i will need the data to be processed on the server before the data is being sent back to the user or when he decides to retrieve it. 我正在添加数据库,但是在将数据发送回用户之前或当他决定检索数据时,我需要在服务器上处理数据。 What i do not get is how i am going to process the data on the server. 我没有得到的是我将如何处理服务器上的数据。 I know this doesnt follow the normal pattern for asking questions, i would search for it with google except I dont know what I am looking for. 我知道这不遵循正常的提问方式,我会用Google搜索它,除非我不知道自己在寻找什么。 A nudge in the right direction will do. 朝正确方向轻推即可。

What I want to do exactly is have users register and click a button (request) which puts the users id in an array , what I need to do on the server is to randomly or not randomly connect two users based on some qualities, this program keeps running infinitely, such that the user can come back later to check if he has been connected with someone already. 我想做的就是让用户注册并单击一个按钮(请求),该按钮将用户ID放入一个数组中,我需要在服务器上做的是根据某些性质随机或不随机连接两个用户,该程序保持无限运行,以便用户稍后可以返回以检查他是否已经与某人连接。

This kind of logic typically belongs in the controller, or perhaps on the models. 这种逻辑通常属于控制器,或者可能属于模型。 You should read the Rails docs, particularly on controllers: http://guides.rubyonrails.org/action_controller_overview.html 您应该阅读Rails文档,尤其是有关控制器的文档: http : //guides.rubyonrails.org/action_controller_overview.html

I think you may find a lot of benefit from running a background job for this that is constantly looking for matches. 我认为您可以从不断寻找匹配项的后台工作中受益匪浅。 You could have an infinitely running Sidekiq process that is queued up with users. 您可能有一个无限运行的Sidekiq进程,并与用户排队。 Then once one finishes, just fire it up again. 然后,一旦完成,就重新启动它。

Or you could create a rake task that does a User.find_each and have it run again when the task finishes. 或者,您可以创建一个执行User.find_each的rake任务,并在任务完成后再次运行它。 But this would make things blocking if you end up having a lot of users. 但是,如果您最终有很多用户,这将使事情受阻。 I'd recommend one job per user and just bloat the system with them. 我建议每位用户一份工作,然后随他们一起膨胀系统。 This way you can scale out both horizontally and vertically. 这样,您既可以横向扩展又可以纵向扩展。

You'll want to learn about ActiveJob and Sidekiq to help achieve what you're looking for :). 您将需要了解ActiveJobSidekiq,以帮助您实现所需的目标:)。 Sidekiq requires Redis which you'll also have to setup as well. Sidekiq需要Redis ,您也必须对其进行设置。 I'd recommend the redis-rails gem to help with the integration. 我建议使用redis-rails gem来帮助集成。

To go off BenMorganIO's answer, I think this is a job for a background worker. 为了摆脱BenMorganIO的答案,我认为这是后台工作者的工作。 This is a job that is processed in the background, so it doesn't slow up your app. 这是一项在后台处理的作业,因此不会降低应用程序的运行速度。 A good example of this is firing off an email in the background. 一个很好的例子是在后台发出一封电子邮件。

There are primarily 3 gems I've seen for this: 我主要看到以下3个宝石:

Those should point you in the right direction. 这些应该为您指明正确的方向。

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

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