Skip to content

Commit b9b41db

Browse files
committed
Use default configuration when the one given is empty
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.
1 parent d708605 commit b9b41db

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/solid_queue/configuration.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def instantiate
2323
recurring_tasks: []
2424
}
2525

26+
DEFAULT_CONFIG = {
27+
workers: [ WORKER_DEFAULTS ],
28+
dispatchers: [ DISPATCHER_DEFAULTS ]
29+
}
30+
2631
def initialize(mode: :fork, load_from: nil)
2732
@mode = mode.to_s.inquiry
2833
@raw_config = config_from(load_from)
@@ -61,22 +66,27 @@ def dispatchers
6166
end
6267

6368
def config_from(file_or_hash, env: Rails.env)
64-
config = load_config_from(file_or_hash)
65-
config[env.to_sym] ? config[env.to_sym] : config
69+
load_config_from(file_or_hash).then do |config|
70+
config = config[env.to_sym] ? config[env.to_sym] : config
71+
if (config.keys & DEFAULT_CONFIG.keys).any? then config
72+
else
73+
DEFAULT_CONFIG
74+
end
75+
end
6676
end
6777

6878
def workers_options
69-
@workers_options ||= options_from_raw_config(:workers, WORKER_DEFAULTS)
79+
@workers_options ||= options_from_raw_config(:workers)
7080
.map { |options| options.dup.symbolize_keys }
7181
end
7282

7383
def dispatchers_options
74-
@dispatchers_options ||= options_from_raw_config(:dispatchers, DISPATCHER_DEFAULTS)
84+
@dispatchers_options ||= options_from_raw_config(:dispatchers)
7585
.map { |options| options.dup.symbolize_keys }
7686
end
7787

78-
def options_from_raw_config(key, defaults)
79-
raw_config.empty? ? [ defaults ] : Array(raw_config[key])
88+
def options_from_raw_config(key)
89+
Array(raw_config[key])
8090
end
8191

8292
def parse_recurring_tasks(tasks)

test/unit/configuration_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class ConfigurationTest < ActiveSupport::TestCase
1111
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
1212
end
1313

14+
test "default configuration when config given doesn't include any configuration" do
15+
configuration = SolidQueue::Configuration.new(load_from: { random_wrong_key: :random_value })
16+
17+
assert_equal 2, configuration.configured_processes.count
18+
assert_processes configuration, :worker, 1, queues: [ "*" ]
19+
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
20+
end
21+
1422
test "read configuration from default file" do
1523
configuration = SolidQueue::Configuration.new
1624
assert 3, configuration.configured_processes.count

0 commit comments

Comments
 (0)