Merge pull request #2087 from jbargu/jg_sci_3870

[SCI-3870] Change/fix asset locking mechanism
This commit is contained in:
Jure Grabnar 2019-09-30 08:48:39 +02:00 committed by GitHub
commit e24541ae51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -449,19 +449,18 @@ class Asset < ApplicationRecord
# locked?, lock_asset and refresh_lock rely on the asset
# being locked in the database to prevent race conditions
def locked?
unlock_expired
!lock.nil?
end
def lock_asset(lock_string)
self.lock = lock_string
self.lock_ttl = Time.now.to_i + LOCK_DURATION
delay(queue: :assets, run_at: LOCK_DURATION.seconds.from_now).unlock_expired
save!
end
def refresh_lock
self.lock_ttl = Time.now.to_i + LOCK_DURATION
delay(queue: :assets, run_at: LOCK_DURATION.seconds.from_now).unlock_expired
save!
end
@ -473,7 +472,7 @@ class Asset < ApplicationRecord
def unlock_expired
with_lock do
if !lock_ttl.nil? && lock_ttl >= Time.now.to_i
if !lock_ttl.nil? && lock_ttl < Time.now.to_i
self.lock = nil
self.lock_ttl = nil
save!