[ap4r-devel] [284] branches/200709_gihyo/async_shop/as_rails: extrace Rails and AP4R service handlers from a test helper into the plugin library.
shino at rubyforge.org
shino at rubyforge.org
Thu Aug 30 21:02:05 EDT 2007
Revision: 284
Author: shino
Date: 2007-08-30 21:02:04 -0400 (Thu, 30 Aug 2007)
Log Message:
-----------
extrace Rails and AP4R service handlers from a test helper into the plugin library.
Hadlers functinals
- Start and stop Rails/AP4R processes
- Start and stop AP4R's dispathcers
- Wait until no active messages in AP4R service
- etc.
Needs more work :-)
Modified Paths:
--------------
branches/200709_gihyo/async_shop/as_rails/test/async/ap4r_test_helper.rb
branches/200709_gihyo/async_shop/as_rails/vendor/plugins/ap4r/lib/ap4r/service_handler.rb
Modified: branches/200709_gihyo/async_shop/as_rails/test/async/ap4r_test_helper.rb
===================================================================
--- branches/200709_gihyo/async_shop/as_rails/test/async/ap4r_test_helper.rb 2007-08-31 00:58:43 UTC (rev 283)
+++ branches/200709_gihyo/async_shop/as_rails/test/async/ap4r_test_helper.rb 2007-08-31 01:02:04 UTC (rev 284)
@@ -4,9 +4,9 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
-require "ap4r/service_handler.rb"
+require "ap4r/service_handler"
-ap4r_test_helper = Ap4rTestHelper.new
+ap4r_test_helper = Ap4r::ServiceHandler.new
# ap4r_test_helper.start_rails_service
# at_exit { ap4r_test_helper.stop_rails_service }
Modified: branches/200709_gihyo/async_shop/as_rails/vendor/plugins/ap4r/lib/ap4r/service_handler.rb
===================================================================
--- branches/200709_gihyo/async_shop/as_rails/vendor/plugins/ap4r/lib/ap4r/service_handler.rb 2007-08-31 00:58:43 UTC (rev 283)
+++ branches/200709_gihyo/async_shop/as_rails/vendor/plugins/ap4r/lib/ap4r/service_handler.rb 2007-08-31 01:02:04 UTC (rev 284)
@@ -2,160 +2,164 @@
# Copyright:: Copyright (c) 2007 Future Architect Inc.
# Licence:: MIT Licence
+require 'rubygems'
require 'erb'
require 'yaml'
require 'reliable-msg'
require 'ap4r'
+require 'timeout'
-class Ap4rTestHelper
+module Ap4r
- def initialize(config_file = RAILS_ROOT + "/config/test_async.yml")
- raise "please create config/async_test.yml to configure ap4r service." unless File.exist?(config_file)
+ class ServiceHandler
- config = {}
- File.open(config_file, "r") do |input|
- YAML.load_documents(ERB.new(input.read).result) do |doc|
- config.merge! doc
+ def initialize(config_file = RAILS_ROOT + "/config/test_async.yml")
+ raise "please create config/async_test.yml to configure ap4r service." unless File.exist?(config_file)
+
+ config = {}
+ File.open(config_file, "r") do |input|
+ YAML.load_documents(ERB.new(input.read).result) do |doc|
+ config.merge! doc
+ end
end
+ @test_config = config["ap4r"]
+ @root_dir = @test_config["root_dir"]
+ @config_file = @test_config["config_file"]
+ @test_server_config = ReliableMsg::Config.new(File.join(@root_dir, @config_file))
+ raise "config file #{@test_server_config.path} NOT exist!" unless @test_server_config.exist?
+
+ @test_server_config.load_no_create
+ @qm = nil
end
- @test_config = config["ap4r"]
- @root_dir = @test_config["root_dir"]
- @config_file = @test_config["config_file"]
- @test_server_config = ReliableMsg::Config.new(File.join(@root_dir, @config_file))
- raise "config file #{@test_server_config.path} NOT exist!" unless @test_server_config.exist?
- @test_server_config.load_no_create
- @qm = nil
- end
+ def qm
+ @qm ||= DRbObject.new_with_uri("druby://localhost:#{@test_server_config.drb["port"]}")
+ end
- def qm
- @qm ||= DRbObject.new_with_uri("druby://localhost:#{@test_server_config.drb["port"]}")
- end
+ # Starts ap4r service.
+ def start_ap4r_service(wait_until_started = true)
+ command = "ruby #{@test_config["start_ruby_args"]} #{@root_dir}/script/mongrel_ap4r " +
+ "start -d -c #{@root_dir} -A #{@config_file} -p 13038"
+ message = "Starting Mongrel(AP4R)"
+ execute_command(command, message, false)
+ if wait_until_started
+ print "and waiting..."
+ wait_until_alive
+ end
+ puts "Done."
+ end
- # Starts ap4r service.
- def start_ap4r_service(wait_until_started = true)
- command = "ruby #{@test_config["start_ruby_args"]} #{@root_dir}/script/mongrel_ap4r " +
- "start -d -c #{@root_dir} -A #{@config_file} -p 13038"
- message = "Starting Mongrel(AP4R)"
- execute_command(command, message, false)
- if wait_until_started
- print "and waiting..."
- wait_until_alive
+ # Stops ap4r service.
+ def stop_ap4r_service
+ command = "ruby #{@test_config["stop_ruby_args"]} #{@root_dir}/script/mongrel_ap4r " +
+ "stop -c #{@root_dir}"
+ message = "Terminating Mongrel(AP4R)"
+ execute_command(command, message, false)
+ @qm = nil
end
- puts "Done."
- end
- # Stops ap4r service.
- def stop_ap4r_service
- command = "ruby #{@test_config["stop_ruby_args"]} #{@root_dir}/script/mongrel_ap4r " +
- "stop -c #{@root_dir}"
- message = "Terminating Mongrel(AP4R)"
- execute_command(command, message, false)
- @qm = nil
- end
+ # Starts rails service.
+ # Invokes mongrel_rails, so mongrel_rails should be installed.
+ def start_rails_service
+ # TODO: Can use script/server? It's more general. 2007/05/31 by shino
+ command = "mongrel_rails start -d --environment test"
+ message = "Starting Mongrel(Rails)"
+ execute_command(command, message)
+ end
- # Starts rails service.
- # Invokes mongrel_rails, so mongrel_rails should be installed.
- def start_rails_service
- # TODO: Can use script/server? It's more general. 2007/05/31 by shino
- command = "mongrel_rails start -d --environment test"
- message = "Starting Mongrel(Rails)"
- execute_command(command, message)
- end
-
- require 'timeout'
- # Stops rails service.
- def stop_rails_service
-# puts "read pid"
-# pid = File.read('log/mongrel.pid').to_i
-# puts "send signal to #{pid}"
-# Process.kill(:TERM, `cat log/mongrel.pid`.to_i)
-# puts "done"
-# return
- command = "mongrel_rails stop"
-# command = "kill " + `cat log/mongrel.pid`
- command = "sh -c 'mongrel_rails stop'"
- message = "Terminating Mongrel(Rails)"
- begin
- timeout(5) do
- execute_command(command, message, false)
- end
- rescue TimeoutError
- puts "!!! command timed out !!!"
+ # Stops rails service.
+ def stop_rails_service
+ # puts "read pid"
+ # pid = File.read('log/mongrel.pid').to_i
+ # puts "send signal to #{pid}"
+ # Process.kill(:TERM, `cat log/mongrel.pid`.to_i)
+ # puts "done"
+ # return
+ command = "mongrel_rails stop"
+ # command = "kill " + `cat log/mongrel.pid`
+ # command = "sh -c 'mongrel_rails stop'"
+ message = "Terminating Mongrel(Rails)"
+ execute_command(command, message, false)
end
- end
- # Starts rails service and ap4r service.
- # After block execution, stops both.
- def with_services
- begin
- start_rails_service
+ # Starts rails service and ap4r service.
+ # After block execution, stops both.
+ def with_services
begin
- start_ap4r_service
- yield
+ start_rails_service
+ begin
+ start_ap4r_service
+ yield
+ ensure
+ stop_ap4r_service
+ end
ensure
- stop_ap4r_service
+ stop_rails_service
end
- ensure
- stop_rails_service
end
- end
- def start_dispatchers
- qm.dispatchers.start
- end
+ def start_dispatchers
+ qm.dispatchers.start
+ end
- def stop_dispatchers
- qm.dispatchers.stop
- end
+ def stop_dispatchers
+ qm.dispatchers.stop
+ end
- def clear(*queues)
- raise "not yet implemented"
- queues.each do |queue|
- q = ReliableMsg::Queue.new(queue)
- loop do
- break unless q.get
+ def clear(*queues)
+ raise "not yet implemented"
+ queues.each do |queue|
+ q = ReliableMsg::Queue.new(queue)
+ loop do
+ break unless q.get
+ end
end
end
- end
- def wait_for_saf_forward
- 50.times do
- count = ::Ap4r::StoredMessage.count(:conditions => {:status => ::Ap4r::StoredMessage::STATUS_STORED})
- break if count == 0
- sleep 0.2
+ def wait_for_saf_forward
+ 50.times do
+ count = ::Ap4r::StoredMessage.count(:conditions => {:status => ::Ap4r::StoredMessage::STATUS_STORED})
+ break if count == 0
+ sleep 0.2
+ end
end
- end
- def wait_all_done
- 50.times do
- break if flag = qm.no_active_message?
- sleep 0.2
+ def wait_all_done
+ 50.times do
+ break if flag = qm.no_active_message?
+ sleep 0.2
+ end
end
- end
- def dlq
- qm.list :queue => "$dlq"
- end
+ def dlq
+ qm.list :queue => "$dlq"
+ end
- private
- def execute_command(command, message, with_done_message = true)
-# print "#{message} with command: #{command}..."
- result = system(command, "")
- # TODO: handle result 2007/08/29 by shino
- puts "Done." if with_done_message
- end
-
- def wait_until_alive(message = nil)
- 50.times do
- print message if message
+ private
+ def execute_command(command, message, with_done_message = true)
+ command += ";" # force to execute via shell
+ print "#{message} with command: #{command}..."
begin
- break if qm.alive?
- rescue => e
- # ignore
+ timeout(10) do
+ result = system("#{command}")
+ # TODO: handle result 2007/08/29 by shino
+ puts "Done." if with_done_message
+ end
+ rescue TimeoutError
+ puts "!!! command timed out !!!"
end
- sleep 0.2
end
+
+ def wait_until_alive(message = nil)
+ 50.times do
+ print message if message
+ begin
+ break if qm.alive?
+ rescue => e
+ # ignore
+ end
+ sleep 0.2
+ end
+ end
end
-
end
More information about the ap4r-devel
mailing list