Skip to content

Instantly share code, notes, and snippets.

@esmevane
Created December 28, 2015 21:10
Show Gist options
  • Save esmevane/2e6343bc79b3fa489bc5 to your computer and use it in GitHub Desktop.
Save esmevane/2e6343bc79b3fa489bc5 to your computer and use it in GitHub Desktop.
[ Celluloid / DCell / Ruby ]: Run a distributed set of cells.

Running a distributed actor web in Ruby using Celluloid and DCells

Requirements

  • DCell version 1.16.0
  • ZeroMQ version 3.2.5
  • A Redis instance accessible by URI

Why?

The docs on the formal wiki seem a little vague on versioning and dependencies, and doesn't neatly indicate a teardown sequence for the actors it demonstrates. This updated example has all of that.

require 'dcell'
url = ENV['REDIS_URI']
DCell.start(
id: "itchy",
addr: "tcp://127.0.0.1:9001",
registry: { adapter: 'redis', url: url }
)
class Itchy
include Celluloid
def initialize
puts "Ready for mayhem!"
@n = 0
end
def fight
@n = (@n % 6) + 1
if @n <= 3
puts "Bite!"
else
puts "Fight!"
end
end
def end_the_show
puts "The itchy and scratchy show!"
terminate
end
end
class ItchyGroup < Celluloid::SupervisionGroup
supervise Itchy, as: :itchy
end
supervisor = ItchyGroup.run!
while supervisor.actors.any?
sleep(rand)
end
require 'dcell'
url = ENV['REDIS_URI']
DCell.start(
id: "scratchy",
addr: "tcp://127.0.0.1:9002",
registry: { adapter: 'redis', url: url }
)
class Scratchy
include Celluloid
attr_reader :itchy, :service
def initialize
@service = DCell::Node[:itchy]
@itchy = service[:itchy]
link service
puts "Fighting itchy! (check itchy's output)"
end
def the_show
6.times { itchy.fight; sleep(rand) }
sleep(rand)
itchy.end_the_show
end
end
Scratchy.new.the_show
@esmevane
Copy link
Author

Some quirks I noticed: If you don't explicitly state the ZMQ address, then the registry will clobber itself and hang. So in terms of service discovery, this doesn't really provide a great example for anything which isn't hard coded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment