如何将此控制器代码移至 Resque 作业?
我想将一些会话控制器进程移至 Resque 工作线程中,以使登录更加顺畅。我想从这里移动部分:
def create
auth = request.env["omniauth.auth"]
omniauth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth,omniauth)
session[:user_id] = user.id
session['fb_auth'] = request.env['omniauth.auth']
session['fb_access_token'] = omniauth['credentials']['token']
session['fb_error'] = nil
@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save
redirect_to root_url
end
到 Resque 工作程序中(是在 /tasks 中吗?)
#ResqueFacebook.rb
require 'resque-retry'
Class FBResque
def self.perform()
@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save
end
End
我需要向会话控制器添加什么来初始化该工作程序作业?另外,因为它不再存在于会话中,所以 current_user 将是一个 nil 对象。这是否意味着工作人员中的代码必须位于 for user in User 循环中?
I want to move some of my sessions controller process into a Resque worker to make logging in much smoother. I want to move parts from here:
def create
auth = request.env["omniauth.auth"]
omniauth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth,omniauth)
session[:user_id] = user.id
session['fb_auth'] = request.env['omniauth.auth']
session['fb_access_token'] = omniauth['credentials']['token']
session['fb_error'] = nil
@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save
redirect_to root_url
end
Into a Resque worker (is it in /tasks?)
#ResqueFacebook.rb
require 'resque-retry'
Class FBResque
def self.perform()
@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save
end
End
What do I add to the sessions controller to initialize that worker job? Also, because it won't exist in the session anymore, current_user will be a nil object. Would that mean the code in the worker would have to be in a for user in User loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我倾向于将它们放在
app/jobs/
中,因为它位于自动加载路径上,而lib
往往更麻烦(尽管它完全有效)。这应该足够了:
PS:为什么你要输入大写的
Class
和End
? o_OI tend to put them in
app/jobs/
, since it's on the autoload path, whilelib
tends to be more of a nuisance (albeit it completely valid).This should be enough:
PS: Why are you typing
Class
andEnd
in uppercase? o_O