From e3f422cfa6fa4841f005cbc43504fc7a8948f093 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 6 Dec 2024 06:56:09 -0500 Subject: [PATCH] Streamline misconfiguration detection and error messaging --- lib/solid_queue/configuration.rb | 20 +++++++++++++++++--- lib/solid_queue/supervisor.rb | 22 +++------------------- test/unit/configuration_test.rb | 2 -- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/solid_queue/configuration.rb b/lib/solid_queue/configuration.rb index dd72ea9d..c317e71e 100644 --- a/lib/solid_queue/configuration.rb +++ b/lib/solid_queue/configuration.rb @@ -37,11 +37,21 @@ def configured_processes end def valid? - skip_recurring_tasks? || recurring_tasks.none? || recurring_tasks.all?(&:valid?) + configured_processes.any? && (skip_recurring_tasks? || invalid_tasks.none?) end - def invalid_tasks - recurring_tasks.select(&:invalid?) + def error_messages + if configured_processes.none? + "No workers or processed configured. Exiting..." + else + error_messages = invalid_tasks.map do |task| + all_messages = task.errors.full_messages.map { |msg| "\t#{msg}" }.join("\n") + "#{task.key}:\n#{all_messages}" + end + .join("\n") + + "Invalid processes configured:\n#{error_messages}" + end end def max_number_of_threads @@ -62,6 +72,10 @@ def default_options } end + def invalid_tasks + recurring_tasks.select(&:invalid?) + end + def only_work? options[:only_work] end diff --git a/lib/solid_queue/supervisor.rb b/lib/solid_queue/supervisor.rb index e50cbb61..d2b11f22 100644 --- a/lib/solid_queue/supervisor.rb +++ b/lib/solid_queue/supervisor.rb @@ -10,28 +10,12 @@ def start(**options) SolidQueue.supervisor = true configuration = Configuration.new(**options) - if configuration.configured_processes.any? - if configuration.valid? - new(configuration).tap(&:start) - else - abort_due_to_invalid_tasks(configuration) - end + if configuration.valid? + new(configuration).tap(&:start) else - abort "No workers or processed configured. Exiting..." + abort configuration.error_messages end end - - private - def abort_due_to_invalid_tasks(configuration) - error_messages = configuration.invalid_tasks - .map do |task| - all_messages = task.errors.full_messages.map { |msg| "\t#{msg}" }.join("\n") - "#{task.key}:\n#{all_messages}" - end - .join("\n") - - abort "Invalid processes configured:\n#{error_messages}" - end end def initialize(configuration) diff --git a/test/unit/configuration_test.rb b/test/unit/configuration_test.rb index 00733ed6..73442d6e 100644 --- a/test/unit/configuration_test.rb +++ b/test/unit/configuration_test.rb @@ -94,8 +94,6 @@ class ConfigurationTest < ActiveSupport::TestCase configuration = SolidQueue::Configuration.new(recurring_schedule_file: config_file_path(:recurring_with_invalid)) assert_not configuration.valid? - - assert_equal configuration.invalid_tasks.size, 1 end test "is valid when there are no recurring tasks" do