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

DEFAULT_MAX_THREADS =

Default number of threads to use per Scheduler

5
DEFAULT_POLL_INTERVAL =

Default number of seconds between polls for jobs

5
DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO =

Default number of seconds to preserve jobs for GoodJob::CLI#cleanup_preserved_jobs

24 * 60 * 60

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.



30
31
32
33
# File 'lib/good_job/configuration.rb', line 30

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

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



24
# File 'lib/good_job/configuration.rb', line 24

attr_reader :options, :env

#optionsHash (readonly)

The options that were explicitly set when initializing Configuration.

Returns:

  • (Hash)


24
25
26
# File 'lib/good_job/configuration.rb', line 24

def options
  @options
end

Instance Method Details

#cleanup_preserved_jobs_before_seconds_agoObject



106
107
108
109
110
111
112
# File 'lib/good_job/configuration.rb', line 106

def cleanup_preserved_jobs_before_seconds_ago
  (
    options[:before_seconds_ago] ||
    env['GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO'] ||
    DEFAULT_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO
  ).to_i
end

#execution_mode(default: :external) ⇒ Symbol

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.

Parameters:

  • default (Symbol) (defaults to: :external)

    Value to use if none was specified in the configuration.

Returns:

  • (Symbol)


45
46
47
48
49
50
51
52
53
# File 'lib/good_job/configuration.rb', line 45

def execution_mode(default: :external)
  if options[:execution_mode]
    options[:execution_mode]
  elsif env['GOOD_JOB_EXECUTION_MODE'].present?
    env['GOOD_JOB_EXECUTION_MODE'].to_sym
  else
    default
  end
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)


74
75
76
77
78
79
80
81
# File 'lib/good_job/configuration.rb', line 74

def max_threads
  (
    options[:max_threads] ||
    env['GOOD_JOB_MAX_THREADS'] ||
    env['RAILS_MAX_THREADS'] ||
    DEFAULT_MAX_THREADS
  ).to_i
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)


98
99
100
101
102
103
104
# File 'lib/good_job/configuration.rb', line 98

def poll_interval
  (
    options[:poll_interval] ||
    env['GOOD_JOB_POLL_INTERVAL'] ||
    DEFAULT_POLL_INTERVAL
  ).to_i
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)


88
89
90
91
92
# File 'lib/good_job/configuration.rb', line 88

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

#rails_execution_modeSymbol

Like #execution_mode, but takes the current Rails environment into account (e.g. in the test environment, it falls back to :inline).

Returns:

  • (Symbol)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/good_job/configuration.rb', line 58

def rails_execution_mode
  if execution_mode(default: nil)
    execution_mode
  elsif Rails.env.development?
    :inline
  elsif Rails.env.test?
    :inline
  else
    :external
  end
end