简体   繁体   English

Rails:如何在后台运行模型回调函数?

[英]Rails: how to run a model callback function in the background?

I have a after_create :get_geolocation function that runs after a new user is created. 我有一个after_create :get_geolocation函数,在创建新用户后运行。

  1. Is there a way I can run this function in the background so that there is no delay for the user? 有没有办法可以在后台运行此功能,以使用户没有延迟? Since if the geolocation service being used is down, it will break signups. 由于如果正在使用的地理位置服务关闭,则它将中断注册。

  2. Models can't seem to call request.remote_ip . 模型似乎无法调用request.remote_ip Could this function be somehow moved into a controller for request.remote_ip to work? 可以将此功能以某种方式移入控制器以使request.remote_ip正常工作吗?

You could use Sidekiq , delayed_job , resque , or another asynchronous task utility to perform the geolocation in the background. 你可以使用Sidekiqdelayed_job的resque或其他异步任务实用程序在后台执行的地理定位。 And for the IP, you could either store it in the model or send it along with your queue request. 对于IP,您既可以将其存储在模型中,也可以将其与队列请求一起发送。

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

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