diff --git a/CHANGELOG.md b/CHANGELOG.md index c012fa712..cde10c5c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ _None_ ### New Features -_None_ +- Adds auto_retry option to `gp_downloadmetadata_action`. [#474] ### Bug Fixes diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb index acc002a56..2de44300b 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb @@ -10,12 +10,13 @@ def self.run(params) UI.message "Locales: #{params[:locales].inspect}" UI.message "Source locale: #{params[:source_locale].nil? ? '-' : params[:source_locale]}" UI.message "Path: #{params[:download_path]}" + UI.message "Auto-retry: #{params[:auto_retry]}" # Check download path FileUtils.mkdir_p(params[:download_path]) # Download - downloader = Fastlane::Helper::MetadataDownloader.new(params[:download_path], params[:target_files]) + downloader = Fastlane::Helper::MetadataDownloader.new(params[:download_path], params[:target_files], params[:auto_retry]) params[:locales].each do |loc| if loc.is_a?(Array) @@ -68,6 +69,12 @@ def self.available_options env_name: 'FL_DOWNLOAD_METADATA_DOWNLOAD_PATH', description: 'The path of the target files', type: String), + FastlaneCore::ConfigItem.new(key: :auto_retry, + env_name: 'FL_DOWNLOAD_METADATA_AUTO_RETRY', + description: 'Whether to auto retry downloads after Too Many Requests error', + type: FastlaneCore::Boolean, + optional: true, + default_value: true), ] end diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb b/lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb index a31add3aa..bdae83c68 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb @@ -4,12 +4,17 @@ module Fastlane module Helper class MetadataDownloader + AUTO_RETRY_SLEEP_TIME = 20 + MAX_AUTO_RETRY_ATTEMPTS = 30 + attr_reader :target_folder, :target_files - def initialize(target_folder, target_files) + def initialize(target_folder, target_files, auto_retry) @target_folder = target_folder @target_files = target_files + @auto_retry = auto_retry @alternates = {} + @auto_retry_attempt_counter = 0 end # Downloads data from GlotPress, in JSON format @@ -112,8 +117,13 @@ def handle_glotpress_download(response:, locale:, is_source:) UI.message("Received 301 for `#{locale}`. Following redirect...") download(locale, response.header['location'], is_source) when '429' - # We got rate-limited, offer to try again - if UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?") + # We got rate-limited, auto_retry or offer to try again with a prompt + if @auto_retry && @auto_retry_attempt_counter <= MAX_AUTO_RETRY_ATTEMPTS + UI.message("Received 429 for `#{locale}`. Auto retrying in #{AUTO_RETRY_SLEEP_TIME} seconds...") + sleep(AUTO_RETRY_SLEEP_TIME) + @auto_retry_attempt_counter += 1 + download(locale, response.uri, is_source) + elsif UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?") download(locale, response.uri, is_source) else UI.error("Abandoning `#{locale}` download as requested.")