aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails')
-rw-r--r--railties/lib/rails/application.rb7
-rw-r--r--railties/lib/rails/application/configuration.rb9
-rw-r--r--railties/lib/rails/application/finisher.rb4
-rw-r--r--railties/lib/rails/commands/dbconsole.rb7
-rw-r--r--railties/lib/rails/deprecation.rb3
-rw-r--r--railties/lib/rails/generators/app_base.rb73
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb32
-rw-r--r--railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt2
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/application.rb8
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt6
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt4
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt7
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb20
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb2
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb16
-rw-r--r--railties/lib/rails/queueing.rb107
16 files changed, 103 insertions, 204 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 80b8e4b9ba..050190cba6 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'active_support/queueing'
require 'rails/engine'
module Rails
@@ -88,8 +89,8 @@ module Rails
@initialized
end
- # Implements call according to the Rack API. It simples
- # dispatch the request to the underlying middleware stack.
+ # Implements call according to the Rack API. It simply
+ # dispatches the request to the underlying middleware stack.
def call(env)
env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
super(env)
@@ -188,7 +189,7 @@ module Rails
end
def queue #:nodoc:
- @queue ||= Queueing::Container.new(build_queue)
+ @queue ||= ActiveSupport::QueueContainer.new(build_queue)
end
def build_queue #:nodoc:
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb
index 0202e86f32..613c5b25f0 100644
--- a/railties/lib/rails/application/configuration.rb
+++ b/railties/lib/rails/application/configuration.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/kernel/reporting'
require 'active_support/file_update_checker'
+require 'active_support/queueing'
require 'rails/engine/configuration'
module Rails
@@ -12,7 +13,7 @@ module Rails
:railties_order, :relative_url_root, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change,
- :queue, :queue_consumer
+ :queue, :queue_consumer, :beginning_of_week
attr_writer :log_level
attr_reader :encoding
@@ -30,6 +31,7 @@ module Rails
@session_store = :cookie_store
@session_options = {}
@time_zone = "UTC"
+ @beginning_of_week = :monday
@log_level = nil
@middleware = app_middleware
@generators = app_generators
@@ -41,8 +43,8 @@ module Rails
@exceptions_app = nil
@autoflush_log = true
@log_formatter = ActiveSupport::Logger::SimpleFormatter.new
- @queue = Rails::Queueing::Queue
- @queue_consumer = Rails::Queueing::ThreadedConsumer
+ @queue = ActiveSupport::SynchronousQueue
+ @queue_consumer = ActiveSupport::ThreadedQueueConsumer
@eager_load = nil
@assets = ActiveSupport::OrderedOptions.new
@@ -55,7 +57,6 @@ module Rails
@assets.debug = false
@assets.compile = true
@assets.digest = false
- @assets.manifest = nil
@assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
@assets.js_compressor = nil
@assets.css_compressor = nil
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb
index 777f0d269e..d2a402aa51 100644
--- a/railties/lib/rails/application/finisher.rb
+++ b/railties/lib/rails/application/finisher.rb
@@ -97,8 +97,8 @@ module Rails
end
initializer :activate_queue_consumer do |app|
- if config.queue == Rails::Queueing::Queue
- app.queue_consumer = config.queue_consumer.start(app.queue)
+ if config.queue == ActiveSupport::Queue
+ app.queue_consumer = config.queue_consumer.start(app.queue, {logger: Rails.logger})
at_exit { app.queue_consumer.shutdown }
end
end
diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb
index cc0552184a..c84fa832f5 100644
--- a/railties/lib/rails/commands/dbconsole.rb
+++ b/railties/lib/rails/commands/dbconsole.rb
@@ -26,7 +26,12 @@ module Rails
'port' => '--port',
'socket' => '--socket',
'username' => '--user',
- 'encoding' => '--default-character-set'
+ 'encoding' => '--default-character-set',
+ 'sslca' => '--ssl-ca',
+ 'sslcert' => '--ssl-cert',
+ 'sslcapath' => '--ssl-capath',
+ 'sslcipher' => '--ssh-cipher',
+ 'sslkey' => '--ssl-key'
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
if config['password'] && options['include_password']
diff --git a/railties/lib/rails/deprecation.rb b/railties/lib/rails/deprecation.rb
index c5811b2629..89f54069e9 100644
--- a/railties/lib/rails/deprecation.rb
+++ b/railties/lib/rails/deprecation.rb
@@ -3,7 +3,8 @@ require 'active_support/deprecation/proxy_wrappers'
module Rails
class DeprecatedConstant < ActiveSupport::Deprecation::DeprecatedConstantProxy
def self.deprecate(old, current)
- constant = new(old, current)
+ # double assignment is used to avoid "assigned but unused variable" warning
+ constant = constant = new(old, current)
eval "::#{old} = constant"
end
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb
index d69afcd2cb..184c59cb90 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -16,53 +16,56 @@ module Rails
attr_accessor :rails_template
add_shebang_option!
- argument :app_path, :type => :string
+ argument :app_path, type: :string
def self.add_shared_options_for(name)
- class_option :builder, :type => :string, :aliases => "-b",
- :desc => "Path to a #{name} builder (can be a filesystem path or URL)"
+ class_option :builder, type: :string, aliases: '-b',
+ desc: "Path to a #{name} builder (can be a filesystem path or URL)"
- class_option :template, :type => :string, :aliases => "-m",
- :desc => "Path to an #{name} template (can be a filesystem path or URL)"
+ class_option :template, type: :string, aliases: '-m',
+ desc: "Path to an #{name} template (can be a filesystem path or URL)"
- class_option :skip_gemfile, :type => :boolean, :default => false,
- :desc => "Don't create a Gemfile"
+ class_option :skip_gemfile, type: :boolean, default: false,
+ desc: "Don't create a Gemfile"
- class_option :skip_bundle, :type => :boolean, :default => false,
- :desc => "Don't run bundle install"
+ class_option :skip_bundle, type: :boolean, default: false,
+ desc: "Don't run bundle install"
- class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
- :desc => "Skip Git ignores and keeps"
+ class_option :skip_git, type: :boolean, aliases: '-G', default: false,
+ desc: 'Skip .gitignore file'
- class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
- :desc => "Skip Active Record files"
+ class_option :skip_keeps, type: :boolean, default: false,
+ desc: 'Skip source control .keep files'
- class_option :skip_sprockets, :type => :boolean, :aliases => "-S", :default => false,
- :desc => "Skip Sprockets files"
+ class_option :skip_active_record, type: :boolean, aliases: '-O', default: false,
+ desc: 'Skip Active Record files'
- class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
- :desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"
+ class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false,
+ desc: 'Skip Sprockets files'
- class_option :javascript, :type => :string, :aliases => '-j', :default => 'jquery',
- :desc => 'Preconfigure for selected JavaScript library'
+ class_option :database, type: :string, aliases: '-d', default: 'sqlite3',
+ desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})"
- class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false,
- :desc => "Skip JavaScript files"
+ class_option :javascript, type: :string, aliases: '-j', default: 'jquery',
+ desc: 'Preconfigure for selected JavaScript library'
- class_option :skip_index_html, :type => :boolean, :aliases => "-I", :default => false,
- :desc => "Skip public/index.html and app/assets/images/rails.png files"
+ class_option :skip_javascript, type: :boolean, aliases: '-J', default: false,
+ desc: 'Skip JavaScript files'
- class_option :dev, :type => :boolean, :default => false,
- :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout"
+ class_option :skip_index_html, type: :boolean, aliases: '-I', default: false,
+ desc: 'Skip public/index.html and app/assets/images/rails.png files'
- class_option :edge, :type => :boolean, :default => false,
- :desc => "Setup the #{name} with Gemfile pointing to Rails repository"
+ class_option :dev, type: :boolean, default: false,
+ desc: "Setup the #{name} with Gemfile pointing to your Rails checkout"
- class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false,
- :desc => "Skip Test::Unit files"
+ class_option :edge, type: :boolean, default: false,
+ desc: "Setup the #{name} with Gemfile pointing to Rails repository"
- class_option :help, :type => :boolean, :aliases => "-h", :group => :rails,
- :desc => "Show this help message and quit"
+ class_option :skip_test_unit, type: :boolean, aliases: '-T', default: false,
+ desc: 'Skip Test::Unit files'
+
+ class_option :help, type: :boolean, aliases: '-h', group: :rails,
+ desc: 'Show this help message and quit'
end
def initialize(*args)
@@ -261,13 +264,13 @@ module Rails
bundle_command('install') unless options[:skip_gemfile] || options[:skip_bundle] || options[:pretend]
end
- def empty_directory_with_gitkeep(destination, config = {})
+ def empty_directory_with_keep_file(destination, config = {})
empty_directory(destination, config)
- git_keep(destination)
+ keep_file(destination)
end
- def git_keep(destination)
- create_file("#{destination}/.gitkeep") unless options[:skip_git]
+ def keep_file(destination)
+ create_file("#{destination}/.keep") unless options[:skip_keeps]
end
end
end
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index c06b0f8994..b71b16b043 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -11,7 +11,7 @@ module Rails
private
%w(template copy_file directory empty_directory inside
- empty_directory_with_gitkeep create_file chmod shebang).each do |method|
+ empty_directory_with_keep_file create_file chmod shebang).each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
@generator.send(:#{method}, *args, &block)
@@ -55,8 +55,8 @@ module Rails
def app
directory 'app'
- git_keep 'app/mailers'
- git_keep 'app/models'
+ keep_file 'app/mailers'
+ keep_file 'app/models'
end
def config
@@ -86,13 +86,13 @@ module Rails
end
def lib
- empty_directory "lib"
- empty_directory_with_gitkeep "lib/tasks"
- empty_directory_with_gitkeep "lib/assets"
+ empty_directory 'lib'
+ empty_directory_with_keep_file 'lib/tasks'
+ empty_directory_with_keep_file 'lib/assets'
end
def log
- empty_directory_with_gitkeep "log"
+ empty_directory_with_keep_file 'log'
end
def public_directory
@@ -100,7 +100,7 @@ module Rails
if options[:skip_index_html]
remove_file "public/index.html"
remove_file 'app/assets/images/rails.png'
- git_keep 'app/assets/images'
+ keep_file 'app/assets/images'
end
end
@@ -112,13 +112,13 @@ module Rails
end
def test
- empty_directory_with_gitkeep "test/fixtures"
- empty_directory_with_gitkeep "test/functional"
- empty_directory_with_gitkeep "test/integration"
- empty_directory_with_gitkeep "test/unit"
+ empty_directory_with_keep_file 'test/fixtures'
+ empty_directory_with_keep_file 'test/functional'
+ empty_directory_with_keep_file 'test/integration'
+ empty_directory_with_keep_file 'test/unit'
- template "test/performance/browsing_test.rb"
- template "test/test_helper.rb"
+ template 'test/performance/browsing_test.rb'
+ template 'test/test_helper.rb'
end
def tmp
@@ -132,11 +132,11 @@ module Rails
end
def vendor_javascripts
- empty_directory_with_gitkeep "vendor/assets/javascripts"
+ empty_directory_with_keep_file 'vendor/assets/javascripts'
end
def vendor_stylesheets
- empty_directory_with_gitkeep "vendor/assets/stylesheets"
+ empty_directory_with_keep_file 'vendor/assets/stylesheets'
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
index 6c0ef31725..d83690e1b9 100644
--- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
- # For APIs, you may want to use :reset_session instead.
+ # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb
index 1ac0248bcf..f7d8f718de 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/application.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb
@@ -46,11 +46,6 @@ module <%= app_const_base %>
# like if you have constraints or database-specific column types.
# config.active_record.schema_format = :sql
- # Enforce whitelist mode for mass assignment.
- # This will create an empty whitelist of attributes available for mass-assignment for all models
- # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
- # parameters by using an attr_accessible or attr_protected declaration.
- <%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true
<% unless options.skip_sprockets? -%>
# Enable the asset pipeline.
@@ -59,8 +54,5 @@ module <%= app_const_base %>
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
<% end -%>
-
- # Enable app-wide asynchronous ActionMailer.
- # config.action_mailer.async = true
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
index 122e7e2b34..6b5b3a0b1f 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
@@ -23,9 +23,6 @@
config.action_dispatch.best_standards_support = :builtin
<%- unless options.skip_active_record? -%>
- # Raise exception on mass assignment protection for Active Record models.
- config.active_record.mass_assignment_sanitizer = :strict
-
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
config.active_record.auto_explain_threshold_in_seconds = 0.5
@@ -41,7 +38,4 @@
# Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
<%- end -%>
-
- # In development, use an in-memory queue for queueing.
- config.queue = Rails::Queueing::Queue
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
index a627636089..cb3e8b123e 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
@@ -80,7 +80,7 @@
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
- # Default the production mode queue to an in-memory queue. You will probably
+ # Default the production mode queue to an synchronous queue. You will probably
# want to replace this with an out-of-process queueing solution.
- config.queue = Rails::Queueing::Queue
+ # config.queue = ActiveSupport::SynchronousQueue
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
index 8ab27eb6e1..202fc98adf 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
@@ -31,14 +31,9 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
- <%- unless options.skip_active_record? -%>
- # Raise exception on mass assignment protection for Active Record models.
- config.active_record.mass_assignment_sanitizer = :strict
- <%- end -%>
-
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Use the testing queue.
- config.queue = Rails::Queueing::TestQueue
+ config.queue = ActiveSupport::TestQueue
end
diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
index 4f937ad65a..c77b3450a3 100644
--- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
+++ b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
@@ -10,15 +10,15 @@ module Rails
def app
if mountable?
- directory "app"
- empty_directory_with_gitkeep "app/assets/images/#{name}"
+ directory 'app'
+ empty_directory_with_keep_file "app/assets/images/#{name}"
elsif full?
- empty_directory_with_gitkeep "app/models"
- empty_directory_with_gitkeep "app/controllers"
- empty_directory_with_gitkeep "app/views"
- empty_directory_with_gitkeep "app/helpers"
- empty_directory_with_gitkeep "app/mailers"
- empty_directory_with_gitkeep "app/assets/images/#{name}"
+ empty_directory_with_keep_file 'app/models'
+ empty_directory_with_keep_file 'app/controllers'
+ empty_directory_with_keep_file 'app/views'
+ empty_directory_with_keep_file 'app/helpers'
+ empty_directory_with_keep_file 'app/mailers'
+ empty_directory_with_keep_file "app/assets/images/#{name}"
end
end
@@ -110,7 +110,7 @@ task :default => :test
copy_file "#{app_templates_dir}/app/assets/stylesheets/application.css",
"app/assets/stylesheets/#{name}/application.css"
elsif full?
- empty_directory_with_gitkeep "app/assets/stylesheets/#{name}"
+ empty_directory_with_keep_file "app/assets/stylesheets/#{name}"
end
end
@@ -121,7 +121,7 @@ task :default => :test
template "#{app_templates_dir}/app/assets/javascripts/application.js.tt",
"app/assets/javascripts/#{name}/application.js"
elsif full?
- empty_directory_with_gitkeep "app/assets/javascripts/#{name}"
+ empty_directory_with_keep_file "app/assets/javascripts/#{name}"
end
end
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
index 0618b16984..f30ad6e20d 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -10,6 +10,8 @@ module Rails
class_option :orm, :banner => "NAME", :type => :string, :required => true,
:desc => "ORM to generate the controller for"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
def create_controller_files
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
index b3e74f9b02..5d038d20e7 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -45,7 +45,7 @@ class <%= controller_class_name %>Controller < ApplicationController
# POST <%= route_url %>
# POST <%= route_url %>.json
def create
- @<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
respond_to do |format|
if @<%= orm_instance.save %>
@@ -64,7 +64,7 @@ class <%= controller_class_name %>Controller < ApplicationController
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
- if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
+ if @<%= orm_instance.update_attributes("#{singular_table_name}_params") %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
format.json { head :no_content }
else
@@ -85,5 +85,17 @@ class <%= controller_class_name %>Controller < ApplicationController
format.json { head :no_content }
end
end
+
+ private
+
+ # Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age)
+ # Also, you can specialize this method with per-user checking of permissible attributes.
+ def <%= "#{singular_table_name}_params" %>
+ <%- if attributes.empty? -%>
+ params[<%= ":#{singular_table_name}" %>]
+ <%- else -%>
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>)
+ <%- end -%>
+ end
end
<% end -%>
diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb
deleted file mode 100644
index baf6811d3e..0000000000
--- a/railties/lib/rails/queueing.rb
+++ /dev/null
@@ -1,107 +0,0 @@
-require "thread"
-require 'delegate'
-
-module Rails
- module Queueing
- # A container for multiple queues. This class delegates to a default Queue
- # so that <tt>Rails.queue.push</tt> and friends will Just Work. To use this class
- # with multiple queues:
- #
- # # In your configuration:
- # Rails.queue[:image_queue] = SomeQueue.new
- # Rails.queue[:mail_queue] = SomeQueue.new
- #
- # # In your app code:
- # Rails.queue[:mail_queue].push SomeJob.new
- #
- class Container < DelegateClass(::Queue)
- def initialize(default_queue)
- @queues = { :default => default_queue }
- super(default_queue)
- end
-
- def [](queue_name)
- @queues[queue_name]
- end
-
- def []=(queue_name, queue)
- @queues[queue_name] = queue
- end
- end
-
- # A Queue that simply inherits from STDLIB's Queue. Everytime this
- # queue is used, Rails automatically sets up a ThreadedConsumer
- # to consume it.
- class Queue < ::Queue
- end
-
- # In test mode, the Rails queue is backed by an Array so that assertions
- # can be made about its contents. The test queue provides a +jobs+
- # method to make assertions about the queue's contents and a +drain+
- # method to drain the queue and run the jobs.
- #
- # 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
- # available on production queues.
- def jobs
- @que.dup
- end
-
- # Marshal and unmarshal job before pushing it onto the queue. This will
- # raise an exception on any attempts in tests to push jobs that can't (or
- # shouldn't) be marshalled.
- def push(job)
- super Marshal.load(Marshal.dump(job))
- end
-
- # 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
- # jobs are caught in test mode.
- Thread.new { pop.run until empty? }.join
- end
- end
-
- # The threaded consumer will run jobs in a background thread in
- # development mode or in a VM where running jobs on a thread in
- # production mode makes sense.
- #
- # When the process exits, the consumer pushes a nil onto the
- # queue and joins the thread, which will ensure that all jobs
- # are executed before the process finally dies.
- class ThreadedConsumer
- def self.start(queue)
- new(queue).start
- end
-
- def initialize(queue)
- @queue = queue
- end
-
- def start
- @thread = Thread.new do
- while job = @queue.pop
- begin
- job.run
- rescue Exception => e
- handle_exception e
- end
- end
- end
- self
- end
-
- def shutdown
- @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