Skip to content

Commit 2939e3d

Browse files
committed
Fix rescheduling recreated dynamic tasks
1 parent afbf37a commit 2939e3d

2 files changed

Lines changed: 111 additions & 16 deletions

File tree

‎lib/solid_queue/scheduler/recurring_schedule.rb‎

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def initialize(static_tasks, dynamic_tasks_enabled: false)
1010
@static_tasks = Array(static_tasks).map { |task| RecurringTask.wrap(task) }.select(&:valid?)
1111
@dynamic_tasks_enabled = dynamic_tasks_enabled
1212

13+
@schedule_lock = Mutex.new
1314
@scheduled_tasks = Concurrent::Hash.new
15+
@scheduled_dynamic_task_ids = {}
16+
@active = false
1417
end
1518

1619
def configured_tasks
@@ -28,18 +31,23 @@ def schedule_tasks
2831
reload_dynamic_tasks
2932
end
3033

31-
configured_tasks.each do |task|
32-
schedule_task(task)
34+
schedule_lock.synchronize do
35+
@active = true
36+
configured_tasks.each { |task| schedule_task_without_lock(task) }
3337
end
3438
end
3539

3640
def schedule_task(task, run_at: task.next_time)
37-
scheduled_tasks[task.key] = schedule(task, run_at: run_at)
41+
schedule_lock.synchronize { schedule_task_without_lock(task, run_at: run_at) }
3842
end
3943

4044
def unschedule_tasks
41-
scheduled_tasks.values.each(&:cancel)
42-
scheduled_tasks.clear
45+
schedule_lock.synchronize do
46+
@active = false
47+
scheduled_tasks.values.each(&:cancel)
48+
scheduled_tasks.clear
49+
scheduled_dynamic_task_ids.clear
50+
end
4351
end
4452

4553
def task_keys
@@ -48,14 +56,17 @@ def task_keys
4856

4957
def reschedule_dynamic_tasks
5058
wrap_in_app_executor do
51-
reload_dynamic_tasks
52-
schedule_created_dynamic_tasks
53-
unschedule_deleted_dynamic_tasks
59+
schedule_lock.synchronize do
60+
reload_dynamic_tasks
61+
schedule_created_dynamic_tasks
62+
reschedule_recreated_dynamic_tasks
63+
unschedule_deleted_dynamic_tasks
64+
end
5465
end
5566
end
5667

5768
private
58-
attr_reader :static_tasks
69+
attr_reader :static_tasks, :schedule_lock, :scheduled_dynamic_task_ids
5970

6071
def static_task_keys
6172
static_tasks.map(&:key)
@@ -70,18 +81,45 @@ def dynamic_tasks_enabled?
7081
end
7182

7283
def schedule_created_dynamic_tasks
73-
RecurringTask.dynamic.where.not(key: scheduled_tasks.keys).each do |task|
74-
schedule_task(task)
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)
7596
end
7697
end
7798

7899
def unschedule_deleted_dynamic_tasks
79-
(scheduled_tasks.keys - RecurringTask.pluck(:key)).each do |key|
80-
scheduled_tasks[key].cancel
81-
scheduled_tasks.delete(key)
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)
82116
end
83117
end
84118

119+
def current_task?(task)
120+
task.static? || scheduled_dynamic_task_ids[task.key] == task.id
121+
end
122+
85123
def persist_static_tasks
86124
RecurringTask.static.where.not(key: static_task_keys).delete_all
87125
RecurringTask.create_or_update_all static_tasks
@@ -102,8 +140,8 @@ def load_dynamic_tasks
102140
def schedule(task, run_at: task.next_time)
103141
delay = [ (run_at - Time.current).to_f, 0.1 ].max
104142

105-
scheduled_task = Concurrent::ScheduledTask.new(delay, args: [ self, task, run_at ]) do |thread_schedule, thread_task, thread_task_run_at|
106-
thread_schedule.schedule_task(thread_task, run_at: thread_task.next_time_after(thread_task_run_at))
143+
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))
107145

108146
wrap_in_app_executor do
109147
thread_task.enqueue(at: thread_task_run_at)

‎test/unit/scheduler_test.rb‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,61 @@ class SchedulerTest < ActiveSupport::TestCase
156156
ensure
157157
scheduler&.stop
158158
end
159+
160+
test "reschedules a dynamic task recreated with the same key" do
161+
recurring_schedule = nil
162+
travel_to Time.zone.local(2026, 8, 25, 10, 15) do
163+
SolidQueue.schedule_recurring_task(
164+
"dynamic_task",
165+
class: "AddToBufferJob",
166+
schedule: "every hour",
167+
args: [ 42 ]
168+
)
169+
original_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")
170+
recurring_schedule = SolidQueue::Scheduler::RecurringSchedule.new({}, dynamic_tasks_enabled: true)
171+
recurring_schedule.schedule_tasks
172+
original_scheduled_task = recurring_schedule.scheduled_tasks.fetch("dynamic_task")
173+
174+
SolidQueue.unschedule_recurring_task("dynamic_task")
175+
SolidQueue.schedule_recurring_task(
176+
"dynamic_task",
177+
class: "AddToBufferJob",
178+
schedule: "every day",
179+
args: [ 42 ]
180+
)
181+
replacement_task = SolidQueue::RecurringTask.find_by!(key: "dynamic_task")
182+
183+
recurring_schedule.reschedule_dynamic_tasks
184+
replacement_scheduled_task = recurring_schedule.scheduled_tasks.fetch("dynamic_task")
185+
186+
assert_instance_of Concurrent::CancelledOperationError, original_scheduled_task.reason
187+
assert_not_same original_scheduled_task, replacement_scheduled_task
188+
assert_in_delta replacement_task.next_time - Time.current, replacement_scheduled_task.initial_delay, 0.1
189+
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")
192+
end
193+
ensure
194+
recurring_schedule&.unschedule_tasks
195+
end
196+
197+
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")
208+
209+
task.update!(schedule: "every day")
210+
recurring_schedule.reschedule_dynamic_tasks
211+
212+
assert_same scheduled_task, recurring_schedule.scheduled_tasks.fetch("dynamic_task")
213+
ensure
214+
recurring_schedule&.unschedule_tasks
215+
end
159216
end

0 commit comments

Comments
 (0)