lib/good_job/configuration.rb in good_job-3.18.0 vs lib/good_job/configuration.rb in good_job-3.18.1

- old
+ new

@@ -77,35 +77,40 @@ # @param env [Hash] A +Hash+ from which to read environment variables that # might specify additional configuration values. def initialize(options, env: ENV) @options = options @env = env + + @_in_webserver = nil end def validate! self.class.validate_execution_mode(execution_mode) end # Specifies how and where jobs should be executed. See {Adapter#initialize} # for more details on possible values. # @return [Symbol] def execution_mode - mode = if GoodJob::CLI.within_exe? - :external - else - options[:execution_mode] || - rails_config[:execution_mode] || - env['GOOD_JOB_EXECUTION_MODE'] - end + mode = options[:execution_mode] || + rails_config[:execution_mode] || + env['GOOD_JOB_EXECUTION_MODE'] + mode = mode.to_sym if mode if mode - mode.to_sym + if GoodJob::CLI.within_exe? && [:async, :async_server].include?(mode) + :external + else + mode + end + elsif GoodJob::CLI.within_exe? + :external elsif Rails.env.development? :async elsif Rails.env.test? :inline - else + else # rubocop:disable Lint/DuplicateBranch :external end end # Indicates the number of threads to use per {Scheduler}. Note that @@ -171,11 +176,11 @@ ).to_i end # The number of seconds to wait for jobs to finish when shutting down # before stopping the thread. +-1+ is forever. - # @return [Numeric] + # @return [Float] def shutdown_timeout ( options[:shutdown_timeout] || rails_config[:shutdown_timeout] || env['GOOD_JOB_SHUTDOWN_TIMEOUT'] || @@ -243,11 +248,11 @@ ).to_i end # Number of jobs a {Scheduler} will execute before automatically cleaning up preserved jobs. # Positive values will clean up after that many jobs have run, false or 0 will disable, and -1 will clean up after every job. - # @return [Integer, nil] + # @return [Integer, Boolean, nil] def cleanup_interval_jobs if rails_config.key?(:cleanup_interval_jobs) value = rails_config[:cleanup_interval_jobs] if value.nil? GoodJob.deprecator.warn( @@ -345,9 +350,23 @@ rails_config[:smaller_number_is_higher_priority] end def dashboard_default_locale rails_config[:dashboard_default_locale] || DEFAULT_DASHBOARD_DEFAULT_LOCALE + end + + # Whether running in a web server process. + # @return [Boolean, nil] + def in_webserver? + return @_in_webserver unless @_in_webserver.nil? + + @_in_webserver = Rails.const_defined?(:Server) || begin + self_caller = caller + self_caller.grep(%r{config.ru}).any? || # EXAMPLE: config.ru:3:in `block in <main>' OR config.ru:3:in `new_from_string' + self_caller.grep(%r{puma/request}).any? || # EXAMPLE: puma-5.6.4/lib/puma/request.rb:76:in `handle_request' + self_caller.grep(%{/rack/handler/}).any? || # EXAMPLE: iodine-0.7.44/lib/rack/handler/iodine.rb:13:in `start' + (Concurrent.on_jruby? && self_caller.grep(%r{jruby/rack/rails_booter}).any?) # EXAMPLE: uri:classloader:/jruby/rack/rails_booter.rb:83:in `load_environment' + end || false end private def rails_config