Commit c13567bb authored by Alper Akgun's avatar Alper Akgun

Set the usage ping cron based on UUID

Review comments
parent cc29e1ba
......@@ -184,14 +184,15 @@ class Settings < Settingslogic
URI.parse(url_without_path).host
end
# Runs at a random time of day on a consistent day of the week based on
# Runs at a consistent random time of day on a day of the week based on
# the instance UUID. This is to balance the load on the service receiving
# these pings. The sidekiq job handles temporary http failures.
def cron_for_usage_ping
hour = rand(24)
minute = rand(60)
# Set a default UUID for the case when the UUID hasn't been initialized.
uuid = Gitlab::CurrentSettings.uuid || 'uuid-not-set'
minute = Digest::MD5.hexdigest(uuid + 'minute').to_i(16) % 60
hour = Digest::MD5.hexdigest(uuid + 'hour').to_i(16) % 24
day_of_week = Digest::MD5.hexdigest(uuid).to_i(16) % 7
"#{minute} #{hour} * * #{day_of_week}"
......
......@@ -112,4 +112,26 @@ RSpec.describe Settings do
end
end
end
describe '.cron_for_usage_ping' do
it 'returns correct crontab for some manually calculated example' do
allow(Gitlab::CurrentSettings)
.to receive(:uuid) { 'd9e2f4e8-db1f-4e51-b03d-f427e1965c4a'}
expect(described_class.send(:cron_for_usage_ping)).to eq('21 18 * * 4')
end
it 'returns min, hour, day in the valid range' do
allow(Gitlab::CurrentSettings)
.to receive(:uuid) { SecureRandom.uuid }
10.times do
cron = described_class.send(:cron_for_usage_ping).split(/\s/)
expect(cron[0].to_i).to be_between(0, 59)
expect(cron[1].to_i).to be_between(0, 23)
expect(cron[4].to_i).to be_between(0, 6)
end
end
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