Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error When an Invalid Recurring Task is Configured #427

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that if you have a record written in your DB, and then it becomes invalid this will error out. E.g. your record has a legacy class name. This would prevent SQ from booting, which might be undesirable. Perhaps it's best to log the errors instead?

"#{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