esi-utils-bvv 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/Gemfile.lock +1 -1
- data/lib/esi-utils-bvv/esiclient.rb +58 -4
- data/lib/esi-utils-bvv/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 721ec43d7544b384b2eefb643265f9297c2f0828
|
4
|
+
data.tar.gz: 4112d15b22ac7a5e9293e2d1efcbe0e81bf73550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b1edf6378608de30b8d9752ad8c0b4784f2fff3f05c7b4f940b5bb5cf479ebd4a14eaa32b3e57225dfc304d08d9f8111137ffe7a74f9d8369040f54e94016b9
|
7
|
+
data.tar.gz: 0ce641c80e98a226ee0ab5c78fc339491f148399d8fbc4ea576e2d7c40e48bc04183e1967ffac9e0071325e3229ca5647cf4ff5a86e2d4029c8e7d6237e8955d
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -12,6 +12,7 @@ module ESIUtils
|
|
12
12
|
def initialize
|
13
13
|
super
|
14
14
|
@seen_warnings = Set.new
|
15
|
+
@delay_times = [0, 0.5, 1, 2, 5, 15].freeze
|
15
16
|
end
|
16
17
|
|
17
18
|
def log_warning_header(path, headers)
|
@@ -21,13 +22,66 @@ module ESIUtils
|
|
21
22
|
# Only notify about a given (genericised) path once
|
22
23
|
return if @seen_warnings.include?(g_path)
|
23
24
|
@seen_warnings.add(g_path)
|
24
|
-
puts("Warning: '#{warning}' on path '#{g_path}")
|
25
|
+
puts("Warning: '#{warning}' on path '#{g_path}'")
|
25
26
|
end
|
26
27
|
|
28
|
+
#
|
29
|
+
# Does the given HTTP status code represent something
|
30
|
+
# that we should retry?
|
31
|
+
#
|
32
|
+
# 420 Error Limited
|
33
|
+
# 502 Bad Gateway
|
34
|
+
# 503 Service Unavailable
|
35
|
+
# 504 Gateway Timeout
|
36
|
+
#
|
37
|
+
def retry_status?(code)
|
38
|
+
case code
|
39
|
+
when 420, 502, 503, 504
|
40
|
+
true
|
41
|
+
else
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Can we retry?
|
48
|
+
#
|
49
|
+
# Based on the details of the APIError exception along
|
50
|
+
# with an array of remaining retry delays.
|
51
|
+
#
|
52
|
+
# If retrying is possible, the result is a number of seconds
|
53
|
+
# to delay before performing the next attempt.
|
54
|
+
#
|
55
|
+
def can_retry?(api_error, delays)
|
56
|
+
# Can't retry if we have exhausted our list of retry delays.
|
57
|
+
return false if delays.empty?
|
58
|
+
# Only certain status codes are retryable.
|
59
|
+
return false unless retry_status?(api_error.code)
|
60
|
+
# Peel off a time to delay for before retrying.
|
61
|
+
delays.shift
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Customised call_api, which wraps itself around the one
|
66
|
+
# provided by the Swagger generated code.
|
67
|
+
#
|
68
|
+
# Adds:
|
69
|
+
#
|
70
|
+
# * Logging of warning headers when the API is versioned.
|
71
|
+
# * Automatic retrying of temporary failures.
|
72
|
+
#
|
27
73
|
def call_api(http_method, path, opts = {})
|
28
|
-
|
29
|
-
|
30
|
-
|
74
|
+
delays = @delay_times.dup
|
75
|
+
begin
|
76
|
+
data, code, headers = super(http_method, path, opts)
|
77
|
+
log_warning_header(path, headers)
|
78
|
+
[data, code, headers]
|
79
|
+
rescue ESI::ApiError => api_error
|
80
|
+
raise unless (secs = can_retry?(api_error, delays))
|
81
|
+
puts("Retrying #{http_method} on '#{path}' after #{secs}")
|
82
|
+
sleep(secs)
|
83
|
+
retry
|
84
|
+
end
|
31
85
|
end
|
32
86
|
end
|
33
87
|
end
|