2013-02-19 15:16:50 -05:00
|
|
|
module DiscourseUpdates
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
def check_version
|
2013-07-03 11:06:07 -04:00
|
|
|
version_info = if updated_at.nil?
|
|
|
|
DiscourseVersionCheck.new(
|
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
installed_sha: (Discourse.git_version == 'unknown' ? nil : Discourse.git_version),
|
|
|
|
updated_at: nil
|
|
|
|
)
|
|
|
|
else
|
|
|
|
DiscourseVersionCheck.new(
|
|
|
|
latest_version: latest_version,
|
|
|
|
critical_updates: critical_updates_available?,
|
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
installed_sha: (Discourse.git_version == 'unknown' ? nil : Discourse.git_version),
|
|
|
|
missing_versions_count: missing_versions_count,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-07-30 12:11:51 -04:00
|
|
|
if SiteSetting.version_checks?
|
|
|
|
|
|
|
|
# Handle cases when version check data is old so we report something that makes sense
|
|
|
|
|
2013-11-04 12:51:01 -05:00
|
|
|
if (version_info.updated_at.nil? or # never performed a version check
|
|
|
|
last_installed_version != Discourse::VERSION::STRING or # upgraded since the last version check
|
|
|
|
(version_info.missing_versions_count == 0 and version_info.latest_version != version_info.installed_version) or # old data
|
|
|
|
(version_info.missing_versions_count != 0 and version_info.latest_version == version_info.installed_version)) # old data
|
2013-07-30 12:11:51 -04:00
|
|
|
Jobs.enqueue(:version_check, all_sites: true)
|
2013-11-04 12:51:01 -05:00
|
|
|
version_info.version_check_pending = true
|
|
|
|
unless version_info.updated_at.nil?
|
|
|
|
version_info.missing_versions_count = 0
|
|
|
|
version_info.critical_updates = false
|
|
|
|
end
|
2013-07-30 12:11:51 -04:00
|
|
|
end
|
2013-07-03 11:06:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
version_info
|
2013-02-19 15:16:50 -05:00
|
|
|
end
|
|
|
|
|
2013-11-04 12:51:01 -05:00
|
|
|
# last_installed_version is the installed version at the time of the last version check
|
|
|
|
def last_installed_version
|
|
|
|
$redis.get last_installed_version_key
|
|
|
|
end
|
|
|
|
|
2013-02-19 15:16:50 -05:00
|
|
|
def latest_version
|
|
|
|
$redis.get latest_version_key
|
|
|
|
end
|
|
|
|
|
2013-03-05 18:14:51 -05:00
|
|
|
def missing_versions_count
|
|
|
|
$redis.get(missing_versions_count_key).try(:to_i)
|
2013-02-19 15:16:50 -05:00
|
|
|
end
|
|
|
|
|
2013-03-06 10:34:15 -05:00
|
|
|
def critical_updates_available?
|
2013-02-19 15:16:50 -05:00
|
|
|
($redis.get(critical_updates_available_key) || false) == 'true'
|
|
|
|
end
|
|
|
|
|
2013-07-03 11:06:07 -04:00
|
|
|
def updated_at
|
|
|
|
t = $redis.get(updated_at_key)
|
|
|
|
t ? Time.zone.parse(t) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at=(time_with_zone)
|
|
|
|
$redis.set updated_at_key, time_with_zone.as_json
|
|
|
|
end
|
|
|
|
|
2013-11-04 12:51:01 -05:00
|
|
|
['last_installed_version', 'latest_version', 'missing_versions_count', 'critical_updates_available'].each do |name|
|
2013-03-05 18:14:51 -05:00
|
|
|
eval "define_method :#{name}= do |arg|
|
|
|
|
$redis.set #{name}_key, arg
|
|
|
|
end"
|
|
|
|
end
|
|
|
|
|
2013-12-31 15:52:16 -05:00
|
|
|
def missing_versions=(versions)
|
|
|
|
# delete previous list from redis
|
|
|
|
prev_keys = $redis.lrange(missing_versions_list_key, 0, 4)
|
|
|
|
if prev_keys
|
|
|
|
$redis.del prev_keys
|
|
|
|
$redis.del(missing_versions_list_key)
|
|
|
|
end
|
|
|
|
|
2014-01-07 14:31:54 -05:00
|
|
|
if versions.present?
|
|
|
|
# store the list in redis
|
|
|
|
version_keys = []
|
|
|
|
versions[0,5].each do |v|
|
|
|
|
key = "#{missing_versions_key_prefix}:#{v['version']}"
|
|
|
|
$redis.mapped_hmset key, v
|
|
|
|
version_keys << key
|
|
|
|
end
|
|
|
|
$redis.rpush missing_versions_list_key, version_keys
|
2013-12-31 15:52:16 -05:00
|
|
|
end
|
2014-01-07 14:31:54 -05:00
|
|
|
|
|
|
|
versions || []
|
2013-12-31 15:52:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def missing_versions
|
|
|
|
keys = $redis.lrange(missing_versions_list_key, 0, 4) # max of 5 versions
|
|
|
|
keys.present? ? keys.map { |k| $redis.hgetall(k) } : []
|
|
|
|
end
|
|
|
|
|
2013-02-19 15:16:50 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-11-04 12:51:01 -05:00
|
|
|
def last_installed_version_key
|
|
|
|
'last_installed_version'
|
|
|
|
end
|
|
|
|
|
2013-02-19 15:16:50 -05:00
|
|
|
def latest_version_key
|
|
|
|
'discourse_latest_version'
|
|
|
|
end
|
|
|
|
|
|
|
|
def critical_updates_available_key
|
|
|
|
'critical_updates_available'
|
|
|
|
end
|
2013-03-05 18:14:51 -05:00
|
|
|
|
|
|
|
def missing_versions_count_key
|
|
|
|
'missing_versions_count'
|
|
|
|
end
|
2013-07-03 11:06:07 -04:00
|
|
|
|
|
|
|
def updated_at_key
|
|
|
|
'last_version_check_at'
|
|
|
|
end
|
2013-12-31 15:52:16 -05:00
|
|
|
|
|
|
|
def missing_versions_list_key
|
|
|
|
'missing_versions'
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_versions_key_prefix
|
|
|
|
'missing_version'
|
|
|
|
end
|
2013-02-19 15:16:50 -05:00
|
|
|
end
|
|
|
|
end
|