File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
+ require 'java_buildpack/diagnostics'
17
18
require 'java_buildpack/util'
18
19
require 'net/http'
19
20
require 'tmpdir'
@@ -32,6 +33,7 @@ class DownloadCache
32
33
def initialize ( cache_root = Dir . tmpdir )
33
34
Dir . mkdir ( cache_root ) unless File . exists? cache_root
34
35
@cache_root = cache_root
36
+ @logger = JavaBuildpack ::Diagnostics ::LoggerFactory . get_logger
35
37
end
36
38
37
39
# Retrieves an item from the cache. Retrieval of the item uses the following algorithm:
@@ -90,6 +92,21 @@ def evict(uri)
90
92
91
93
private
92
94
95
+ HTTP_ERRORS = [
96
+ EOFError ,
97
+ Errno ::ECONNREFUSED ,
98
+ Errno ::ECONNRESET ,
99
+ Errno ::EHOSTUNREACH ,
100
+ Errno ::EINVAL ,
101
+ Errno ::EPIPE ,
102
+ Errno ::ETIMEDOUT ,
103
+ Net ::HTTPBadResponse ,
104
+ Net ::HTTPHeaderSyntaxError ,
105
+ Net ::ProtocolError ,
106
+ SocketError ,
107
+ Timeout ::Error
108
+ ]
109
+
93
110
def delete_file ( filename )
94
111
File . delete filename if File . exists? filename
95
112
end
@@ -103,6 +120,10 @@ def download(filenames, uri)
103
120
write_response ( filenames , response )
104
121
end
105
122
end
123
+
124
+ rescue *HTTP_ERRORS
125
+ puts 'FAIL'
126
+ raise "Unable to download from #{ uri } "
106
127
end
107
128
108
129
def filenames ( uri )
@@ -151,6 +172,9 @@ def update(filenames, uri)
151
172
write_response ( filenames , response ) unless response . code == '304'
152
173
end
153
174
end
175
+
176
+ rescue *HTTP_ERRORS
177
+ @logger . warn "Unable to update from #{ uri } . Using cached version."
154
178
end
155
179
156
180
def use_ssl? ( uri )
Original file line number Diff line number Diff line change @@ -21,6 +21,10 @@ module JavaBuildpack::Util
21
21
22
22
describe DownloadCache do
23
23
24
+ before do
25
+ $stderr = StringIO . new
26
+ end
27
+
24
28
it 'should download from a uri if the cached file does not exist' do
25
29
stub_request ( :get , 'http://foo-uri/' ) . to_return (
26
30
status : 200 ,
@@ -40,6 +44,14 @@ module JavaBuildpack::Util
40
44
end
41
45
end
42
46
47
+ it 'should raise error if download cannot be completed' do
48
+ stub_request ( :get , 'http://foo-uri/' ) . to_raise ( SocketError )
49
+
50
+ Dir . mktmpdir do |root |
51
+ expect { DownloadCache . new ( root ) . get ( 'http://foo-uri/' ) { } } . to raise_error
52
+ end
53
+ end
54
+
43
55
it 'should download from a uri if the cached file exists and etag exists' do
44
56
stub_request ( :get , 'http://foo-uri/' ) . with (
45
57
headers : {
@@ -66,6 +78,17 @@ module JavaBuildpack::Util
66
78
end
67
79
end
68
80
81
+ it 'should use cached copy if update cannot be completed' do
82
+ stub_request ( :get , 'http://foo-uri/' ) . to_raise ( SocketError )
83
+
84
+ Dir . mktmpdir do |root |
85
+ touch root , 'cached' , 'foo-cached'
86
+ touch root , 'etag' , 'foo-etag'
87
+
88
+ DownloadCache . new ( root ) . get ( 'http://foo-uri/' ) { }
89
+ end
90
+ end
91
+
69
92
it 'should download from a uri if the cached file exists and last modified exists' do
70
93
stub_request ( :get , 'http://foo-uri/' ) . with (
71
94
headers : {
You can’t perform that action at this time.
0 commit comments