2019-06-12 17:11:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Namespace::AggregationSchedule < ApplicationRecord
|
2019-07-02 10:44:39 -04:00
|
|
|
include AfterCommitQueue
|
|
|
|
include ExclusiveLeaseGuard
|
|
|
|
|
2019-06-12 17:11:14 -04:00
|
|
|
self.primary_key = :namespace_id
|
|
|
|
|
2019-07-02 10:44:39 -04:00
|
|
|
DEFAULT_LEASE_TIMEOUT = 3.hours
|
|
|
|
REDIS_SHARED_KEY = 'gitlab:update_namespace_statistics_delay'.freeze
|
|
|
|
|
2019-06-12 17:11:14 -04:00
|
|
|
belongs_to :namespace
|
2019-07-02 10:44:39 -04:00
|
|
|
|
|
|
|
after_create :schedule_root_storage_statistics
|
|
|
|
|
|
|
|
def self.delay_timeout
|
|
|
|
redis_timeout = Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.get(REDIS_SHARED_KEY)
|
|
|
|
end
|
|
|
|
|
|
|
|
redis_timeout.nil? ? DEFAULT_LEASE_TIMEOUT : redis_timeout.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_root_storage_statistics
|
|
|
|
run_after_commit_or_now do
|
|
|
|
try_obtain_lease do
|
|
|
|
Namespaces::RootStatisticsWorker
|
|
|
|
.perform_async(namespace_id)
|
|
|
|
|
|
|
|
Namespaces::RootStatisticsWorker
|
|
|
|
.perform_in(self.class.delay_timeout, namespace_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Used by ExclusiveLeaseGuard
|
|
|
|
def lease_timeout
|
|
|
|
self.class.delay_timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
# Used by ExclusiveLeaseGuard
|
|
|
|
def lease_key
|
|
|
|
"namespace:namespaces_root_statistics:#{namespace_id}"
|
|
|
|
end
|
2019-07-04 11:52:02 -04:00
|
|
|
|
|
|
|
# Used by ExclusiveLeaseGuard
|
|
|
|
# Overriding value as we never release the lease
|
|
|
|
# before the timeout in order to prevent multiple
|
|
|
|
# RootStatisticsWorker to start in a short span of time
|
|
|
|
def lease_release?
|
|
|
|
false
|
|
|
|
end
|
2019-06-12 17:11:14 -04:00
|
|
|
end
|