如何将此控制器代码移至 Resque 作业?

发布于 2024-12-05 16:25:01 字数 1304 浏览 0 评论 0原文

我想将一些会话控制器进程移至 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-12-12 16:25:01

我倾向于将它们放在 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

I tend to put them in app/jobs/, since it's on the autoload path, while lib tends to be more of a nuisance (albeit it completely valid).

This should be enough:

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: Why are you typing Class and End in uppercase? o_O

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文