Skip to content

Commit 3129f39

Browse files
rosaclaude
andcommitted
Track scheduled recurring tasks by record id
A task that's deleted and created again with the same key gets a new id, so keying the in-memory schedule by id lets the existing steps that schedule new tasks and unschedule deleted ones handle it, without a second map from keys to ids or a separate active flag. A run only schedules the next one if its task is still in the schedule, which also covers a replaced task and a stopped scheduler. The database query for dynamic tasks now runs before taking the lock, so it doesn't hold up a task that's due. Polling now only unschedules dynamic tasks. A static task whose row is deleted keeps running until the scheduler restarts, when it reads the configuration again. Also freeze time in the test for tasks updated in place, so a run at the top of the hour can't land between its two lookups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 03f759d commit 3129f39

2 files changed

Lines changed: 41 additions & 62 deletions

File tree

‎lib/solid_queue/scheduler/recurring_schedule.rb‎

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ def initialize(static_tasks, dynamic_tasks_enabled: false)
1212

1313
@schedule_lock = Mutex.new
1414
@scheduled_tasks = Concurrent::Hash.new
15-
@scheduled_dynamic_task_ids = {}
16-
@active = false
1715
end
1816

1917
def configured_tasks
@@ -32,21 +30,14 @@ def schedule_tasks
3230
end
3331

3432
schedule_lock.synchronize do
35-
@active = true
36-
configured_tasks.each { |task| schedule_task_without_lock(task) }
33+
configured_tasks.each { |task| schedule_task(task) }
3734
end
3835
end
3936

40-
def schedule_task(task, run_at: task.next_time)
41-
schedule_lock.synchronize { schedule_task_without_lock(task, run_at: run_at) }
42-
end
43-
4437
def unschedule_tasks
4538
schedule_lock.synchronize do
46-
@active = false
4739
scheduled_tasks.values.each(&:cancel)
4840
scheduled_tasks.clear
49-
scheduled_dynamic_task_ids.clear
5041
end
5142
end
5243

@@ -56,17 +47,17 @@ def task_keys
5647

5748
def reschedule_dynamic_tasks
5849
wrap_in_app_executor do
50+
reload_dynamic_tasks
51+
5952
schedule_lock.synchronize do
60-
reload_dynamic_tasks
6153
schedule_created_dynamic_tasks
62-
reschedule_recreated_dynamic_tasks
6354
unschedule_deleted_dynamic_tasks
6455
end
6556
end
6657
end
6758

6859
private
69-
attr_reader :static_tasks, :schedule_lock, :scheduled_dynamic_task_ids
60+
attr_reader :static_tasks, :schedule_lock
7061

7162
def static_task_keys
7263
static_tasks.map(&:key)
@@ -80,44 +71,20 @@ def dynamic_tasks_enabled?
8071
@dynamic_tasks_enabled
8172
end
8273

74+
# Scheduled tasks are tracked by record id, so a dynamic task that's deleted and
75+
# created again with the same key is unscheduled and scheduled as a new one.
8376
def schedule_created_dynamic_tasks
84-
dynamic_tasks.reject { |task| scheduled_tasks.key?(task.key) }.each do |task|
85-
schedule_task_without_lock(task)
86-
end
87-
end
88-
89-
def reschedule_recreated_dynamic_tasks
90-
dynamic_tasks.each do |task|
91-
next unless scheduled_dynamic_task_ids.key?(task.key)
92-
next if scheduled_dynamic_task_ids[task.key] == task.id
93-
94-
unschedule_task(task.key)
95-
schedule_task_without_lock(task)
96-
end
77+
dynamic_tasks.reject { |task| scheduled_tasks.key?(task.id) }.each { |task| schedule_task(task) }
9778
end
9879

9980
def unschedule_deleted_dynamic_tasks
100-
(scheduled_dynamic_task_ids.keys - dynamic_tasks.map(&:key)).each { |key| unschedule_task(key) }
101-
end
102-
103-
def unschedule_task(key)
104-
scheduled_tasks.delete(key)&.cancel
105-
scheduled_dynamic_task_ids.delete(key)
106-
end
107-
108-
def schedule_task_without_lock(task, run_at: task.next_time)
109-
scheduled_tasks[task.key] = schedule(task, run_at: run_at)
110-
scheduled_dynamic_task_ids[task.key] = task.id unless task.static?
111-
end
112-
113-
def schedule_next_task(task, run_at:)
114-
schedule_lock.synchronize do
115-
schedule_task_without_lock(task, run_at: run_at) if @active && current_task?(task)
81+
(scheduled_tasks.keys - static_tasks.map(&:id) - dynamic_tasks.map(&:id)).each do |id|
82+
scheduled_tasks.delete(id).cancel
11683
end
11784
end
11885

119-
def current_task?(task)
120-
task.static? || scheduled_dynamic_task_ids[task.key] == task.id
86+
def schedule_task(task, run_at: task.next_time)
87+
scheduled_tasks[task.id] = schedule(task, run_at: run_at)
12188
end
12289

12390
def persist_static_tasks
@@ -141,7 +108,7 @@ def schedule(task, run_at: task.next_time)
141108
delay = [ (run_at - Time.current).to_f, 0.1 ].max
142109

143110
scheduled_task = Concurrent::ScheduledTask.new(delay, args: [ task, run_at ]) do |thread_task, thread_task_run_at|
144-
schedule_next_task(thread_task, run_at: thread_task.next_time_after(thread_task_run_at))
111+
schedule_next_run(thread_task, run_at: thread_task.next_time_after(thread_task_run_at))
145112

146113
wrap_in_app_executor do
147114
thread_task.enqueue(at: thread_task_run_at)
@@ -156,5 +123,12 @@ def schedule(task, run_at: task.next_time)
156123

157124
scheduled_task.tap(&:execute)
158125
end
126+
127+
# Only schedule the next run if the task wasn't unscheduled in the meantime
128+
def schedule_next_run(task, run_at:)
129+
schedule_lock.synchronize do
130+
schedule_task(task, run_at: run_at) if scheduled_tasks.key?(task.id)
131+
end
132+
end
159133
end
160134
end

‎test/unit/scheduler_test.rb‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class SchedulerTest < ActiveSupport::TestCase
169169
original_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")
170170
recurring_schedule = SolidQueue::Scheduler::RecurringSchedule.new({}, dynamic_tasks_enabled: true)
171171
recurring_schedule.schedule_tasks
172-
original_scheduled_task = recurring_schedule.scheduled_tasks.fetch("dynamic_task")
172+
original_scheduled_task = recurring_schedule.scheduled_tasks.fetch(original_task.id)
173173

174174
SolidQueue.unschedule_recurring_task("dynamic_task")
175175
SolidQueue.schedule_recurring_task(
@@ -181,35 +181,40 @@ class SchedulerTest < ActiveSupport::TestCase
181181
replacement_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")
182182

183183
recurring_schedule.reschedule_dynamic_tasks
184-
replacement_scheduled_task = recurring_schedule.scheduled_tasks.fetch("dynamic_task")
184+
replacement_scheduled_task = recurring_schedule.scheduled_tasks.fetch(replacement_task.id)
185185

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

190-
recurring_schedule.send(:schedule_next_task, original_task, run_at: original_task.next_time)
191-
assert_same replacement_scheduled_task, recurring_schedule.scheduled_tasks.fetch("dynamic_task")
190+
recurring_schedule.send(:schedule_next_run, original_task, run_at: original_task.next_time)
191+
assert_equal [ replacement_task.id ], recurring_schedule.scheduled_tasks.keys
192+
assert_same replacement_scheduled_task, recurring_schedule.scheduled_tasks.fetch(replacement_task.id)
192193
end
193194
ensure
194195
recurring_schedule&.unschedule_tasks
195196
end
196197

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

209-
task.update!(schedule: "every day")
210-
recurring_schedule.reschedule_dynamic_tasks
213+
task.update!(schedule: "every day")
214+
recurring_schedule.reschedule_dynamic_tasks
211215

212-
assert_same scheduled_task, recurring_schedule.scheduled_tasks.fetch("dynamic_task")
216+
assert_same scheduled_task, recurring_schedule.scheduled_tasks.fetch(task.id)
217+
end
213218
ensure
214219
recurring_schedule&.unschedule_tasks
215220
end

0 commit comments

Comments
 (0)