修订版 | 79dcb78eed622c4d09a9c0237748e7b73db90e59 (tree) |
---|---|
时间 | 2016-07-25 16:41:18 |
作者 | Tatsuki Sugiura <sugi@nemu...> |
Commiter | Tatsuki Sugiura |
Add retry on file upload.
@@ -199,9 +199,27 @@ module OSDN; module CLI; module Command | ||
199 | 199 | OSDN::CLI._show_progress = true |
200 | 200 | fio = file.open |
201 | 201 | logger.info "Starting upload #{file}..." |
202 | - finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility | |
203 | - fio.close | |
204 | - OSDN::CLI._show_progress = false | |
202 | + max_upload_tries = 5 | |
203 | + upload_tries = 0 | |
204 | + begin | |
205 | + upload_tries += 1 | |
206 | + finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility | |
207 | + rescue OSDNClient::ApiError => e | |
208 | + if max_upload_tries - upload_tries <= 0 | |
209 | + logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!" | |
210 | + raise e | |
211 | + elsif [0, 100, 502].member?(e.code.to_i) | |
212 | + fio.rewind | |
213 | + logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..." | |
214 | + sleep 10 | |
215 | + retry | |
216 | + else | |
217 | + raise e | |
218 | + end | |
219 | + ensure | |
220 | + OSDN::CLI._show_progress = false | |
221 | + fio.close | |
222 | + end | |
205 | 223 | if digests.find { |type, dig| dig != finfo.send("digest_#{type}") } |
206 | 224 | logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check." |
207 | 225 | else |
@@ -121,9 +121,29 @@ module OSDN; module CLI; module Command | ||
121 | 121 | logger.level <= Logger::INFO && @show_progress != false || @show_progress and |
122 | 122 | OSDN::CLI._show_progress = true |
123 | 123 | logger.info "Starting upload #{file}..." |
124 | - f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility | |
125 | - fio.close | |
126 | - OSDN::CLI._show_progress = false | |
124 | + max_upload_tries = 5 | |
125 | + upload_tries = 0 | |
126 | + f = nil | |
127 | + begin | |
128 | + upload_tries += 1 | |
129 | + f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility | |
130 | + rescue OSDNClient::ApiError => e | |
131 | + if max_upload_tries - upload_tries <= 0 | |
132 | + logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!" | |
133 | + raise e | |
134 | + elsif [0, 100, 502].member?(e.code.to_i) | |
135 | + fio.rewind | |
136 | + logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..." | |
137 | + sleep 10 | |
138 | + retry | |
139 | + else | |
140 | + raise e | |
141 | + end | |
142 | + ensure | |
143 | + fio.close | |
144 | + OSDN::CLI._show_progress = false | |
145 | + end | |
146 | + | |
127 | 147 | if digests.find { |type, dig| dig != f.send("digest_#{type}") } |
128 | 148 | logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check." |
129 | 149 | else |
@@ -92,7 +92,7 @@ module OSDN | ||
92 | 92 | logger.fatal "Command failed by ApiError: #{e.response_body}" |
93 | 93 | end |
94 | 94 | rescue |
95 | - logger.fatal "Command failed: #{e.inspect}" | |
95 | + logger.fatal "Command failed: #{e.inspect} #{e.message} (#{e.code}): #{e.response_body} #{e.response_headers}" | |
96 | 96 | end |
97 | 97 | end |
98 | 98 | end |