简体   繁体   English

创建记录的Rails后台作业?

[英]Rails Background Job for Creating Records?

Currently on Rails 4 with Resque. 目前在带有Resque的Rails 4上。

My question is , would it make sense to use a background job for creating a record? 我的问题是 ,使用后台作业创建记录是否有意义? Something like... 就像是...

def create 
  @article = Article.new(articles_params)
  if @article.valid?
      background_job_here
  else
      render 'new'
  end
end

The only other documentation I can find on this matter says that it does not make sense to do this, however, does not explain why. 我可以找到的唯一与此有关的其他文档说,这样做没有任何意义,但是并没有解释原因。

Reading Heroku documentation, they suggest that any request taking more than 500 ms be moved off into the background. 阅读Heroku文档后,他们建议将耗时500毫秒以上的所有请求移到后台。 Creating an article on my site is no exception as it can take upwards of 1,000 ms. 在我的网站上创建文章也不例外,因为它可能需要1000毫秒以上的时间。 If not a background job, how should I architect this? 如果不是后台工作,我应该如何设计呢? Thx! 谢谢!

In my humble opinion, I would not use a background job for anything that the user is expecting a response for. 以我的愚见,我不会将后台作业用于用户期望得到响应的任何内容。 I think that this would disturb the request/response cycle. 我认为这会打扰请求/响应周期。 For instance, in your case, the user is expecting a response to a request for creating his article. 例如,在您的情况下,用户期望对创建其文章的请求做出响应。 However, it is perfectly fine to create a record in a background job as long as it is not mandatory for the response. 但是,只要不是强制性的响应,就可以在后台作业中创建记录。 For example, I do have background jobs for creating other records that are not being requested by the user, but are used for analytics on user action. 例如,我确实有后台作业用于创建用户未请求但用于用户操作分析的其他记录。 Now, it is definitely a problem that it takes 500ms to 1000ms for creating an article. 现在,创建文章大约需要500毫秒到1000毫秒的问题。 It is a performance problem that a background job is not really addressing anyway. 后台作业实际上没有真正解决是一个性能问题。 I do not know what the specifics of your app are, but you may want to look into caching your database (with redis for instance). 我不知道您的应用程序的详细信息是什么,但是您可能希望研究缓存数据库(例如,使用redis)。

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

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