我想将一些会话控制器进程移动到Resque worker中,以使日志记录更顺畅。我想从这里移动部件:
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用户中?
答案 0 :(得分:1)
我倾向于将它们放在app/jobs/
中,因为它位于自动加载路径上,而lib
往往更令人讨厌(尽管它完全有效)。
这应该足够了:
require 'resque-retry'
class FBConnectionsJob
@queue = :fb_connections
def self.perform(user_id)
user = User.find(user_id)
graph = Koala::Facebook::GraphAPI.new(user.token)
user.profile = graph.get_object("me")
user.likes = graph.get_connections("me", "likes")
user.friends = graph.get_connections("me", "friends")
user.save
end
end
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
Resque.enqueue(FBConnectionsJob, current_user.id)
redirect_to root_url
end
PS:为什么要以大写字母输入Class
和End
? O_O