Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cron double-enqueue because delay close to 0.01 and possibly clock-drift #1543

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(good_job-1536): case of clock drift and delay close to 0.001
  • Loading branch information
ccouton committed Nov 15, 2024
commit 59b43aa86a64ff8581fc490bfc9ad6268336e24e
4 changes: 3 additions & 1 deletion app/models/good_job/cron_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CronEntry
include ActiveModel::Model

attr_reader :params
attr_reader :enqueued_at_least_one_time

def self.all(configuration: nil)
configuration ||= GoodJob.configuration
Expand All @@ -26,7 +27,7 @@ def self.find(key, configuration: nil)

def initialize(params = {})
@params = params

@enqueued_at_least_one_time = false
return if cron_proc?
raise ArgumentError, "Invalid cron format: '#{cron}'" unless fugit.instance_of?(Fugit::Cron)
end
Expand Down Expand Up @@ -98,6 +99,7 @@ def disable

def enqueue(cron_at = nil)
GoodJob::CurrentThread.within do |current_thread|
@enqueued_at_least_one_time = true
current_thread.cron_key = key
current_thread.cron_at = cron_at

Expand Down
20 changes: 11 additions & 9 deletions lib/good_job/cron_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,20 @@ def shutdown?
def create_task(cron_entry, previously_at: nil)
cron_at = cron_entry.next_at(previously_at: previously_at)
delay = [(cron_at - Time.current).to_f, 0].max
future = Concurrent::ScheduledTask.new(delay, args: [self, cron_entry, cron_at], executor: @executor) do |thr_scheduler, thr_cron_entry, thr_cron_at|
# Re-schedule the next cron task before executing the current task
thr_scheduler.create_task(thr_cron_entry, previously_at: thr_cron_at)
if !cron_entry.enqueued_at_least_one_time || delay >= 0.01 #case of clock drift and delay close to 0.001
future = Concurrent::ScheduledTask.new(delay, args: [self, cron_entry, cron_at], executor: @executor) do |thr_scheduler, thr_cron_entry, thr_cron_at|
# Re-schedule the next cron task before executing the current task
thr_scheduler.create_task(thr_cron_entry, previously_at: thr_cron_at)

Rails.application.executor.wrap do
cron_entry.enqueue(thr_cron_at) if thr_cron_entry.enabled?
Rails.application.executor.wrap do
cron_entry.enqueue(thr_cron_at) if thr_cron_entry.enabled?
end
end
end

@tasks[cron_entry.key] = future
future.add_observer(self.class, :task_observer)
future.execute
@tasks[cron_entry.key] = future
future.add_observer(self.class, :task_observer)
future.execute
end
end

# Uses the graceful restart period to re-enqueue jobs that were scheduled to run during the period.
Expand Down
Loading