OSDN Git Service

Add retry on file upload.
authorTatsuki Sugiura <sugi@nemui.org>
Mon, 25 Jul 2016 07:41:18 +0000 (16:41 +0900)
committerTatsuki Sugiura <sugi@nemui.org>
Mon, 25 Jul 2016 07:41:18 +0000 (16:41 +0900)
lib/osdn/cli/command/frs_upload.rb
lib/osdn/cli/command/relfile.rb
lib/osdn/cli/runner.rb

index b6a473a..ad2bac4 100644 (file)
@@ -199,9 +199,27 @@ module OSDN; module CLI; module Command
           OSDN::CLI._show_progress = true
         fio = file.open
         logger.info "Starting upload #{file}..."
-        finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility
-        fio.close
-        OSDN::CLI._show_progress = false
+        max_upload_tries = 5
+        upload_tries = 0
+        begin
+          upload_tries += 1
+          finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility
+        rescue OSDNClient::ApiError => e
+          if max_upload_tries - upload_tries <= 0 
+            logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!"
+            raise e
+          elsif [0, 100, 502].member?(e.code.to_i)
+            fio.rewind
+            logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..."
+            sleep 10
+            retry
+          else
+            raise e
+          end
+        ensure
+          OSDN::CLI._show_progress = false
+          fio.close
+        end
         if digests.find { |type, dig| dig != finfo.send("digest_#{type}") }
           logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check."
         else
index b7ad732..de060ac 100644 (file)
@@ -121,9 +121,29 @@ module OSDN; module CLI; module Command
       logger.level <= Logger::INFO && @show_progress != false || @show_progress and
         OSDN::CLI._show_progress = true
       logger.info "Starting upload #{file}..."
-      f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility
-      fio.close
-      OSDN::CLI._show_progress = false
+      max_upload_tries = 5
+      upload_tries = 0
+      f = nil
+      begin
+        upload_tries += 1
+        f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility
+      rescue OSDNClient::ApiError => e
+        if max_upload_tries - upload_tries <= 0 
+          logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!"
+          raise e
+        elsif [0, 100, 502].member?(e.code.to_i)
+          fio.rewind
+          logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..."
+          sleep 10
+          retry
+        else
+          raise e
+        end
+      ensure
+        fio.close
+        OSDN::CLI._show_progress = false
+      end
+      
       if digests.find { |type, dig| dig != f.send("digest_#{type}") }
         logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check."
       else
index 1c5cdd9..874b01c 100644 (file)
@@ -92,7 +92,7 @@ module OSDN
               logger.fatal "Command failed by ApiError: #{e.response_body}"
             end
           rescue
-            logger.fatal "Command failed: #{e.inspect}"
+            logger.fatal "Command failed: #{e.inspect} #{e.message} (#{e.code}): #{e.response_body} #{e.response_headers}"
           end
         end
       end