Update check: a failed one or CI still running is asked again after a minute, not cached for 10
This commit is contained in:
1 parent
e03e426eb6
commit
e7f3b91337
1 file changed
+9
-2
@@ -10,6 +10,7 @@ module Maptool
|
||||
# back in data/update/status.json.
|
||||
module Updater
|
||||
CHECK_EVERY_S = 600
|
||||
RETRY_AFTER_S = 60 # a check that failed (offline, a commit CI hasn't seen yet)
|
||||
MUTEX = Mutex.new
|
||||
|
||||
def self.dir(data_dir)
|
||||
@@ -20,11 +21,17 @@ module Maptool
|
||||
# CHECK_EVERY_S; nil while Gitea can't be reached (no internet on the road).
|
||||
def self.latest(gitea_url, repo, fetch: method(:get_json))
|
||||
MUTEX.synchronize do
|
||||
return @latest if @checked_at && Time.now.to_f - @checked_at < CHECK_EVERY_S
|
||||
# CI still running (or the check failed): sooner, so the button shows when it's through
|
||||
return @latest if @checked_at && Time.now.to_f - @checked_at < (@latest&.dig(:ci) == "success" ? CHECK_EVERY_S : RETRY_AFTER_S)
|
||||
|
||||
@checked_at = Time.now.to_f
|
||||
sha = fetch.call("#{gitea_url}/api/v1/repos/#{repo}/branches/main").dig("commit", "id")
|
||||
@latest = { sha: sha, ci: fetch.call("#{gitea_url}/api/v1/repos/#{repo}/commits/#{sha}/status")["state"] }
|
||||
ci = begin
|
||||
fetch.call("#{gitea_url}/api/v1/repos/#{repo}/commits/#{sha}/status")["state"]
|
||||
rescue StandardError
|
||||
nil # no status yet: not offered, asked again in a minute
|
||||
end
|
||||
@latest = ci ? { sha: sha, ci: ci } : nil
|
||||
rescue StandardError
|
||||
@latest = nil
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user