Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sidekiq current Celluloid Actor

I need to access the current celluloid actor inside of my Sidekiq worker, but I don't see a way to do that.

Whenever I try to call:

Celluloid::Actor.current

I get an error: not in actor scope

I tried to get around finding the current actor by creating a new one every time with:

Celluloid::Actor.new(SecureRandom.hex)

But for some reason it was giving me an error of attempted to call dead actor.

What should I be doing differently to get the current actor inside of a Sidekiq worker?

Background Info I am connecting to a websocket in my worker and sending messages to it.

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

like image 524
ardavis Avatar asked Jun 07 '13 19:06

ardavis


1 Answers

Guess you should define a separate class including Celluloid, here's an example based on 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    

Works like a charm, just finished playing with it. Get the full updated example, install required gems, then start both Sinatra and Sidekiq

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

then browse to http://localhost:4567

like image 166
Utgarda Avatar answered Oct 19 '22 17:10

Utgarda