s3_meta_sync 0.10.0 → 0.11.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6235bd32a6abcd45233ba1051b3069b051989565
4
- data.tar.gz: eaf9169f2d31c82d4da45ca5f277aac62ecbc179
3
+ metadata.gz: e68ff7b84c212d7cc29205287b35c9fa181303c1
4
+ data.tar.gz: ed8e7d21833f827d6e10e092d6a257fb3b03e29e
5
5
  SHA512:
6
- metadata.gz: 992e315158c7980790e148a1ce34d2152fb57b4b25d66f66f79b6245323f80ddf729586bfd67a81d067aee840d2d6a107d0f4897c3df59f13c0dbe554d902a2f
7
- data.tar.gz: 5e3326f634a9865bd9e840f262f793ef055c869e6c8f76b8a9c868ae943dcef5834be84e2ede43eafad7aeb93298a37d5641071224f6583137195db6e203a79f
6
+ metadata.gz: b0e3edc1923c29beed3247808673e97d805f674564ab2ff1bf4133d2035f04d5af6b7e7a6ea374d8191342a327b8c55414e09a4f5ce01a2fc0282a5a6d174cf4
7
+ data.tar.gz: 0b2ebf35c1eb061fc6d3ff6bb47ce9b8818cba8622142e739bdc6f8601946262a74b77213dc5c9c10b8fbc8928cc75d53a607cfccc8ba8953123171a89bc853e
data/lib/s3_meta_sync.rb CHANGED
@@ -42,6 +42,7 @@ module S3MetaSync
42
42
  opts.on("--ssl-none", "Do not verify ssl certs") { options[:ssl_none] = true }
43
43
  opts.on("-z", "--zip", "Zip when uploading to save bandwidth") { options[:zip] = true }
44
44
  opts.on("--no-local-changes", "Do not md5 all the local files, they did not change") { options[:no_local_changes] = true }
45
+ opts.on("--retries MAX", Integer, "MAX number of times retrying failed http requests default: 2") { |c| options[:max_retries] = c }
45
46
  opts.on("-V", "--verbose", "Verbose mode"){ options[:verbose] = true }
46
47
  opts.on("-h", "--help", "Show this.") { puts opts; exit }
47
48
  opts.on("-v", "--version", "Show Version") { puts VERSION; exit}
@@ -11,6 +11,7 @@ require "s3_meta_sync/zip"
11
11
  module S3MetaSync
12
12
  class Syncer
13
13
  DEFAULT_REGION = 'us-east-1'
14
+ STAGING_AREA_PREFIX = "s3ms_"
14
15
 
15
16
  def initialize(config)
16
17
  @config = config
@@ -59,6 +60,8 @@ module S3MetaSync
59
60
  end
60
61
 
61
62
  def download(source, destination)
63
+ delete_old_temp_folders
64
+
62
65
  remote_meta = download_meta(source)
63
66
  local_files = ((@config[:no_local_changes] && read_meta(destination)) || meta_data(destination))[:files]
64
67
 
@@ -68,7 +71,8 @@ module S3MetaSync
68
71
  log "Downloading: #{download.size} Deleting: #{delete.size}", true
69
72
 
70
73
  if download.any? || delete.any?
71
- Dir.mktmpdir do |staging_area|
74
+ Dir.mktmpdir(STAGING_AREA_PREFIX) do |staging_area|
75
+ log "Staging area: #{staging_area}"
72
76
  FileUtils.mkdir_p(destination)
73
77
  copy_content(destination, staging_area)
74
78
  download_files(source, staging_area, download, remote_meta[:zip])
@@ -83,7 +87,21 @@ module S3MetaSync
83
87
  end
84
88
  end
85
89
 
90
+ # Sometimes SIGTERM causes Dir.mktmpdir to not properly delete the temp folder
91
+ # Remove 1 day old folders
92
+ def delete_old_temp_folders
93
+ path = File.join(Dir.tmpdir, STAGING_AREA_PREFIX + '*')
94
+
95
+ day = 24 * 60 * 60
96
+ dirs = Dir.glob(path)
97
+ dirs.select! { |dir| Time.now.utc - File.ctime(dir).utc > day } # only stale ones
98
+ removed = dirs.each { |dir| FileUtils.rm_rf(dir) }
99
+
100
+ log "Removed #{removed} old temp folder(s)" if removed.count > 0
101
+ end
102
+
86
103
  def copy_content(destination, dir)
104
+ log "Copying content from #{destination} to #{dir}"
87
105
  system "cp -R #{destination}/* #{dir} 2>/dev/null"
88
106
  end
89
107
 
@@ -209,13 +227,13 @@ module S3MetaSync
209
227
  content = download_content("#{destination}/#{META_FILE}") { |io| io.read }
210
228
  parse_yaml_content(content)
211
229
  rescue OpenURI::HTTPError
212
- retries ||= 1
213
- if retries == 2
214
- raise RemoteWithoutMeta
215
- else
216
- retries += 1
230
+ retries ||= 0
231
+ retries += 1
232
+ if retries <= 1
217
233
  sleep 1 # maybe the remote meta was just updated ... give aws a second chance ...
218
234
  retry
235
+ else
236
+ raise RemoteWithoutMeta
219
237
  end
220
238
  end
221
239
 
@@ -244,22 +262,27 @@ module S3MetaSync
244
262
  "https://s3#{"-#{region}" if region}.amazonaws.com/#{@bucket}/#{path}"
245
263
  end
246
264
  options = (@config[:ssl_none] ? {:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE} : {})
247
- open(url, options)
248
- rescue OpenURI::HTTPError
249
- retries ||= 0
250
- retries += 1
251
- if retries >= 3
265
+ retry_downloads(url: url) { open(url, options) }
266
+ end
267
+
268
+ def retry_downloads(url:)
269
+ yield
270
+ rescue OpenURI::HTTPError, Errno::ECONNRESET => e
271
+ max_retries = @config[:max_retries] || 2
272
+ http_error_retries ||= 0
273
+ http_error_retries += 1
274
+ if http_error_retries <= max_retries
275
+ log "#{e.class} error downloading #{url}, retrying #{http_error_retries}/#{max_retries}"
276
+ retry
277
+ else
252
278
  $!.message << " -- while trying to download #{url}"
253
279
  raise
254
- else
255
- log "HTTP Error downloading #{path}, retrying"
256
- retry
257
280
  end
258
281
  rescue OpenSSL::SSL::SSLError
259
- retries ||= 0
260
- retries += 1
261
- if retries == 1
262
- log "SSL error downloading #{path}, retrying"
282
+ ssl_error_retries ||= 0
283
+ ssl_error_retries += 1
284
+ if ssl_error_retries == 1
285
+ log "SSL error downloading #{url}, retrying"
263
286
  retry
264
287
  else
265
288
  raise
@@ -1,3 +1,3 @@
1
1
  module S3MetaSync
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3_meta_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-20 00:00:00.000000000 Z
11
+ date: 2017-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.5.1
59
+ rubygems_version: 2.6.11
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Sync folders with s3 using a metadata file and md5 diffs