public
Description: self assembling fabric of ruby daemons
Home | Edit | New

Web Frameworks

Web Framework Integration

The integration of Nanite into web frameworks depends on the web server running our application and, more importantly, if it uses EventMachine or not.

Thin

Thin is EventMachine-based, so we only need to make sure that the EventMachine reactor is already ‘heated’ up

  Thread.new do
    until EM.reactor_running?
      sleep 1
    end
    Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
  end

Mongrel

Mongrel on the other hand does not use EventMachine and therefore requires to wrap the start of our mapper

  Thread.new do
    EM.run do
      Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
    end
  end

Passenger

if defined?(PhusionPassenger)

 PhusionPassenger.on_event(:starting_worker_process) do |forked|
   if forked
     if EM.reactor_running?
       EM.stop_event_loop
       EM.release_machine
       EM.instance_variable_set( '@reactor_running', false )
     end
     Thread.current[:mq] = nil
     AMQP.instance_variable_set('@conn', nil)
   end

   th = Thread.current
   Thread.new{
     Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
   }
   Thread.stop
 end

end

Where to put the mapper initialization code depends on the framework and our preference. For Rails the canonical place to start our mapper is within nanite.rb (or any other filename you prefer) in config/initalizers. In Merb we can use init.rb in config.

Check out also:

  • nanite-rails plugin initializer:

http://github.com/dcu/nanite-rails/blob/39250ad8facab7c8afa3cbf2a465bfd77225e63e/generators/nanite/templates/config/initializers/nanite.rb

ActiveRecord

If you use ActiveRecord on an agent, be sure to close the database connections after:

   .. your code ..
ensure
  ActiveRecord::Base.clear_active_connections!
end

Start your agents that use non thread safe code (such as ActiveRecord) with the single threaded flag:

  nanite-agent ..  --single-threaded
Last edited by shift, Wed Sep 23 03:40:41 -0700 2009
Home | Edit | New
Versions: