Module: Delayed::Settings

Defined in:
lib/delayed/settings.rb

Constant Summary collapse

SETTINGS =
[
  :default_job_options,
  :disable_abandoned_job_cleanup,
  :disable_periodic_jobs,
  :disable_automatic_orphan_unlocking,
  :fetch_batch_size,
  :kill_workers_on_exit,
  :last_ditch_logfile,
  :max_attempts,
  :pool_procname_suffix,
  :queue,
  :select_random_from_batch,
  :silence_periodic_log,
  :sleep_delay,
  :sleep_delay_stagger,
  :slow_exit_timeout,
  :worker_health_check_type,
  :worker_health_check_config,
  :worker_procname_prefix,
]
SETTINGS_WITH_ARGS =
[
  :job_detailed_log_format,
  :num_strands
]
PARENT_PROCESS_DEFAULTS =
{
  server_socket_timeout: 10.0,
  prefetched_jobs_timeout: 30.0,

  client_connect_timeout: 2.0,

  # We'll accept a partial, relative path and assume we want it inside
  # Rails.root with inst-jobs.sock appended if provided a directory.
  server_address: 'tmp',
}.with_indifferent_access.freeze
@@parent_process =
PARENT_PROCESS_DEFAULTS.dup

Class Method Summary collapse

Class Method Details

.apply_worker_config!(config) ⇒ Object


102
103
104
105
106
107
108
# File 'lib/delayed/settings.rb', line 102

def self.apply_worker_config!(config)
  SETTINGS.each do |setting|
    self.send("#{setting}=", config[setting.to_s]) if config.key?(setting.to_s)
  end
  parent_process.client_timeout = config['parent_process_client_timeout'] if config.key?('parent_process_client_timeout')
  self.parent_process = config['parent_process'] if config.key?('parent_process')
end

.default_worker_config_nameObject


110
111
112
# File 'lib/delayed/settings.rb', line 110

def self.default_worker_config_name
  expand_rails_path("config/delayed_jobs.yml")
end

.expand_rails_path(path) ⇒ Object

Expands rails-relative paths, without depending on rails being loaded.


115
116
117
118
119
120
121
122
# File 'lib/delayed/settings.rb', line 115

def self.expand_rails_path(path)
  root = if defined?(Rails) && Rails.root
    (Rails.root+"Gemfile").to_s
  else
    ENV.fetch('BUNDLE_GEMFILE', Dir.pwd+"/Gemfile")
  end
  File.expand_path("../#{path}", root)
end

.parent_process=(new_config) ⇒ Object


128
129
130
131
# File 'lib/delayed/settings.rb', line 128

def self.parent_process=(new_config)
  raise 'Parent process configurations must be a hash!' unless Hash === new_config
  @@parent_process = PARENT_PROCESS_DEFAULTS.merge(new_config)
end

.parent_process_client_timeout=(val) ⇒ Object


124
125
126
# File 'lib/delayed/settings.rb', line 124

def self.parent_process_client_timeout=(val)
  parent_process['server_socket_timeout'] = Integer(val)
end

.queue=(queue_name) ⇒ Object

Raises:

  • (ArgumentError)

59
60
61
62
# File 'lib/delayed/settings.rb', line 59

def self.queue=(queue_name)
  raise(ArgumentError, "queue_name must not be blank") if queue_name.blank?
  @@queue = queue_name
end

.worker_config(config_filename = nil) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/delayed/settings.rb', line 84

def self.worker_config(config_filename = nil)
  config_filename ||= default_worker_config_name
  config = YAML.load(ERB.new(File.read(config_filename)).result)
  env = defined?(RAILS_ENV) ? RAILS_ENV : ENV['RAILS_ENV'] || 'development'
  config = config[env] || config['default']
  # Backwards compatibility from when the config was just an array of queues
  config = { :workers => config } if config.is_a?(Array)
  unless config && config.is_a?(Hash)
    raise ArgumentError,
      "Invalid config file #{config_filename}"
  end
  config = config.with_indifferent_access
  config[:workers].map! do |worker_config|
    config.except(:workers).merge(worker_config.with_indifferent_access)
  end
  config
end

.worker_health_check_config=(new_config) ⇒ Object


133
134
135
# File 'lib/delayed/settings.rb', line 133

def self.worker_health_check_config=(new_config)
  @@worker_health_check_config = (new_config || {}).with_indifferent_access
end