Class: GoodJob::Configuration
- Inherits:
-
Object
- Object
- GoodJob::Configuration
- Defined in:
- lib/good_job/configuration.rb
Overview
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.
Constant Summary collapse
- EXECUTION_MODES =
Valid execution modes.
[:async, :async_all, :async_server, :external, :inline].freeze
- DEFAULT_MAX_THREADS =
Default number of threads to use per Scheduler
5
- DEFAULT_POLL_INTERVAL =
Default number of seconds between polls for jobs
10
- DEFAULT_DEVELOPMENT_ASYNC_POLL_INTERVAL =
Default poll interval for async in development environment
-1 # Default number of threads to use per {Scheduler}
- DEFAULT_MAX_CACHE =
Default number of threads to use per Scheduler
10000
- DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO =
Default number of seconds to preserve jobs for GoodJob::CLI#cleanup_preserved_jobs and GoodJob.cleanup_preserved_jobs
24 * 60 * 60
- DEFAULT_SHUTDOWN_TIMEOUT =
Default to always wait for jobs to finish for Adapter#shutdown
-1 # Default to not running cron
- DEFAULT_ENABLE_CRON =
Default to not running cron
false
Instance Attribute Summary collapse
-
#env ⇒ Hash
readonly
The environment from which to read GoodJob’s environment variables.
-
#options ⇒ Hash
readonly
The options that were explicitly set when initializing
Configuration
.
Instance Method Summary collapse
-
#cleanup_preserved_jobs_before_seconds_ago ⇒ Integer
Number of seconds to preserve jobs when using the good_job cleanup_preserved_jobs CLI command.
- #cron ⇒ Object
-
#daemonize? ⇒ Boolean
Tests whether to daemonize the process.
-
#enable_cron ⇒ Boolean
(also: #enable_cron?)
Whether to run cron.
-
#execution_mode ⇒ Symbol
Specifies how and where jobs should be executed.
-
#initialize(options, env: ENV) ⇒ Configuration
constructor
A new instance of Configuration.
-
#max_cache ⇒ Integer
The maximum number of future-scheduled jobs to store in memory.
-
#max_threads ⇒ Integer
Indicates the number of threads to use per Scheduler.
-
#pidfile ⇒ Pathname, String
Path of the pidfile to create when running as a daemon.
-
#poll_interval ⇒ Integer
The number of seconds between polls for jobs.
-
#queue_string ⇒ String
Describes which queues to execute jobs from and how those queues should be grouped into Scheduler instances.
-
#shutdown_timeout ⇒ Numeric
The number of seconds to wait for jobs to finish when shutting down before stopping the thread.
- #validate! ⇒ Object
Constructor Details
#initialize(options, env: ENV) ⇒ Configuration
Returns a new instance of Configuration.
40 41 42 43 |
# File 'lib/good_job/configuration.rb', line 40 def initialize(, env: ENV) = @env = env end |
Instance Attribute Details
#env ⇒ Hash (readonly)
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.
34 35 36 |
# File 'lib/good_job/configuration.rb', line 34 def env @env end |
#options ⇒ Hash (readonly)
The options that were explicitly set when initializing Configuration
.
28 29 30 |
# File 'lib/good_job/configuration.rb', line 28 def end |
Instance Method Details
#cleanup_preserved_jobs_before_seconds_ago ⇒ Integer
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
.
171 172 173 174 175 176 177 178 |
# File 'lib/good_job/configuration.rb', line 171 def cleanup_preserved_jobs_before_seconds_ago ( [:before_seconds_ago] || rails_config[:cleanup_preserved_jobs_before_seconds_ago] || env['GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO'] || DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO ).to_i end |
#cron ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/good_job/configuration.rb', line 159 def cron env_cron = JSON.parse(ENV['GOOD_JOB_CRON']) if ENV['GOOD_JOB_CRON'].present? [:cron] || rails_config[:cron] || env_cron || {} end |
#daemonize? ⇒ Boolean
Tests whether to daemonize the process.
182 183 184 |
# File 'lib/good_job/configuration.rb', line 182 def daemonize? [:daemonize] || false end |
#enable_cron ⇒ Boolean Also known as: enable_cron?
Whether to run cron
147 148 149 150 151 152 153 154 155 |
# File 'lib/good_job/configuration.rb', line 147 def enable_cron value = ActiveModel::Type::Boolean.new.cast( [:enable_cron] || rails_config[:enable_cron] || env['GOOD_JOB_ENABLE_CRON'] || false ) value && cron.size.positive? end |
#execution_mode ⇒ Symbol
Specifies how and where jobs should be executed. See Adapter#initialize for more details on possible values.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/good_job/configuration.rb', line 52 def execution_mode @_execution_mode ||= begin mode = if GoodJob::CLI.within_exe? :external else [:execution_mode] || rails_config[:execution_mode] || env['GOOD_JOB_EXECUTION_MODE'] end if mode mode.to_sym elsif Rails.env.development? :async elsif Rails.env.test? :inline else :external end end end |
#max_cache ⇒ Integer
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.
124 125 126 127 128 129 130 131 |
# File 'lib/good_job/configuration.rb', line 124 def max_cache ( [:max_cache] || rails_config[:max_cache] || env['GOOD_JOB_MAX_CACHE'] || DEFAULT_MAX_CACHE ).to_i end |
#max_threads ⇒ Integer
Indicates the number of threads to use per Scheduler. Note that #queue_string may provide more specific thread counts to use with individual schedulers.
78 79 80 81 82 83 84 85 86 |
# File 'lib/good_job/configuration.rb', line 78 def max_threads ( [:max_threads] || rails_config[:max_threads] || env['GOOD_JOB_MAX_THREADS'] || env['RAILS_MAX_THREADS'] || DEFAULT_MAX_THREADS ).to_i end |
#pidfile ⇒ Pathname, String
Path of the pidfile to create when running as a daemon.
188 189 190 191 192 |
# File 'lib/good_job/configuration.rb', line 188 def pidfile [:pidfile] || env['GOOD_JOB_PIDFILE'] || Rails.application.root.join('tmp', 'pids', 'good_job.pid') end |
#poll_interval ⇒ Integer
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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/good_job/configuration.rb', line 104 def poll_interval interval = ( [:poll_interval] || rails_config[:poll_interval] || env['GOOD_JOB_POLL_INTERVAL'] ) if interval interval.to_i elsif Rails.env.development? && execution_mode.in?([:async, :async_all, :async_server]) DEFAULT_DEVELOPMENT_ASYNC_POLL_INTERVAL else DEFAULT_POLL_INTERVAL end end |
#queue_string ⇒ String
93 94 95 96 97 98 |
# File 'lib/good_job/configuration.rb', line 93 def queue_string [:queues].presence || rails_config[:queues].presence || env['GOOD_JOB_QUEUES'].presence || '*' end |
#shutdown_timeout ⇒ Numeric
The number of seconds to wait for jobs to finish when shutting down before stopping the thread. -1
is forever.
136 137 138 139 140 141 142 143 |
# File 'lib/good_job/configuration.rb', line 136 def shutdown_timeout ( [:shutdown_timeout] || rails_config[:shutdown_timeout] || env['GOOD_JOB_SHUTDOWN_TIMEOUT'] || DEFAULT_SHUTDOWN_TIMEOUT ).to_f end |
#validate! ⇒ Object
45 46 47 |
# File 'lib/good_job/configuration.rb', line 45 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 |