etcenv 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a34bc3d33dbd8d1c025fe6ff53a5272ea9d9f91e
4
- data.tar.gz: 301aa17b8229307c74326d18d025a7663c200735
3
+ metadata.gz: 71bb85e40d0ef6634a604310ebe76b3a7707244e
4
+ data.tar.gz: 4bfeab28a6562cc8fb7e259cafccc92396a3d198
5
5
  SHA512:
6
- metadata.gz: 5603e6e5b8ed263db00bef09ff903186b01a551a3550e0def6ff352a30d16354169f78be0bc802b2b51b1a791d6f284c06edb72bb95ef3555779cb62f8512bd0
7
- data.tar.gz: fb36bb81a2651e41cbb787d635d23ead2138a5f42f05bfc23bb415f9fb70db3a33ad33a75dba30f230bf4eac14fa416bb884cfc1b228acd7747f9de790bf93d3
6
+ metadata.gz: 5837fa617120b09edf6fa744bf93d48e499bd4fd90aa4537a761c409ded2c804a4bb7142fe00ef495c59a2bd11d5814c7e54fad61994e3f083f5e51a20689841
7
+ data.tar.gz: 6968faa4d765dca8c58097adfdbf1d395133018f0075e22e4f9d898b1c8de8ca06ea2a69355e79f683c3076b0b3b6ab29e8df60db09164e107dfb43a0fa008ee
@@ -21,7 +21,7 @@ module Etcenv
21
21
  load
22
22
  end
23
23
 
24
- attr_reader :root_key, :env, :etcd
24
+ attr_reader :root_key, :env, :etcd, :cluster_index
25
25
  attr_accessor :max_depth
26
26
 
27
27
  def expanded_env
@@ -32,6 +32,10 @@ module Etcenv
32
32
  @modified_indices ||= {}
33
33
  end
34
34
 
35
+ def keys
36
+ modified_indices.keys
37
+ end
38
+
35
39
  def load
36
40
  @lock.synchronize do
37
41
  flush
@@ -52,6 +56,7 @@ module Etcenv
52
56
  @includes = nil
53
57
  @cache = {}
54
58
  @modified_indices = {}
59
+ @cluster_index = nil
55
60
  self
56
61
  end
57
62
 
@@ -79,7 +84,9 @@ module Etcenv
79
84
  key = resolve_key(name)
80
85
  return cache[key] if cache[key]
81
86
 
82
- node = @etcd.get(key).node
87
+ resp = @etcd.get(key)
88
+ node = resp.node
89
+ @cluster_index = [@cluster_index, resp.etcd_index].compact.min
83
90
 
84
91
  if node.etcvault_error
85
92
  raise EtcvaultFailure, node.etcvault_error
@@ -1,3 +1,3 @@
1
1
  module Etcenv
2
- VERSION = "0.5.4"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -19,8 +19,8 @@ module Etcenv
19
19
 
20
20
  def watch
21
21
  ch = Queue.new
22
- threads = env.modified_indices.map do |key, index|
23
- Thread.new(ch, key, index, &method(:watch_thread)).tap do |th|
22
+ threads = env.keys.map do |key|
23
+ Thread.new(ch, key, env.cluster_index, &method(:watch_thread)).tap do |th|
24
24
  th.abort_on_exception = true
25
25
  end
26
26
  end
@@ -30,6 +30,7 @@ module Etcenv
30
30
  end
31
31
 
32
32
  def auto_reload_loop
33
+ retries = 0
33
34
  loop do
34
35
  begin
35
36
  watch
@@ -37,8 +38,15 @@ module Etcenv
37
38
  env.load
38
39
  yield env if block_given?
39
40
  rescue => e
41
+ retries += 1
42
+ interval = (2**retries) * 0.1
43
+
40
44
  $stderr.puts "[watcher][error] Failed to reload env #{env.root_key}: #{e.inspect}"
41
45
  $stderr.puts "\t#{e.backtrace.join("\n\t")}"
46
+ $stderr.puts "[watcher][error] RETRYING reload #{env.root_key} in #{'%.2f' % interval} sec"
47
+ sleep interval
48
+ else
49
+ retries = 0
42
50
  end
43
51
  end
44
52
  end
@@ -46,8 +54,24 @@ module Etcenv
46
54
  private
47
55
 
48
56
  def watch_thread(ch, key, index)
57
+ tries = 0
58
+ loop do
59
+ if try_watch(key, index)
60
+ ch << key
61
+ break
62
+ end
63
+ interval = (2 ** tries) * 0.1
64
+ $stderr.puts "[watcher] RETRYING; #{key.inspect} watch will resume after #{'%.2f' % interval} sec"
65
+ sleep interval
66
+ tries += 1
67
+ end
68
+ end
69
+
70
+ def try_watch(key, index)
49
71
  $stderr.puts "[watcher] waiting for change on #{key} (index: #{index.succ})" if verbose
50
- response = etcd.watch(key, recursive: true, index: [@indices[key] || 0, index].max.succ, timeout: WATCH_TIMEOUT)
72
+ index = [@indices[key], index].compact.max
73
+ index += 1 if index
74
+ response = etcd.watch(key, recursive: true, index: index, timeout: WATCH_TIMEOUT)
51
75
  @lock.synchronize do
52
76
  # Record modified_index in watcher itself; Because the latest index may be hidden in normal response
53
77
  # e.g. unlisted keys, removed keys
@@ -55,8 +79,17 @@ module Etcenv
55
79
  end
56
80
  $stderr.puts "[watcher] dir #{key} has updated" if verbose
57
81
  ch << key
82
+ return true
83
+ rescue Etcd::EventIndexCleared => e
84
+ $stderr.puts "[watcher][warn] #{e.inspect} on key #{key.inspect}, trying to get X-Etcd-Index" if verbose
85
+ @lock.synchronize do
86
+ @indices[key] = etcd.get(key).etcd_index
87
+ end
88
+ $stderr.puts "[watcher][warn] Updated #{key.inspect} index to #{@indices[key]}" if verbose
89
+ return nil
58
90
  rescue Net::ReadTimeout
59
- retry
91
+ $stderr.puts "[watcher] #{e.inspect} on key #{key.inspect}" if verbose
92
+ return nil
60
93
  end
61
94
  end
62
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etcenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Fukumori (sora_h)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  version: '0'
129
129
  requirements: []
130
130
  rubyforge_project:
131
- rubygems_version: 2.4.5
131
+ rubygems_version: 2.6.3
132
132
  signing_key:
133
133
  specification_version: 4
134
134
  summary: Dump etcd keys into dotenv file or docker env file