debian-mirror-gitlab/lib/version_check.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require "base64"
class VersionCheck
2022-01-26 12:08:38 +05:30
include ReactiveCaching
2022-11-25 23:54:43 +05:30
## Version Check Reactive Caching
## This cache stores the external API response from https://version.gitlab.com
##
## Example API Response
## {
## "latest_version": "15.2.2",
## "severity": "success"
## }
##
## This response from this endpoint only changes in 2 scenarios:
## 1. Customer upgrades their GitLab Instance
## 2. GitLab releases a new version
##
## We use GitLab::VERSION as the identifier for the cached information.
## This means if the user upgrades their version we will create a new cache record.
## The old one will be invalidated and cleaned up at the end of the self.reactive_cache_lifetime.
##
## - self.reactive_cache_refresh_interval = 12.hours
## We want to prevent as many external API calls as possible to save on resources.
## Since an EXISTING cache record will only become "invalid" if GitLab releases a new version we
## determined that 12 hour intervals is enough of a window to capture an available upgrade.
##
## - self.reactive_cache_lifetime = 7.days
## We don't want the data to be missing every time a user revisits a page using this info.
## Thus 7 days seems like a fair amount of time before we erase the cache.
## This also will handle cleaning up old cache records as they will no longer be accessed after an upgrade.
##
self.reactive_cache_refresh_interval = 12.hours
self.reactive_cache_lifetime = 7.days
2022-01-26 12:08:38 +05:30
self.reactive_cache_work_type = :external_dependency
self.reactive_cache_worker_finder = ->(_id, *args) { from_cache }
2019-02-15 15:39:39 +05:30
def self.data
2015-09-11 14:41:01 +05:30
{ version: Gitlab::VERSION }
end
2022-01-26 12:08:38 +05:30
def self.headers
{ "REFERER": Gitlab.config.gitlab.url }
end
def self.url
encoded_data = Base64.urlsafe_encode64(data.to_json)
"#{host}/check.json?gitlab_info=#{encoded_data}"
end
2019-02-15 15:39:39 +05:30
def self.host
2021-06-08 01:23:25 +05:30
'https://version.gitlab.com'
2015-09-11 14:41:01 +05:30
end
2022-01-26 12:08:38 +05:30
def self.from_cache(*)
new
end
def id
Gitlab::VERSION
end
def calculate_reactive_cache(*)
response = Gitlab::HTTP.try_get(self.class.url, headers: self.class.headers)
case response&.code
when 200
response.body
end
end
def response
with_reactive_cache do |data|
Gitlab::Json.parse(data) if data
end
end
2015-09-11 14:41:01 +05:30
end
2021-06-08 01:23:25 +05:30
VersionCheck.prepend_mod