Skip to content

Commit

Permalink
Error When an Invalid Recurring Task is Configured
Browse files Browse the repository at this point in the history
Resolves #414
  • Loading branch information
jherdman committed Nov 26, 2024
1 parent 6b32e17 commit 3bf4dd1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/solid_queue/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ def run
end

def schedule_recurring_tasks
recurring_schedule.schedule_tasks
if recurring_schedule.valid?
recurring_schedule.schedule_tasks
else
error_messages = recurring_schedule.invalid_tasks.map do |task|
"#{task.key}: #{task.errors.full_messages.to_sentence}"
end.join(", ")

raise "Invalid recurring tasks: #{error_messages}"
end
end

def unschedule_recurring_tasks
Expand Down
8 changes: 6 additions & 2 deletions lib/solid_queue/scheduler/recurring_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ module SolidQueue
class Scheduler::RecurringSchedule
include AppExecutor

attr_reader :configured_tasks, :scheduled_tasks
attr_reader :configured_tasks, :invalid_tasks, :scheduled_tasks

def initialize(tasks)
@configured_tasks = Array(tasks).map { |task| SolidQueue::RecurringTask.wrap(task) }.select(&:valid?)
@configured_tasks, @invalid_tasks = Array(tasks).map { |task| SolidQueue::RecurringTask.wrap(task) }.partition(&:valid?)
@scheduled_tasks = Concurrent::Hash.new
end

def empty?
configured_tasks.empty?
end

def valid?
invalid_tasks.empty?
end

def schedule_tasks
wrap_in_app_executor do
persist_tasks
Expand Down
10 changes: 10 additions & 0 deletions test/unit/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class SchedulerTest < ActiveSupport::TestCase
scheduler.stop
end

test "recurring schedule has an invalid task" do
invalid_recurring_tasks = { example_task: { class: "BarBarBarBarJob", schedule: "every hour", args: 42 } }

error = assert_raises(RuntimeError) do
SolidQueue::Scheduler.new(recurring_tasks: invalid_recurring_tasks).tap(&:start)
end

assert_equal "Invalid recurring tasks: example_task: Class name doesn't correspond to an existing class", error.message
end

test "run more than one instance of the scheduler with recurring tasks" do
recurring_tasks = { example_task: { class: "AddToBufferJob", schedule: "every second", args: 42 } }
schedulers = 2.times.collect do
Expand Down

0 comments on commit 3bf4dd1

Please sign in to comment.