2011-09-21 1 views
1

내 세션 컨트롤러 프로세스 중 일부를 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 근로자 (가/작업에?) 나는 그 작업자 작업을 초기화하는 세션 컨트롤러에 무엇을 추가 할

#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 객체가됩니다. 그러면 Worker의 코드가 User 루프의 사용자가되어야할까요?

답변

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 : 당신은 왜 대문자 ClassEnd를 입력하는? o_O

+0

이 사진을 줄 것입니다. 음,별로 모르겠다. 오케이. 'fb_connections' 심볼을 설명해 주시겠습니까? – Simpleton

+0

@Simpleton 당신이 아직 그것을 알아 내지 못했다면 그 큐의 이름을 넣으십시오. – Autodidact

+0

@Millisami 예. 내가 그때 어떻게 상징을 이해하지 못했는지 파악하기가 어렵다. – Simpleton

관련 문제