@@ -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 )
0 commit comments