Sidekiq当前的Celluloid Actor

9

我需要在我的Sidekiq worker内访问当前的Celluloid actor,但是我不知道如何做到这一点。

每次我尝试调用:

Celluloid::Actor.current

我遇到一个错误:不在演员范围内

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

Celluloid::Actor.new(SecureRandom.hex)

但是由于某种原因,它给了我一个错误:attempted to call dead actor

为了在Sidekiq工作者中获取当前的actor,我应该怎么做?

背景信息:我正在我的工作者中连接到一个websocket并向其发送消息。

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


为什么你需要这样做? - Sergio Tulentsev
好问题,我认为这并不与问题相关。但是我正在我的 worker 中连接到一个 websocket,并向其发送消息。@websocket_client ||= Celluloid::WebSocket::Client.new('ws://my-uri', Celluloid::Actor.current) - ardavis
你确定要使用工作进程吗?也许你只需要一个简单的守护进程? - Sergio Tulentsev
很不幸,我确实需要使用工作线程。不过这是个好主意! - ardavis
1
祝你好运,希望你能解决这个问题 :) - Sergio Tulentsev
1个回答

1

我猜你应该定义一个包含Celluloid的单独类,这里有一个例子,基于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    

功能非常好,刚刚试玩了一下。获取完整的更新示例,安装所需的gems,然后启动Sinatra和Sidekiq。

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接