Skip to content

Commit dea79fc

Browse files
committed
Retry git-sync-check
1 parent bcfa6a8 commit dea79fc

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

bin/git-sync-check.rb

+53-30
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,64 @@ def post(url, payload:)
6363
end
6464
end
6565

66-
begin
67-
# Quickly finish collecting facts to avoid a race condition as much as possible.
68-
# TODO: Retry this operation several times if the race happens often.
69-
ls_remote = Git.ls_remote('github')
70-
show_ref = Git.show_ref
71-
72-
# Start digesting the data after the collection.
73-
remote_refs = Hash[ls_remote.lines.map { |l| rev, ref = l.chomp.split("\t"); [ref, rev] }]
74-
local_refs = Hash[show_ref.lines.map { |l| rev, ref = l.chomp.split(' '); [ref, rev] }]
75-
76-
# Remove refs which are not to be checked here.
77-
remote_refs.delete('HEAD') # show-ref does not show it
78-
remote_refs.keys.each { |ref| remote_refs.delete(ref) if ref.match(%r[\Arefs/pull/\d+/\w+\z]) } # pull requests
79-
80-
# Check consistency
81-
errors = []
82-
(remote_refs.keys | local_refs.keys).each do |ref|
83-
remote_rev = remote_refs[ref]
84-
local_rev = local_refs[ref]
85-
86-
if remote_rev != local_rev
87-
errors << [remote_rev, local_rev]
66+
module GitSyncCheck
67+
class Errors < StandardError
68+
attr_reader :errors
69+
70+
def initialize(errors)
71+
@errors = errors
72+
super('git-sync-check failed')
8873
end
8974
end
9075

91-
if errors.empty?
92-
puts 'SUCCUESS: Everything is consistent.'
93-
else
94-
message = "FAILURE: Following inconsistencies are found.\n"
95-
errors.each do |remote_rev, local_rev|
96-
message << "remote:#{remote_rev.inspect} local:#{local_rev.inspect}\n"
76+
def self.check_consistency
77+
# Quickly finish collecting facts to avoid a race condition as much as possible.
78+
# TODO: Retry this operation several times if the race happens often.
79+
ls_remote = Git.ls_remote('github')
80+
show_ref = Git.show_ref
81+
82+
# Start digesting the data after the collection.
83+
remote_refs = Hash[ls_remote.lines.map { |l| rev, ref = l.chomp.split("\t"); [ref, rev] }]
84+
local_refs = Hash[show_ref.lines.map { |l| rev, ref = l.chomp.split(' '); [ref, rev] }]
85+
86+
# Remove refs which are not to be checked here.
87+
remote_refs.delete('HEAD') # show-ref does not show it
88+
remote_refs.keys.each { |ref| remote_refs.delete(ref) if ref.match(%r[\Arefs/pull/\d+/\w+\z]) } # pull requests
89+
90+
# Check consistency
91+
errors = {}
92+
(remote_refs.keys | local_refs.keys).each do |ref|
93+
remote_rev = remote_refs[ref]
94+
local_rev = local_refs[ref]
95+
96+
if remote_rev != local_rev
97+
errors[ref] = [remote_rev, local_rev]
98+
end
9799
end
98-
Slack.notify(message)
99-
puts message
100+
101+
unless errors.empty?
102+
raise Errors.new(errors)
103+
end
104+
end
105+
end
106+
107+
attempts = 3
108+
begin
109+
GitSyncCheck.check_consistency
110+
puts 'SUCCUESS: Everything is consistent.'
111+
rescue GitSyncCheck::Errors => e
112+
attempts -= 1
113+
if attempts > 0
114+
sleep 5
115+
retry
116+
end
117+
118+
message = "FAILURE: Following inconsistencies are found.\n"
119+
e.errors.each do |ref, (remote_rev, local_rev)|
120+
message << "ref:#{ref.inspect} remote:#{remote_rev.inspect} local:#{local_rev.inspect}\n"
100121
end
122+
Slack.notify(message)
123+
puts message
101124
rescue => e
102125
Slack.notify("#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}")
103126
end

0 commit comments

Comments
 (0)