Skip to content
Merged
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
50 changes: 31 additions & 19 deletions lib/solid_queue/scheduler/recurring_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(static_tasks, dynamic_tasks_enabled: false)
@static_tasks = Array(static_tasks).map { |task| RecurringTask.wrap(task) }.select(&:valid?)
@dynamic_tasks_enabled = dynamic_tasks_enabled

@schedule_lock = Mutex.new
@scheduled_tasks = Concurrent::Hash.new
end

Expand All @@ -28,18 +29,16 @@ def schedule_tasks
reload_dynamic_tasks
end

configured_tasks.each do |task|
schedule_task(task)
schedule_lock.synchronize do
configured_tasks.each { |task| schedule_task(task) }
end
end

def schedule_task(task, run_at: task.next_time)
scheduled_tasks[task.key] = schedule(task, run_at: run_at)
end

def unschedule_tasks
scheduled_tasks.values.each(&:cancel)
scheduled_tasks.clear
schedule_lock.synchronize do
scheduled_tasks.values.each(&:cancel)
scheduled_tasks.clear
end
end

def task_keys
Expand All @@ -49,13 +48,16 @@ def task_keys
def reschedule_dynamic_tasks
wrap_in_app_executor do
reload_dynamic_tasks
schedule_created_dynamic_tasks
unschedule_deleted_dynamic_tasks

schedule_lock.synchronize do
schedule_created_dynamic_tasks
unschedule_deleted_dynamic_tasks
end
end
end

private
attr_reader :static_tasks
attr_reader :static_tasks, :schedule_lock

def static_task_keys
static_tasks.map(&:key)
Expand All @@ -69,19 +71,22 @@ def dynamic_tasks_enabled?
@dynamic_tasks_enabled
end

# Scheduled tasks are tracked by record id, so a dynamic task that's deleted and
# created again with the same key is unscheduled and scheduled as a new one.
def schedule_created_dynamic_tasks
RecurringTask.dynamic.where.not(key: scheduled_tasks.keys).each do |task|
schedule_task(task)
end
dynamic_tasks.reject { |task| scheduled_tasks.key?(task.id) }.each { |task| schedule_task(task) }
end

def unschedule_deleted_dynamic_tasks
(scheduled_tasks.keys - RecurringTask.pluck(:key)).each do |key|
scheduled_tasks[key].cancel
scheduled_tasks.delete(key)
(scheduled_tasks.keys - static_tasks.map(&:id) - dynamic_tasks.map(&:id)).each do |id|
scheduled_tasks.delete(id).cancel
end
end

def schedule_task(task, run_at: task.next_time)
scheduled_tasks[task.id] = schedule(task, run_at: run_at)
end

def persist_static_tasks
RecurringTask.static.where.not(key: static_task_keys).delete_all
RecurringTask.create_or_update_all static_tasks
Expand All @@ -102,8 +107,8 @@ def load_dynamic_tasks
def schedule(task, run_at: task.next_time)
delay = [ (run_at - Time.current).to_f, 0.1 ].max

scheduled_task = Concurrent::ScheduledTask.new(delay, args: [ self, task, run_at ]) do |thread_schedule, thread_task, thread_task_run_at|
thread_schedule.schedule_task(thread_task, run_at: thread_task.next_time_after(thread_task_run_at))
scheduled_task = Concurrent::ScheduledTask.new(delay, args: [ task, run_at ]) do |thread_task, thread_task_run_at|
schedule_next_run(thread_task, run_at: thread_task.next_time_after(thread_task_run_at))

wrap_in_app_executor do
thread_task.enqueue(at: thread_task_run_at)
Expand All @@ -118,5 +123,12 @@ def schedule(task, run_at: task.next_time)

scheduled_task.tap(&:execute)
end

# Only schedule the next run if the task wasn't unscheduled in the meantime
def schedule_next_run(task, run_at:)
schedule_lock.synchronize do
schedule_task(task, run_at: run_at) if scheduled_tasks.key?(task.id)
end
end
end
end
62 changes: 62 additions & 0 deletions test/unit/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,66 @@ class SchedulerTest < ActiveSupport::TestCase
ensure
scheduler&.stop
end

test "reschedules a dynamic task recreated with the same key" do
recurring_schedule = nil
travel_to Time.zone.local(2026, 8, 25, 10, 15) do
SolidQueue.schedule_recurring_task(
"dynamic_task",
class: "AddToBufferJob",
schedule: "every hour",
args: [ 42 ]
)
original_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")
recurring_schedule = SolidQueue::Scheduler::RecurringSchedule.new({}, dynamic_tasks_enabled: true)
recurring_schedule.schedule_tasks
original_scheduled_task = recurring_schedule.scheduled_tasks.fetch(original_task.id)

SolidQueue.unschedule_recurring_task("dynamic_task")
SolidQueue.schedule_recurring_task(
"dynamic_task",
class: "AddToBufferJob",
schedule: "every day",
args: [ 42 ]
)
replacement_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")

recurring_schedule.reschedule_dynamic_tasks
replacement_scheduled_task = recurring_schedule.scheduled_tasks.fetch(replacement_task.id)

assert_instance_of Concurrent::CancelledOperationError, original_scheduled_task.reason
assert_not_same original_scheduled_task, replacement_scheduled_task
assert_in_delta replacement_task.next_time - Time.current, replacement_scheduled_task.initial_delay, 0.1

recurring_schedule.send(:schedule_next_run, original_task, run_at: original_task.next_time)
assert_equal [ replacement_task.id ], recurring_schedule.scheduled_tasks.keys
assert_same replacement_scheduled_task, recurring_schedule.scheduled_tasks.fetch(replacement_task.id)
end
ensure
recurring_schedule&.unschedule_tasks
end

test "does not reschedule a dynamic task updated in place" do
recurring_schedule = nil
# Away from the top of the hour, so the task can't run between the two lookups
travel_to Time.zone.local(2026, 8, 25, 10, 15) do
task = SolidQueue::RecurringTask.create!(
key: "dynamic_task",
static: false,
class_name: "AddToBufferJob",
schedule: "every hour",
arguments: [ 42 ]
)
recurring_schedule = SolidQueue::Scheduler::RecurringSchedule.new({}, dynamic_tasks_enabled: true)
recurring_schedule.schedule_tasks
scheduled_task = recurring_schedule.scheduled_tasks.fetch(task.id)

task.update!(schedule: "every day")
recurring_schedule.reschedule_dynamic_tasks

assert_same scheduled_task, recurring_schedule.scheduled_tasks.fetch(task.id)
end
ensure
recurring_schedule&.unschedule_tasks
end
end
Loading