Created
August 18, 2013 14:28
-
-
Save StephanieSunshine/6261919 to your computer and use it in GitHub Desktop.
Celluloid signal testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'celluloid' | |
require 'pp' | |
require 'timers' | |
#include Celluloid | |
class Bot | |
include Celluloid | |
attr_accessor :id | |
class Restart < StandardError; end | |
class Shutdown < StandardError; end | |
def initialize(name) | |
self.id = name | |
end | |
def set_status(status) | |
@status = status | |
end | |
def report | |
"#{self.id} is #{@status}" | |
end | |
def restart(reason) | |
raise Restart, 'Restart|'+self.id.to_s+'|'+reason | |
end | |
def shutdown(reason) | |
raise Shutdown, reason | |
end | |
end | |
class Controller | |
attr_accessor :botlist | |
include Celluloid | |
trap_exit :actor_died | |
def initialize | |
self.botlist = {} | |
end | |
def actor_died(actor, reason) | |
p "Oh no! #{actor.inspect} has died because of a #{reason}" | |
# we need to remove them from the list | |
result = reason.to_s.split(/\|/) | |
self.botlist.delete(result[1]) | |
#p "We want to restart #{result[1]}" if result[0] == 'Restart' | |
self.start_bot(result[1]) if result[0] == 'Restart' | |
end | |
def start_bot(id) | |
mybot = Bot.supervise(id) | |
self.link mybot | |
self.botlist[id] = mybot | |
end | |
def stop_bot(id) | |
mybot = self.botlist[id] | |
#mybot.restart("Controller Called") | |
mybot.terminate | |
self.botlist.delete(id) | |
end | |
def restart_bot(id) | |
mybot = self.botlist[id] | |
#mybot.restart("Controller Called") | |
mybot.terminate | |
self.botlist.delete(id) | |
start_bot(id) | |
end | |
def bot_count | |
self.botlist.keys.count | |
end | |
end | |
controller = Controller.new | |
timer = Timers.new | |
#loop { timer.wait } | |
(1..10).each do |bot| | |
controller.start_bot(bot) | |
end | |
controller.botlist.keys.each do |bot| | |
controller.restart_bot(bot) if bot.even? | |
controller.stop_bot(bot) if bot.odd? | |
end | |
sleep(5) | |
pp controller.botlist.keys | |
puts "bots running #{controller.bot_count}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fuzzy@treehouse:~/test/celluloid$ ./test.rb
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
"Oh no! #<Celluloid::ActorProxy(Celluloid::SupervisionGroup) dead> has died because of a "
[2, 4, 6, 8, 10]
bots running 5
D, [2013-08-18T07:13:48.546705 #16128] DEBUG -- : Terminating 11 actors...