Skip to content
Closed
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
12 changes: 12 additions & 0 deletions lib/solid_queue/scheduler/recurring_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ def task_keys

def reschedule_dynamic_tasks
wrap_in_app_executor do
previous_tasks = dynamic_tasks.index_by(&:key)
reload_dynamic_tasks
schedule_created_dynamic_tasks
reschedule_updated_dynamic_tasks(previous_tasks)
unschedule_deleted_dynamic_tasks
end
end
Expand All @@ -75,6 +77,16 @@ def schedule_created_dynamic_tasks
end
end

def reschedule_updated_dynamic_tasks(previous_tasks)
dynamic_tasks.each do |task|
previous_task = previous_tasks[task.key]
next if previous_task.nil? || previous_task.updated_at == task.updated_at

scheduled_tasks[task.key]&.cancel
schedule_task(task)
end
end

def unschedule_deleted_dynamic_tasks
(scheduled_tasks.keys - RecurringTask.pluck(:key)).each do |key|
scheduled_tasks[key].cancel
Expand Down
20 changes: 20 additions & 0 deletions test/unit/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ class SchedulerTest < ActiveSupport::TestCase
scheduler&.stop
end

test "picks up updates to dynamic tasks post-start" do
task = SolidQueue::RecurringTask.create!(
key: "updatable_task", static: false, class_name: "AddToBufferJob", schedule: "every hour", arguments: [ 42 ]
)

scheduler = SolidQueue::Scheduler.new(recurring_tasks: {}, dynamic_tasks_enabled: true, polling_interval: 0.1).tap(&:start)

wait_for_registered_processes(1, timeout: 1.second)

task.update!(schedule: "every second")

wait_while_with_timeout(3.seconds) { SolidQueue::Job.count < 1 }

skip_active_record_query_cache do
assert SolidQueue::Job.count >= 1, "Expected the updated schedule to enqueue jobs without a scheduler restart"
end
ensure
scheduler&.stop
end

test "updates metadata after removing dynamic task post-start" do
old_dynamic_task = SolidQueue::RecurringTask.create!(
key: "old_dynamic_task",
Expand Down
Loading