Skip to content

Commit f1e5479

Browse files
authored
Merge pull request #474 from wordpress-mobile/auto-retry-gp_downloadmetadata
Adds the option to auto-retry for downloading metadata from GlotPress
2 parents 4df4c38 + dc56ef0 commit f1e5479

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _None_
1010

1111
### New Features
1212

13-
_None_
13+
- Adds auto_retry option to `gp_downloadmetadata_action`. [#474]
1414

1515
### Bug Fixes
1616

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ def self.run(params)
1010
UI.message "Locales: #{params[:locales].inspect}"
1111
UI.message "Source locale: #{params[:source_locale].nil? ? '-' : params[:source_locale]}"
1212
UI.message "Path: #{params[:download_path]}"
13+
UI.message "Auto-retry: #{params[:auto_retry]}"
1314

1415
# Check download path
1516
FileUtils.mkdir_p(params[:download_path])
1617

1718
# Download
18-
downloader = Fastlane::Helper::MetadataDownloader.new(params[:download_path], params[:target_files])
19+
downloader = Fastlane::Helper::MetadataDownloader.new(params[:download_path], params[:target_files], params[:auto_retry])
1920

2021
params[:locales].each do |loc|
2122
if loc.is_a?(Array)
@@ -68,6 +69,12 @@ def self.available_options
6869
env_name: 'FL_DOWNLOAD_METADATA_DOWNLOAD_PATH',
6970
description: 'The path of the target files',
7071
type: String),
72+
FastlaneCore::ConfigItem.new(key: :auto_retry,
73+
env_name: 'FL_DOWNLOAD_METADATA_AUTO_RETRY',
74+
description: 'Whether to auto retry downloads after Too Many Requests error',
75+
type: FastlaneCore::Boolean,
76+
optional: true,
77+
default_value: true),
7178
]
7279
end
7380

lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
module Fastlane
55
module Helper
66
class MetadataDownloader
7+
AUTO_RETRY_SLEEP_TIME = 20
8+
MAX_AUTO_RETRY_ATTEMPTS = 30
9+
710
attr_reader :target_folder, :target_files
811

9-
def initialize(target_folder, target_files)
12+
def initialize(target_folder, target_files, auto_retry)
1013
@target_folder = target_folder
1114
@target_files = target_files
15+
@auto_retry = auto_retry
1216
@alternates = {}
17+
@auto_retry_attempt_counter = 0
1318
end
1419

1520
# Downloads data from GlotPress, in JSON format
@@ -112,8 +117,13 @@ def handle_glotpress_download(response:, locale:, is_source:)
112117
UI.message("Received 301 for `#{locale}`. Following redirect...")
113118
download(locale, response.header['location'], is_source)
114119
when '429'
115-
# We got rate-limited, offer to try again
116-
if UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?")
120+
# We got rate-limited, auto_retry or offer to try again with a prompt
121+
if @auto_retry && @auto_retry_attempt_counter <= MAX_AUTO_RETRY_ATTEMPTS
122+
UI.message("Received 429 for `#{locale}`. Auto retrying in #{AUTO_RETRY_SLEEP_TIME} seconds...")
123+
sleep(AUTO_RETRY_SLEEP_TIME)
124+
@auto_retry_attempt_counter += 1
125+
download(locale, response.uri, is_source)
126+
elsif UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?")
117127
download(locale, response.uri, is_source)
118128
else
119129
UI.error("Abandoning `#{locale}` download as requested.")

0 commit comments

Comments
 (0)