Skip to content
Snippets Groups Projects
Unverified Commit a207ecde authored by Sébastien Gaya's avatar Sébastien Gaya Committed by GitHub
Browse files

V3 du lock du website : Locked by (#1962)

* locked_by_job_id

* fix
parent d513a872
No related branches found
No related tags found
No related merge requests found
......@@ -20,9 +20,9 @@ class Communication::Website::BaseJob < ApplicationJob
# Website might be deleted in between
return unless website.present?
# Raise if website is locked to retry later
raise Communication::Website::LockError.new("Interrupted because of website lock.") if website.locked_for_background_jobs?
raise Communication::Website::LockError.new("Interrupted because of website lock.") if website.locked_for_background_jobs?(job_id)
# We lock the website to prevent race conditions
website.lock_for_background_jobs!
website.lock_for_background_jobs!(job_id)
begin
# We execute the job
execute
......
......@@ -6,16 +6,24 @@ module Communication::Website::WithLock
LOCK_MAX_DURATION = 2.hours
end
def locked_for_background_jobs?
def locked_for_background_jobs?(executing_job_id)
reload
locked_at.present?
locked_at.present? && # Not locked if locked_at is nil
locked_by_job_id != executing_job_id && # If the website was locked by the same job, we can continue
GoodJob::Job.running.find_by(id: locked_by_job_id).present? # Check if the locking job is still running (to handle SIGKILL and hard shutdowns)
end
def lock_for_background_jobs!
update_column :locked_at, Time.zone.now
def lock_for_background_jobs!(job_id)
update_columns(
locked_at: Time.zone.now,
locked_by_job_id: job_id
)
end
def unlock_for_background_jobs!
update_column :locked_at, nil
update_columns(
locked_at: nil,
locked_by_job_id: nil
)
end
end
class AddLockedByToCommunicationWebsites < ActiveRecord::Migration[7.1]
def change
add_column :communication_websites, :locked_by_job_id, :uuid
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment