Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/halorgium/nanite
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/halorgium/nanite:
  Register actors in the init.rb for agents to allow actors to not be auto-loaded if needed
  • Loading branch information
Ezra Zygmuntowicz committed Jan 23, 2009
2 parents 3958d0d + 1beb4c0 commit e7cc7b0
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 26 deletions.
4 changes: 1 addition & 3 deletions examples/cli.rb
@@ -1,8 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby


require 'rubygems' require File.dirname(__FILE__) + '/../lib/nanite'
require 'nanite'
require 'nanite/mapper'


# cli.rb # cli.rb
# #
Expand Down
13 changes: 2 additions & 11 deletions examples/rack-worker/actors/rack_worker.rb
@@ -1,20 +1,11 @@
class RackWorker < Nanite::Actor class RackWorker < Nanite::Actor

expose :call expose :call

def initialize(app) def initialize(app)
@app = app @app = app
end end

def call(env) def call(env)
@app.call(env) @app.call(env)
end end

end end

app = Proc.new do |env|
[200, {'Content-Type'=>'text/html'}, "hello world!"]
end

Nanite.register(RackWorker.new(app), 'rack')

4 changes: 2 additions & 2 deletions examples/rack-worker/config.yml
@@ -1,5 +1,5 @@
--- ---
:pass: testing :pass: testing
:vhost: /nanite :vhost: /nanite
:user: nanite :user: nanite
:identity: fred :identity: fred
5 changes: 5 additions & 0 deletions examples/rack-worker/init.rb
@@ -0,0 +1,5 @@
app = Proc.new do |env|
[200, {'Content-Type'=>'text/html'}, "hello world!"]
end

register(RackWorker.new(app), 'rack')
5 changes: 0 additions & 5 deletions examples/simpleagent/actors/simple.rb
@@ -1,7 +1,6 @@
# you can execute this nanite from the cli.rb command line example app # you can execute this nanite from the cli.rb command line example app


class Simple < Nanite::Actor class Simple < Nanite::Actor

expose :echo, :time, :gems expose :echo, :time, :gems


def echo(payload) def echo(payload)
Expand All @@ -15,8 +14,4 @@ def time(payload)
def gems(filter) def gems(filter)
::Gem.source_index.refresh!.search(filter).flatten.collect {|gemspec| "#{gemspec.name} #{gemspec.version}"} ::Gem.source_index.refresh!.search(filter).flatten.collect {|gemspec| "#{gemspec.name} #{gemspec.version}"}
end end

end end

Nanite.register(Simple.new)

3 changes: 1 addition & 2 deletions lib/nanite.rb
Expand Up @@ -29,8 +29,7 @@ class << self


# Registers actor instance with given prefix # Registers actor instance with given prefix
def register(actor_instance, prefix = nil) def register(actor_instance, prefix = nil)
@agent.log.info "Registering #{actor_instance.inspect} with prefix #{prefix.inspect}" @agent.register(actor_instance, prefix)
@agent.dispatcher.register(actor_instance, prefix)
end end


# Initializes a new agent and establishes # Initializes a new agent and establishes
Expand Down
11 changes: 8 additions & 3 deletions lib/nanite/agent.rb
Expand Up @@ -39,14 +39,15 @@ def start_console
# Nanite looks for actors under app_root/actors directory. # Nanite looks for actors under app_root/actors directory.
def load_actors def load_actors
return unless root return unless root
if File.exist?(root / 'init.rb')
instance_eval(File.read(root / 'init.rb'), root / 'init.rb')
end


Dir["#{root}/actors/*.rb"].each do |actor| Dir["#{root}/actors/*.rb"].each do |actor|
log.info "loading actor: #{actor}" log.info "loading actor: #{actor}"
require actor require actor
end end

if File.exist?(root / 'init.rb')
instance_eval(File.read(root / 'init.rb'), root / 'init.rb')
end
end end


# Log level used, defaults to INFO. # Log level used, defaults to INFO.
Expand Down Expand Up @@ -145,6 +146,10 @@ def start
start_console if opts[:console] && !opts[:daemonize] start_console if opts[:console] && !opts[:daemonize]
end end


def register(actor_instance, prefix = nil)
dispatcher.register(actor_instance, prefix)
end

# Updates last ping time # Updates last ping time
def pinged! def pinged!
@last_ping = Time.now @last_ping = Time.now
Expand Down
1 change: 1 addition & 0 deletions lib/nanite/dispatcher.rb
Expand Up @@ -21,6 +21,7 @@ def initialize(agent)


def register(actor_instance, prefix = nil) def register(actor_instance, prefix = nil)
raise ArgumentError, "#{actor_instance.inspect} is not a Nanite::Actor subclass instance" unless Nanite::Actor === actor_instance raise ArgumentError, "#{actor_instance.inspect} is not a Nanite::Actor subclass instance" unless Nanite::Actor === actor_instance
@agent.log.info "Registering #{actor_instance.inspect} with prefix #{prefix.inspect}"
prefix ||= actor_instance.class.default_prefix prefix ||= actor_instance.class.default_prefix
@actors[prefix.to_s] = actor_instance @actors[prefix.to_s] = actor_instance
end end
Expand Down

0 comments on commit e7cc7b0

Please sign in to comment.