Skip to content

Commit

Permalink
Use default configuration when the one given is empty
Browse files Browse the repository at this point in the history
Which happens for example when you have solid queue configuration for an
environment but not for others, so you can't find the `workers` and
`dispatchers` keys.
  • Loading branch information
rosa committed Aug 24, 2024
1 parent d708605 commit b9b41db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/solid_queue/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def instantiate
recurring_tasks: []
}

DEFAULT_CONFIG = {
workers: [ WORKER_DEFAULTS ],
dispatchers: [ DISPATCHER_DEFAULTS ]
}

def initialize(mode: :fork, load_from: nil)
@mode = mode.to_s.inquiry
@raw_config = config_from(load_from)
Expand Down Expand Up @@ -61,22 +66,27 @@ def dispatchers
end

def config_from(file_or_hash, env: Rails.env)
config = load_config_from(file_or_hash)
config[env.to_sym] ? config[env.to_sym] : config
load_config_from(file_or_hash).then do |config|
config = config[env.to_sym] ? config[env.to_sym] : config
if (config.keys & DEFAULT_CONFIG.keys).any? then config
else
DEFAULT_CONFIG
end
end
end

def workers_options
@workers_options ||= options_from_raw_config(:workers, WORKER_DEFAULTS)
@workers_options ||= options_from_raw_config(:workers)
.map { |options| options.dup.symbolize_keys }
end

def dispatchers_options
@dispatchers_options ||= options_from_raw_config(:dispatchers, DISPATCHER_DEFAULTS)
@dispatchers_options ||= options_from_raw_config(:dispatchers)
.map { |options| options.dup.symbolize_keys }
end

def options_from_raw_config(key, defaults)
raw_config.empty? ? [ defaults ] : Array(raw_config[key])
def options_from_raw_config(key)
Array(raw_config[key])
end

def parse_recurring_tasks(tasks)
Expand Down
8 changes: 8 additions & 0 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
end

test "default configuration when config given doesn't include any configuration" do
configuration = SolidQueue::Configuration.new(load_from: { random_wrong_key: :random_value })

assert_equal 2, configuration.configured_processes.count
assert_processes configuration, :worker, 1, queues: [ "*" ]
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
end

test "read configuration from default file" do
configuration = SolidQueue::Configuration.new
assert 3, configuration.configured_processes.count
Expand Down

0 comments on commit b9b41db

Please sign in to comment.