Sidekiq目前赛璐珞演员

时间:2013-06-07 19:29:53

标签: ruby sidekiq celluloid

我需要访问我的Sidekiq工作者当前的赛璐珞演员,但我没有办法做到这一点。

每当我试着打电话时:

Celluloid::Actor.current

我收到错误:not in actor scope

我试图通过每次创建一个新演员来找到当前的演员:

Celluloid::Actor.new(SecureRandom.hex)

但由于某种原因,它给了我attempted to call dead actor的错误。

我应该采取哪些不同方式让当前演员进入Sidekiq工作人员?

背景信息  我正在连接到我的工作人员的websocket并向其发送消息。

Celluloid::WebSocket::Client.new('ws://my-uri', Celluloid::Actor.current)

1 个答案:

答案 0 :(得分:1)

猜猜你应该定义一个单独的类,包括Celluloid,这是一个基于one of those from Sidekiq repo的例子

class MyWebSocketWhatever
  include Celluloid

  def initialize(url)
    @ws_client = Celluloid::WebSocket::Client.new url, Celluloid::Actor.current
  end

  def send_message(msg)
    @ws_client.text msg
  end

  def on_message(msg)
    $redis.lpush 'sinkiq-example-messages', "RESPONSE: #{msg}"
  end
end

class SinatraWorker
  include Sidekiq::Worker

  def perform(msg='lulz you forgot a msg!')
    $redis.lpush 'sinkiq-example-messages', "SENT: #{msg}"
    MyWebSocketWhatever.new('ws://echo.websocket.org').send_message msg
  end
end    

像魅力一样,只是玩完了它。获取the full updated example,安装所需的宝石,然后启动Sinatra和Sidekiq

sidekiq -r ./sinkiq.rb
ruby ./sinkiq.rb

然后浏览到http://localhost:4567