Commit f493c9a8 authored by Stan Hu's avatar Stan Hu

Add async task that reports EE usage every week

Closes #380
parent 1c04b072
......@@ -3,6 +3,11 @@ module LicenseHelper
User.active.count
end
def max_historical_user_count
date_range = (Date.today - 1.year)..Date.today
HistoricalData.during(date_range).maximum(:active_user_count) || 0
end
def license_message(signed_in: signed_in?, is_admin: (current_user && current_user.is_admin?))
@license_message ||=
if License.current
......
......@@ -4,8 +4,7 @@
- else
- licensed_users = 'Unlimited'
- date_range = (Date.today - 1.year)..Date.today
- historical = HistoricalData.during(date_range).maximum(:active_user_count) || 0
- historical = max_historical_user_count
- if historical && restricted && historical > restricted
- users_over_license = historical - restricted
- else
......
class GitlabUsagePingWorker
LEASE_TIMEOUT = 86400
include LicenseHelper
include Sidekiq::Worker
include HTTParty
# This is not guaranteed to succeed, so don't retry on failure
sidekiq_options queue: :default, retry: false
def perform
# Multiple Sidekiq workers could run this. We should only do this at most once a day.
return unless try_obtain_lease
HTTParty.post(url,
body: data.to_json,
headers: { 'Content-type' => 'application/json' }
)
end
def try_obtain_lease
Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain
end
def data
usage_data = { version: Gitlab::VERSION }
usage_data[:active_users] = current_active_user_count
license = License.current
if license
usage_data[:historical_max_users] = max_historical_user_count
usage_data[:licensee] = license.licensee
usage_data[:license_user_count] = license.user_count
usage_data[:license_starts_at] = license.starts_at
usage_data[:license_expires_at] = license.expires_at
usage_data[:license_add_ons] = license.add_ons
usage_data[:recorded_at] = Time.now
end
usage_data
end
def url
'https://version.gitlab.com/usage_ping'
end
end
......@@ -125,6 +125,14 @@ class Settings < Settingslogic
URI.parse(url_without_path).host
end
# Random cron time every Sunday to load balance usage pings
def cron_random_weekly_time
hour = rand(24)
minute = rand(60)
"#{hour} #{minute} * * 0"
end
end
end
......@@ -370,6 +378,9 @@ Settings.cron_jobs['geo_bulk_notify_worker']['job_class'] ||= 'GeoBulkNotifyWork
Settings.cron_jobs['gitlab_remove_project_export_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['gitlab_remove_project_export_worker']['cron'] ||= '0 * * * *'
Settings.cron_jobs['gitlab_remove_project_export_worker']['job_class'] = 'GitlabRemoveProjectExportWorker'
Settings.cron_jobs['gitlab_usage_ping_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['gitlab_usage_ping_worker']['cron'] ||= Settings.send(:cron_random_weekly_time)
Settings.cron_jobs['gitlab_usage_ping_worker']['job_class'] = 'GitlabUsagePingWorker'
#
# GitLab Shell
......
require 'spec_helper'
describe GitlabUsagePingWorker do
subject { GitlabUsagePingWorker.new }
it "gathers license data" do
data = subject.data
license = License.current
expect(data[:version]).to eq(Gitlab::VERSION)
expect(data[:licensee]).to eq(license.licensee)
expect(data[:active_users]).to eq(User.active.count)
expect(data[:licensee]).to eq(license.licensee)
expect(data[:license_user_count]).to eq(license.user_count)
expect(data[:license_starts_at]).to eq(license.starts_at)
expect(data[:license_expires_at]).to eq(license.expires_at)
expect(data[:license_add_ons]).to eq(license.add_ons)
expect(data[:recorded_at]).to be_a(Time)
end
it "sends POST request" do
stub_request(:post, "https://version.gitlab.com/usage_ping").
to_return(status: 200, body: '', headers: {})
expect(subject).to receive(:try_obtain_lease).and_return(true)
expect(subject.perform.response.code.to_i).to eq(200)
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