class GoodJob::Configuration
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.
Constants
- DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO
Default number of seconds to preserve jobs for {CLI#cleanup_preserved_jobs} and {GoodJob.cleanup_preserved_jobs}
- DEFAULT_DEVELOPMENT_ASYNC_POLL_INTERVAL
Default poll interval for async in development environment
- DEFAULT_ENABLE_CRON
Default to not running cron
- DEFAULT_MAX_CACHE
Default number of threads to use per {Scheduler}
- DEFAULT_MAX_THREADS
Default number of threads to use per {Scheduler}
- DEFAULT_POLL_INTERVAL
Default number of seconds between polls for jobs
- DEFAULT_SHUTDOWN_TIMEOUT
Default to always wait for jobs to finish for {Adapter#shutdown}
- EXECUTION_MODES
Valid execution modes.
Attributes
The environment from which to read GoodJob's environment variables. By default, this is the current process's environment, but it can be set to something else in {#initialize}. @return [Hash]
The options that were explicitly set when initializing Configuration
. @return [Hash]
Public Class Methods
@param options [Hash] Any explicitly specified configuration options to
use. Keys are symbols that match the various methods on this class.
@param env [Hash] A Hash
from which to read environment variables that
might specify additional configuration values.
# File lib/good_job/configuration.rb 40 def initialize(options, env: ENV) 41 @options = options 42 @env = env 43 end
Public Instance Methods
Number of seconds to preserve jobs when using the +good_job cleanup_preserved_jobs+ CLI
command. This configuration is only used when {GoodJob.preserve_job_records} is true
. @return [Integer]
# File lib/good_job/configuration.rb 171 def cleanup_preserved_jobs_before_seconds_ago 172 ( 173 options[:before_seconds_ago] || 174 rails_config[:cleanup_preserved_jobs_before_seconds_ago] || 175 env['GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO'] || 176 DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO 177 ).to_i 178 end
# File lib/good_job/configuration.rb 159 def cron 160 env_cron = JSON.parse(ENV['GOOD_JOB_CRON']) if ENV['GOOD_JOB_CRON'].present? 161 162 options[:cron] || 163 rails_config[:cron] || 164 env_cron || 165 {} 166 end
Tests whether to daemonize the process. @return [Boolean]
# File lib/good_job/configuration.rb 182 def daemonize? 183 options[:daemonize] || false 184 end
Whether to run cron @return [Boolean]
# File lib/good_job/configuration.rb 147 def enable_cron 148 value = ActiveModel::Type::Boolean.new.cast( 149 options[:enable_cron] || 150 rails_config[:enable_cron] || 151 env['GOOD_JOB_ENABLE_CRON'] || 152 false 153 ) 154 value && cron.size.positive? 155 end
Specifies how and where jobs should be executed. See {Adapter#initialize} for more details on possible values. @return [Symbol]
# File lib/good_job/configuration.rb 52 def execution_mode 53 @_execution_mode ||= begin 54 mode = if GoodJob::CLI.within_exe? 55 :external 56 else 57 options[:execution_mode] || 58 rails_config[:execution_mode] || 59 env['GOOD_JOB_EXECUTION_MODE'] 60 end 61 62 if mode 63 mode.to_sym 64 elsif Rails.env.development? 65 :async 66 elsif Rails.env.test? 67 :inline 68 else 69 :external 70 end 71 end 72 end
The maximum number of future-scheduled jobs to store in memory. Storing future-scheduled jobs in memory reduces execution latency at the cost of increased memory usage. 10,000 stored jobs = ~20MB. @return [Integer]
# File lib/good_job/configuration.rb 124 def max_cache 125 ( 126 options[:max_cache] || 127 rails_config[:max_cache] || 128 env['GOOD_JOB_MAX_CACHE'] || 129 DEFAULT_MAX_CACHE 130 ).to_i 131 end
Indicates the number of threads to use per {Scheduler}. Note that {#queue_string} may provide more specific thread counts to use with individual schedulers. @return [Integer]
# File lib/good_job/configuration.rb 78 def max_threads 79 ( 80 options[:max_threads] || 81 rails_config[:max_threads] || 82 env['GOOD_JOB_MAX_THREADS'] || 83 env['RAILS_MAX_THREADS'] || 84 DEFAULT_MAX_THREADS 85 ).to_i 86 end
Path of the pidfile to create when running as a daemon. @return [Pathname,String]
# File lib/good_job/configuration.rb 188 def pidfile 189 options[:pidfile] || 190 env['GOOD_JOB_PIDFILE'] || 191 Rails.application.root.join('tmp', 'pids', 'good_job.pid') 192 end
The number of seconds between polls for jobs. GoodJob
will execute jobs on queues continuously until a queue is empty, at which point it will poll (using this interval) for new queued jobs to execute. @return [Integer]
# File lib/good_job/configuration.rb 104 def poll_interval 105 interval = ( 106 options[:poll_interval] || 107 rails_config[:poll_interval] || 108 env['GOOD_JOB_POLL_INTERVAL'] 109 ) 110 111 if interval 112 interval.to_i 113 elsif Rails.env.development? && execution_mode.in?([:async, :async_all, :async_server]) 114 DEFAULT_DEVELOPMENT_ASYNC_POLL_INTERVAL 115 else 116 DEFAULT_POLL_INTERVAL 117 end 118 end
Describes which queues to execute jobs from and how those queues should be grouped into {Scheduler} instances. See {file:README.md#optimize-queues-threads-and-processes} for more details on the format of this string. @return [String]
# File lib/good_job/configuration.rb 93 def queue_string 94 options[:queues].presence || 95 rails_config[:queues].presence || 96 env['GOOD_JOB_QUEUES'].presence || 97 '*' 98 end
The number of seconds to wait for jobs to finish when shutting down before stopping the thread. -1
is forever. @return [Numeric]
# File lib/good_job/configuration.rb 136 def shutdown_timeout 137 ( 138 options[:shutdown_timeout] || 139 rails_config[:shutdown_timeout] || 140 env['GOOD_JOB_SHUTDOWN_TIMEOUT'] || 141 DEFAULT_SHUTDOWN_TIMEOUT 142 ).to_f 143 end
# File lib/good_job/configuration.rb 45 def validate! 46 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) 47 end
Private Instance Methods
# File lib/good_job/configuration.rb 196 def rails_config 197 Rails.application.config.good_job 198 end