Class: GoodJob::Configuration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options, env: ENV) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • options (Hash)

    Any explicitly specified configuration options to use. Keys are symbols that match the various methods on this class.

  • env (Hash) (defaults to: ENV)

    A Hash from which to read environment variables that might specify additional configuration values.



40
41
42
43
# File 'lib/good_job/configuration.rb', line 40

def initialize(options, env: ENV)
  @options = options
  @env = env
end

Instance Attribute Details

#envHash (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.

Returns:

  • (Hash)


34
35
36
# File 'lib/good_job/configuration.rb', line 34

def env
  @env
end

#optionsHash (readonly)

The options that were explicitly set when initializing Configuration.

Returns:

  • (Hash)


28
29
30
# File 'lib/good_job/configuration.rb', line 28

def options
  @options
end

Instance Method Details

#cleanup_preserved_jobs_before_seconds_agoInteger

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.

Returns:

  • (Integer)


171
172
173
174
175
176
177
178
# File 'lib/good_job/configuration.rb', line 171

def cleanup_preserved_jobs_before_seconds_ago
  (
    options[: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

#cronObject



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?

  options[:cron] ||
    rails_config[:cron] ||
    env_cron ||
    {}
end

#daemonize?Boolean

Tests whether to daemonize the process.

Returns:

  • (Boolean)


182
183
184
# File 'lib/good_job/configuration.rb', line 182

def daemonize?
  options[:daemonize] || false
end

#enable_cronBoolean Also known as: enable_cron?

Whether to run cron

Returns:

  • (Boolean)


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(
    options[:enable_cron] ||
      rails_config[:enable_cron] ||
      env['GOOD_JOB_ENABLE_CRON'] ||
      false
  )
  value && cron.size.positive?
end

#execution_modeSymbol

Specifies how and where jobs should be executed. See Adapter#initialize for more details on possible values.

Returns:

  • (Symbol)


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
             options[: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_cacheInteger

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.

Returns:

  • (Integer)


124
125
126
127
128
129
130
131
# File 'lib/good_job/configuration.rb', line 124

def max_cache
  (
    options[:max_cache] ||
      rails_config[:max_cache] ||
      env['GOOD_JOB_MAX_CACHE'] ||
      DEFAULT_MAX_CACHE
  ).to_i
end

#max_threadsInteger

Indicates the number of threads to use per Scheduler. Note that #queue_string may provide more specific thread counts to use with individual schedulers.

Returns:

  • (Integer)


78
79
80
81
82
83
84
85
86
# File 'lib/good_job/configuration.rb', line 78

def max_threads
  (
    options[:max_threads] ||
      rails_config[:max_threads] ||
      env['GOOD_JOB_MAX_THREADS'] ||
      env['RAILS_MAX_THREADS'] ||
      DEFAULT_MAX_THREADS
  ).to_i
end

#pidfilePathname, String

Path of the pidfile to create when running as a daemon.

Returns:

  • (Pathname, String)


188
189
190
191
192
# File 'lib/good_job/configuration.rb', line 188

def pidfile
  options[:pidfile] ||
    env['GOOD_JOB_PIDFILE'] ||
    Rails.application.root.join('tmp', 'pids', 'good_job.pid')
end

#poll_intervalInteger

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.

Returns:

  • (Integer)


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 = (
    options[: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_stringString

Describes which queues to execute jobs from and how those queues should be grouped into Scheduler instances. See README for more details on the format of this string.

Returns:

  • (String)


93
94
95
96
97
98
# File 'lib/good_job/configuration.rb', line 93

def queue_string
  options[:queues].presence ||
    rails_config[:queues].presence ||
    env['GOOD_JOB_QUEUES'].presence ||
    '*'
end

#shutdown_timeoutNumeric

The number of seconds to wait for jobs to finish when shutting down before stopping the thread. -1 is forever.

Returns:

  • (Numeric)


136
137
138
139
140
141
142
143
# File 'lib/good_job/configuration.rb', line 136

def shutdown_timeout
  (
    options[:shutdown_timeout] ||
      rails_config[:shutdown_timeout] ||
      env['GOOD_JOB_SHUTDOWN_TIMEOUT'] ||
      DEFAULT_SHUTDOWN_TIMEOUT
  ).to_f
end

#validate!Object

Raises:

  • (ArgumentError)


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