diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 4 | ||||
-rw-r--r-- | railties/lib/rails/application.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/application/finisher.rb | 4 | ||||
-rw-r--r-- | railties/lib/rails/commands/dbconsole.rb | 56 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 7 | ||||
-rw-r--r-- | railties/lib/rails/queueing.rb | 10 | ||||
-rw-r--r-- | railties/test/application/queue_test.rb | 39 | ||||
-rw-r--r-- | railties/test/commands/dbconsole_test.rb | 128 | ||||
-rw-r--r-- | railties/test/queueing/threaded_consumer_test.rb | 19 | ||||
-rw-r--r-- | railties/test/railties/engine_test.rb | 31 |
11 files changed, 273 insertions, 35 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index bc34ced283..b7c042cee3 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Add `config.queue_consumer` to allow the default consumer to be configurable. *Carlos Antonio da Silva* + +* Add Rails.queue as an interface with a default implementation that consumes jobs in a separate thread. *Yehuda Katz* + * Remove Rack::SSL in favour of ActionDispatch::SSL. *Rafael Mendonça França* * Remove Active Resource from Rails framework. *Prem Sichangrist* diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index c7b19c964a..c4edbae55b 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -66,7 +66,7 @@ module Rails end end - attr_accessor :assets, :sandbox + attr_accessor :assets, :sandbox, :queue_consumer alias_method :sandbox?, :sandbox attr_reader :reloaders attr_writer :queue diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 25bb680f69..a2e5dece16 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -8,10 +8,11 @@ module Rails attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets, :autoflush_log, :cache_classes, :cache_store, :consider_all_requests_local, :console, :dependency_loading, :exceptions_app, :file_watcher, :filter_parameters, - :force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags, :preload_frameworks, - :railties_order, :relative_url_root, :secret_token, + :force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags, + :preload_frameworks, :railties_order, :relative_url_root, :secret_token, :serve_static_assets, :ssl_options, :static_cache_control, :session_options, - :time_zone, :reload_classes_only_on_change, :use_schema_cache_dump, :queue + :time_zone, :reload_classes_only_on_change, :use_schema_cache_dump, + :queue, :queue_consumer attr_writer :log_level attr_reader :encoding @@ -44,6 +45,7 @@ module Rails @log_formatter = ActiveSupport::Logger::SimpleFormatter.new @use_schema_cache_dump = true @queue = Rails::Queueing::Queue + @queue_consumer = Rails::Queueing::ThreadedConsumer @assets = ActiveSupport::OrderedOptions.new @assets.enabled = false diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 6a24e01f29..84f2601f28 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -96,8 +96,8 @@ module Rails initializer :activate_queue_consumer do |app| if config.queue == Rails::Queueing::Queue - consumer = Rails::Queueing::ThreadedConsumer.start(app.queue) - at_exit { consumer.shutdown } + app.queue_consumer = config.queue_consumer.start(app.queue) + at_exit { app.queue_consumer.shutdown } end end end diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index 6fc127efae..25a8caeec3 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -5,12 +5,15 @@ require 'rbconfig' module Rails class DBConsole + attr_reader :arguments + def self.start(app) new(app).start end - def initialize(app) + def initialize(app, arguments = ARGV) @app = app + @arguments = arguments end def start @@ -31,8 +34,8 @@ module Rails options['header'] = h end - opt.parse!(ARGV) - abort opt.to_s unless (0..1).include?(ARGV.size) + opt.parse!(arguments) + abort opt.to_s unless (0..1).include?(arguments.size) end unless config = @app.config.database_configuration[Rails.env] @@ -40,20 +43,6 @@ module Rails end - def find_cmd(*commands) - dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR) - commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ - - full_path_command = nil - found = commands.detect do |cmd| - dir = dirs_on_path.detect do |path| - full_path_command = File.join(path, cmd) - File.executable? full_path_command - end - end - found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.") - end - case config["adapter"] when /^mysql/ args = { @@ -72,17 +61,17 @@ module Rails args << config['database'] - exec(find_cmd('mysql', 'mysql5'), *args) + find_cmd_and_exec(['mysql', 'mysql5'], *args) when "postgresql", "postgres" ENV['PGUSER'] = config["username"] if config["username"] ENV['PGHOST'] = config["host"] if config["host"] ENV['PGPORT'] = config["port"].to_s if config["port"] ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password - exec(find_cmd('psql'), config["database"]) + find_cmd_and_exec('psql', config["database"]) when "sqlite" - exec(find_cmd('sqlite'), config["database"]) + find_cmd_and_exec('sqlite', config["database"]) when "sqlite3" args = [] @@ -91,7 +80,7 @@ module Rails args << "-header" if options['header'] args << config['database'] - exec(find_cmd('sqlite3'), *args) + find_cmd_and_exec('sqlite3', *args) when "oracle", "oracle_enhanced" logon = "" @@ -102,12 +91,35 @@ module Rails logon << "@#{config['database']}" if config['database'] end - exec(find_cmd('sqlplus'), logon) + find_cmd_and_exec('sqlplus', logon) else abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!" end end + + protected + + def find_cmd_and_exec(commands, *args) + commands = Array(commands) + + dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR) + commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + + full_path_command = nil + found = commands.detect do |cmd| + dir = dirs_on_path.detect do |path| + full_path_command = File.join(path, cmd) + File.executable? full_path_command + end + end + + if found + exec full_path_command, *args + else + abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.") + end + end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index c9654fc63d..9bf9cbe022 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -608,7 +608,12 @@ module Rails desc "Copy migrations from #{railtie_name} to application" task :migrations do ENV["FROM"] = railtie_name - Rake::Task["railties:install:migrations"].invoke + if Rake::Task.task_defined?("railties:install:migrations") + Rake::Task["railties:install:migrations"].invoke + else + Rake::Task["app:railties:install:migrations"].invoke + end + end end end diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb index 2e187b8555..b4bc7fcd18 100644 --- a/railties/lib/rails/queueing.rb +++ b/railties/lib/rails/queueing.rb @@ -16,13 +16,13 @@ module Rails # Jobs are run in a separate thread to catch mistakes where code # assumes that the job is run in the same thread. class TestQueue < ::Queue - # Get a list of the jobs off this queue. This method may not be + # Get a list of the jobs off this queue. This method may not be # available on production queues. def jobs @que.dup end - # Drain the queue, running all jobs in a different thread. This method + # Drain the queue, running all jobs in a different thread. This method # may not be available on production queues. def drain # run the jobs in a separate thread so assumptions of synchronous @@ -53,7 +53,7 @@ module Rails begin job.run rescue Exception => e - Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}" + handle_exception e end end end @@ -64,6 +64,10 @@ module Rails @queue.push nil @thread.join end + + def handle_exception(e) + Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}" + end end end end diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb index 71b0c2bc38..da8bdeed52 100644 --- a/railties/test/application/queue_test.rb +++ b/railties/test/application/queue_test.rb @@ -63,7 +63,7 @@ module ApplicationTests test "in test mode, the queue can be observed" do app("test") - job = Class.new(Struct.new(:id)) do + job = Struct.new(:id) do def run end end @@ -79,7 +79,7 @@ module ApplicationTests assert_equal jobs, Rails.queue.jobs end - test "a custom queue implementation can be provided" do + def setup_custom_queue add_to_env_config "production", <<-RUBY require "my_queue" config.queue = MyQueue @@ -94,10 +94,14 @@ module ApplicationTests RUBY app("production") + end + + test "a custom queue implementation can be provided" do + setup_custom_queue assert_kind_of MyQueue, Rails.queue - job = Class.new(Struct.new(:id, :ran)) do + job = Struct.new(:id, :ran) do def run self.ran = true end @@ -108,5 +112,34 @@ module ApplicationTests assert_equal true, job1.ran end + + test "a custom consumer implementation can be provided" do + add_to_env_config "production", <<-RUBY + require "my_queue_consumer" + config.queue_consumer = MyQueueConsumer + RUBY + + app_file "lib/my_queue_consumer.rb", <<-RUBY + class MyQueueConsumer < Rails::Queueing::ThreadedConsumer + attr_reader :started + + def start + @started = true + self + end + end + RUBY + + app("production") + + assert_kind_of MyQueueConsumer, Rails.application.queue_consumer + assert Rails.application.queue_consumer.started + end + + test "default consumer is not used with custom queue implementation" do + setup_custom_queue + + assert_nil Rails.application.queue_consumer + end end end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb new file mode 100644 index 0000000000..0bf417c014 --- /dev/null +++ b/railties/test/commands/dbconsole_test.rb @@ -0,0 +1,128 @@ +require 'abstract_unit' +require 'rails/commands/dbconsole' + +class Rails::DBConsoleTest < ActiveSupport::TestCase + def teardown + %w[PGUSER PGHOST PGPORT PGPASSWORD'].each{|key| ENV.delete(key)} + end + + def test_no_database_configured + start [], false + assert aborted + assert_match /No database is configured for the environment '\w+'/, output + end + + def test_mysql + dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db') + start [], {adapter: 'mysql', database: 'db'} + assert !aborted + end + + def test_mysql_full + dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--host=locahost', '--port=1234', '--socket=socket', '--user=user', '--default-character-set=UTF-8', '-p', 'db') + start [], {adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8'} + assert !aborted + end + + def test_mysql_include_password + dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--user=user', '--password=qwerty', 'db') + start ['-p'], {adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'} + assert !aborted + end + + def test_postgresql + dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') + start [], {adapter: 'postgresql', database: 'db'} + assert !aborted + end + + def test_postgresql_full + dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') + start [], {adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432} + assert !aborted + assert_equal 'user', ENV['PGUSER'] + assert_equal 'host', ENV['PGHOST'] + assert_equal '5432', ENV['PGPORT'] + assert_not_equal 'q1w2e3', ENV['PGPASSWORD'] + end + + def test_postgresql_include_password + dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') + start ['-p'], {adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'} + assert !aborted + assert_equal 'user', ENV['PGUSER'] + assert_equal 'q1w2e3', ENV['PGPASSWORD'] + end + + def test_sqlite + dbconsole.expects(:find_cmd_and_exec).with('sqlite', 'db') + start [], {adapter: 'sqlite', database: 'db'} + assert !aborted + end + + def test_sqlite3 + dbconsole.expects(:find_cmd_and_exec).with('sqlite3', 'db') + start [], {adapter: 'sqlite3', database: 'db'} + assert !aborted + end + + def test_sqlite3_mode + dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', 'db') + start ['--mode', 'html'], {adapter: 'sqlite3', database: 'db'} + assert !aborted + end + + def test_sqlite3_header + dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', 'db') + start ['--header'], {adapter: 'sqlite3', database: 'db'} + assert !aborted + end + + def test_oracle + dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db') + start [], {adapter: 'oracle', database: 'db', username: 'user', password: 'secret'} + assert !aborted + end + + def test_oracle_include_password + dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user/secret@db') + start ['-p'], {adapter: 'oracle', database: 'db', username: 'user', password: 'secret'} + assert !aborted + end + + def test_unknown_command_line_client + start [], {adapter: 'unknown', database: 'db'} + assert aborted + assert_match /Unknown command-line client for db/, output + end + + private + attr_reader :aborted, :output + + def dbconsole + @dbconsole ||= Rails::DBConsole.new(app) + end + + def start(argv = [], database_configuration = {}) + dbconsole.stubs(arguments: argv) + app.config.stubs(database_configuration: { + Rails.env => database_configuration ? database_configuration.stringify_keys : database_configuration + }) + + @aborted = false + @output = capture(:stderr) do + begin + dbconsole.start + rescue SystemExit + @aborted = true + end + end + end + + def app + @app ||= begin + config = mock("config") + stub("app", config: config) + end + end +end diff --git a/railties/test/queueing/threaded_consumer_test.rb b/railties/test/queueing/threaded_consumer_test.rb index 559de2a82d..c34a966d6e 100644 --- a/railties/test/queueing/threaded_consumer_test.rb +++ b/railties/test/queueing/threaded_consumer_test.rb @@ -78,4 +78,23 @@ class TestThreadConsumer < ActiveSupport::TestCase assert_equal 1, logger.logged(:error).size assert_match(/Job Error: RuntimeError: Error!/, logger.logged(:error).last) end + + test "test overriding exception handling" do + @consumer.shutdown + @consumer = Class.new(Rails::Queueing::ThreadedConsumer) do + attr_reader :last_error + def handle_exception(e) + @last_error = e.message + end + end.start(@queue) + + job = Job.new(1) do + raise "RuntimeError: Error!" + end + + @queue.push job + sleep 0.1 + + assert_equal "RuntimeError: Error!", @consumer.last_error + end end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 9d9565e5f3..9a6b2b66ca 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -111,6 +111,37 @@ module RailtiesTest end end + test "mountable engine should copy migrations within engine_path" do + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + isolate_namespace Bukkits + end + end + RUBY + + @plugin.write "db/migrate/0_add_first_name_to_users.rb", <<-RUBY + class AddFirstNameToUsers < ActiveRecord::Migration + end + RUBY + + @plugin.write "Rakefile", <<-RUBY + APP_RAKEFILE = '#{app_path}/Rakefile' + load 'rails/tasks/engine.rake' + RUBY + + add_to_config "ActiveRecord::Base.timestamped_migrations = false" + + boot_rails + + Dir.chdir(@plugin.path) do + output = `bundle exec rake app:bukkits:install:migrations` + assert File.exists?("#{app_path}/db/migrate/0_add_first_name_to_users.bukkits.rb") + assert_match(/Copied migration 0_add_first_name_to_users.bukkits.rb from bukkits/, output) + assert_equal 1, Dir["#{app_path}/db/migrate/*.rb"].length + end + end + test "no rake task without migrations" do boot_rails require 'rake' |