lib/good_job/configuration.rb in good_job-1.8.0 vs lib/good_job/configuration.rb in good_job-1.9.0

- old
+ new

@@ -3,19 +3,21 @@ # +GoodJob::Configuration+ provides normalized configuration information to # the rest of GoodJob. It combines environment information with explicitly # set options to get the final values for each option. # class Configuration + # Valid execution modes. + EXECUTION_MODES = [:async, :async_server, :external, :inline].freeze # Default number of threads to use per {Scheduler} DEFAULT_MAX_THREADS = 5 # Default number of seconds between polls for jobs DEFAULT_POLL_INTERVAL = 10 # Default number of threads to use per {Scheduler} DEFAULT_MAX_CACHE = 10000 # Default number of seconds to preserve jobs for {CLI#cleanup_preserved_jobs} DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO = 24 * 60 * 60 - # Default to always wait for jobs to finish for {#shutdown} + # Default to always wait for jobs to finish for {Adapter#shutdown} DEFAULT_SHUTDOWN_TIMEOUT = -1 # The options that were explicitly set when initializing +Configuration+. # @return [Hash] attr_reader :options @@ -33,43 +35,33 @@ def initialize(options, env: ENV) @options = options @env = env end + def validate! + raise ArgumentError, "GoodJob execution mode must be one of #{EXECUTION_MODES.join(', ')}. It was '#{execution_mode}' which is not valid." unless execution_mode.in?(EXECUTION_MODES) + end + # Specifies how and where jobs should be executed. See {Adapter#initialize} # for more details on possible values. - # - # When running inside a Rails app, you may want to use - # {#rails_execution_mode}, which takes the current Rails environment into - # account when determining the final value. - # - # @param default [Symbol] - # Value to use if none was specified in the configuration. # @return [Symbol] - def execution_mode(default: :external) - if defined?(GOOD_JOB_WITHIN_CLI) && GOOD_JOB_WITHIN_CLI - :external - elsif options[:execution_mode] - options[:execution_mode] - elsif rails_config[:execution_mode] - rails_config[:execution_mode] - elsif env['GOOD_JOB_EXECUTION_MODE'].present? - env['GOOD_JOB_EXECUTION_MODE'].to_sym - else - default - end - end + def execution_mode + @_execution_mode ||= begin + mode = if GoodJob::CLI.within_exe? + :external + else + options[:execution_mode] || + rails_config[:execution_mode] || + env['GOOD_JOB_EXECUTION_MODE'] + end - # Like {#execution_mode}, but takes the current Rails environment into - # account (e.g. in the +test+ environment, it falls back to +:inline+). - # @return [Symbol] - def rails_execution_mode - if execution_mode(default: nil) - execution_mode - elsif Rails.env.development? || Rails.env.test? - :inline - else - :external + if mode + mode.to_sym + elsif Rails.env.development? || Rails.env.test? + :inline + else + :external + end end end # Indicates the number of threads to use per {Scheduler}. Note that # {#queue_string} may provide more specific thread counts to use with