Skip to content

Commit

Permalink
switch to add_actor, make things readvertisable
Browse files Browse the repository at this point in the history
  • Loading branch information
outerim committed May 19, 2009
1 parent 1d64e0c commit b393919
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
19 changes: 16 additions & 3 deletions examples/actors/randomz.rb
@@ -1,14 +1,27 @@
class Randomz < Pelvis::Actor
operation "/do/random"
def run
send_wait(rand(10))
def self.resources_for(op)
case op
when '/never/works'
['bla']
end
end

def self.added_to_agent(agent)
EM.add_timer(20) {
resources_changed
}
end

operation "/never/works"
def broken
fail :message => "I am hopelessly broken"
end

operation "/do/random"
def run
send_wait(rand(10))
end

def send_wait(number)
wait = random_time
send_data :message => "remaining #{number} waiting #{wait}"
Expand Down
8 changes: 8 additions & 0 deletions lib/pelvis/actor.rb
Expand Up @@ -59,6 +59,9 @@ def operations_for(job)
def resources_for(op)
nil
end

def added_to_agent(agent)
end
end

# TODO: Enable config and deployment resources
Expand Down Expand Up @@ -148,5 +151,10 @@ def fail(args)
failed(args)
raise
end

def resources_changed
self.class.instance_eval "resources_changed"
end

end
end
26 changes: 16 additions & 10 deletions lib/pelvis/agent.rb
Expand Up @@ -58,13 +58,6 @@ def actors
end
private :actors

def add_actor(actor_klass)
actors << actor_klass
actor_klass.on_resources_changed {
Advertiser.new(self, [actor_klass])
}
end

class ConfiguredActor < SimpleDelegator
def initialize(klass, block)
super(klass)
Expand All @@ -83,7 +76,19 @@ def operations_for(job)
end

def add_actor(klass, &block)
actors << ConfiguredActor.new(klass, block)
if block_given?
actors << ConfiguredActor.new(klass, block)
else
actors << klass
end
it = actors.last
it.added_to_agent(self)
it.on_resources_changed {
logger.debug "got resources changed for #{it}, readvertising"
Advertiser.new(self, [it]).on_completed {
logger.debug "readvertisement successful"
}
}
end

def advertise
Expand All @@ -93,8 +98,9 @@ def advertise
return
end

@advertiser = Advertiser.new(self, actors)
@advertiser.on_completed { advertised }
# initial advertisement
a = Advertiser.new(self, actors)
a.on_completed { advertised }
end

def evoke(identity, job)
Expand Down
4 changes: 2 additions & 2 deletions run
Expand Up @@ -12,7 +12,7 @@ require File.dirname(__FILE__) + '/examples/delegates/debugger'

def connect_herault
Pelvis.connect(:local, :identity => "herault", :advertise => false) do |agent|
agent.actors << Herault
agent.add_actor Herault
agent.on_advertised do
puts "herault is ready"
connect_foo
Expand All @@ -22,7 +22,7 @@ end

def connect_foo
Pelvis.connect(:local, :identity => "foo") do |agent|
agent.actors << Randomz
agent.add_actor Randomz
agent.on_advertised do
puts "foo is ready"
connect_bar
Expand Down
8 changes: 4 additions & 4 deletions run_xmpp
Expand Up @@ -18,7 +18,7 @@ end

def connect_herault
Pelvis.connect(:xmpp, :jid => "herault@localhost/agent", :password => "testing", :advertise => false) do |agent|
agent.actors << Herault
agent.add_actor Herault
agent.on_advertised do
puts "herault is ready"
connect_chained
Expand All @@ -28,8 +28,8 @@ end

def connect_chained
Pelvis.connect(:xmpp, :jid => "dummy@localhost/agent", :password => "testing") do |agent|
agent.actors << Randomz
agent.actors << Chained
agent.add_actor Randomz
agent.add_actor Chained

agent.on_advertised do
puts "chained is ready"
Expand All @@ -40,7 +40,7 @@ end

def connect_inner
Pelvis.connect(:xmpp, :jid => "dummy2@localhost/agent", :password => "testing") do |agent|
agent.actors << Inner
agent.add_actor Inner

agent.on_advertised do
puts "inner is ready"
Expand Down

0 comments on commit b393919

Please sign in to comment.