Skip to content

Commit

Permalink
* Fixes for running the poller with Rails.
Browse files Browse the repository at this point in the history
  • Loading branch information
donv committed Jun 11, 2009
1 parent bdde485 commit 569e040
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
10 changes: 6 additions & 4 deletions generators/processor/templates/poller
@@ -1,13 +1,15 @@
#!/usr/bin/env ruby

ENV['APP_ROOT'] ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
APP_ROOT = ENV['APP_ROOT']
script_file = File.join(APP_ROOT, 'lib', 'poller.rb')
tmp_dir = File.join(APP_ROOT, 'tmp')

require 'rubygems'
require 'daemons'
require 'activesupport'
require 'activemessaging'

APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
script_file = File.join(File.dirname(__FILE__)+'/../lib/poller.rb')
tmp_dir = File.join(File.expand_path(APP_ROOT), 'tmp')

options = {
:app_name => "poller",
:dir_mode => :normal,
Expand Down
9 changes: 5 additions & 4 deletions generators/processor/templates/poller.rb
Expand Up @@ -4,12 +4,13 @@
STDERR.sync = true; STDERR.flush

#Try to Load Merb
begin
merb_init_file = File.expand_path(File.dirname(__FILE__)+'/../config/merb_init')
if File.exists? merb_init_file
require File.expand_path(File.dirname(__FILE__)+'/../config/boot')
#need this because of the CWD
Merb.root = MERB_ROOT
require File.expand_path(File.dirname(__FILE__)+'/../config/merb_init')
rescue LoadError
require merb_init_file
else
# Load Rails
RAILS_ROOT=File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(RAILS_ROOT, 'config', 'boot')
Expand All @@ -20,4 +21,4 @@
#ActiveMessaging::load_processors

# Start it up!
ActiveMessaging::start
ActiveMessaging::start
2 changes: 1 addition & 1 deletion generators/processor/templates/processor_test.rb
@@ -1,5 +1,5 @@
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/../../vendor/plugins/activemessaging/lib/activemessaging/test_helper'
require 'activemessaging/test_helper'
require File.dirname(__FILE__) + '/../../app/processors/application'

class <%= class_name %>ProcessorTest < Test::Unit::TestCase
Expand Down
26 changes: 13 additions & 13 deletions lib/activemessaging.rb
@@ -1,7 +1,8 @@
module ActiveMessaging
VERSION = "0.5" #maybe this should be higher, but I'll let others judge :)
VERSION = "0.6.0"
APP_ROOT = ENV['APP_ROOT'] || ENV['RAILS_ROOT'] || ((defined? RAILS_ROOT) && RAILS_ROOT) || File.dirname($0)
APP_ENV = ENV['APP_ENV'] || ENV['RAILS_ENV'] || 'development'
ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

# Used to indicate that the processing for a thread shoud complete
class StopProcessingException < Interrupt #:nodoc:
Expand All @@ -18,9 +19,10 @@ class StopFilterException < Exception #:nodoc:
end

def ActiveMessaging.logger
@@logger = RAILS_DEFAULT_LOGGER if !defined?(@@logger) && (defined?(RAILS_DEFAULT_LOGGER) && !RAILS_DEFAULT_LOGGER.nil?)
@@logger = ActiveRecord::Base.logger unless defined?(@@logger)
@@logger = Logger.new(STDOUT) unless defined?(@@logger)
@@logger = nil unless defined? @@logger
@@logger ||= RAILS_DEFAULT_LOGGER if defined? RAILS_DEFAULT_LOGGER
@@logger ||= ActiveRecord::Base.logger if defined? ActiveRecord
@@logger ||= Logger.new(STDOUT)
@@logger
end

Expand All @@ -36,18 +38,20 @@ def self.load_extensions
require 'activemessaging/trace_filter'

# load all under the adapters dir
Dir[APP_ROOT + '/vendor/plugins/activemessaging/lib/activemessaging/adapters/*.rb'].each{|a|
Dir[File.join(ROOT, 'lib', 'activemessaging', 'adapters', '*.rb')].each do |a|
begin
adapter_name = File.basename(a, ".rb")
require 'activemessaging/adapters/' + adapter_name
rescue RuntimeError, LoadError => e
logger.debug "ActiveMessaging: adapter #{adapter_name} not loaded: #{ e.message }"
end
}
end
end

def self.load_config
p APP_ROOT
path = File.expand_path("#{APP_ROOT}/config/messaging.rb")
p path
begin
load path
rescue MissingSourceFile
Expand Down Expand Up @@ -115,10 +119,9 @@ class DoSomethingProcessor < ActiveMessaging::Processor
#load these once to start with
ActiveMessaging.load_activemessaging



# reload these on each request - leveraging Dispatcher semantics for consistency
begin
# reload these on each Rails request - leveraging Dispatcher semantics for consistency
if defined? Rails
ActiveMessaging.logger.info "Rails available: Adding dispatcher prepare callback."
require 'dispatcher' unless defined?(::Dispatcher)

# add processors and config to on_prepare if supported (rails 1.2+)
Expand All @@ -127,7 +130,4 @@ class DoSomethingProcessor < ActiveMessaging::Processor
ActiveMessaging.reload_activemessaging
end
end
rescue MissingSourceFile => e
logger.info e.message
logger.info "Rails not available."
end

0 comments on commit 569e040

Please sign in to comment.