Use a lower timeout for sync worker and renew it from within the loop

parent 433ef188
...@@ -3,7 +3,7 @@ class GeoRepositorySyncWorker ...@@ -3,7 +3,7 @@ class GeoRepositorySyncWorker
include CronjobQueue include CronjobQueue
LEASE_KEY = 'geo_repository_sync_worker'.freeze LEASE_KEY = 'geo_repository_sync_worker'.freeze
LEASE_TIMEOUT = 8.hours.freeze LEASE_TIMEOUT = 10.minutes
BATCH_SIZE = 1000 BATCH_SIZE = 1000
BACKOFF_DELAY = 5.minutes BACKOFF_DELAY = 5.minutes
MAX_CAPACITY = 25 MAX_CAPACITY = 25
...@@ -40,6 +40,7 @@ class GeoRepositorySyncWorker ...@@ -40,6 +40,7 @@ class GeoRepositorySyncWorker
schedule_jobs schedule_jobs
break if last_batch break if last_batch
break unless renew_lease!
sleep(1) sleep(1)
end end
...@@ -117,17 +118,28 @@ class GeoRepositorySyncWorker ...@@ -117,17 +118,28 @@ class GeoRepositorySyncWorker
end end
def try_obtain_lease def try_obtain_lease
lease = Gitlab::ExclusiveLease.new(LEASE_KEY, timeout: LEASE_TIMEOUT).try_obtain lease = exclusive_lease.try_obtain
return unless lease unless lease
logger.info "Cannot obtain an exclusive lease. There must be another worker already in execution."
return
end
begin begin
yield lease yield lease
ensure ensure
Gitlab::ExclusiveLease.cancel(LEASE_KEY, lease) release_lease(lease)
end end
end end
def exclusive_lease
@lease ||= Gitlab::ExclusiveLease.new(LEASE_KEY, timeout: LEASE_TIMEOUT)
end
def renew_lease!
exclusive_lease.renew
end
def release_lease(uuid) def release_lease(uuid)
Gitlab::ExclusiveLease.cancel(LEASE_KEY, uuid) Gitlab::ExclusiveLease.cancel(LEASE_KEY, uuid)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment