teamd-discover 0.2.0 → 0.3.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: 82fba18d3cb946fe4de42b630f7e8660a1a08c7b
4
- data.tar.gz: e3cedb935bfdab0f4961818a776e00d466c13f14
3
+ metadata.gz: c06635e4f5ca0c56cd1467cafd4c262de1776918
4
+ data.tar.gz: 8d66e2d83f34f57f43ef6e7bc10393e4eb48bd6d
5
5
  SHA512:
6
- metadata.gz: 0daf8d401d4d88790b94662f3f8116c23c88ac1a115ac77f29bb3db3230d3e3ed6d8900c9ad05f418ebec0e7544e376d44588bb373c54aa774a72452564b84d1
7
- data.tar.gz: eb6dbf912dc17469fa530bc136e69da273115936a5ef5bbfc153722f81add07866a679484b14e062a8212f90c46965b415aa07a9eb48700fee61c77fc546b229
6
+ metadata.gz: 269219ef0493dde5ee5eeb39cd6d908ede5a8c13b6cfd35fdbdee75b818c972650cb3f25db584cc3c9c6573907ff4455b707116f7d51e174bc7f7e59747bb0e7
7
+ data.tar.gz: 675229be6ff062034698bc285382584473505036f688a6cebb1bc87cf7cabb3a35b67910c59616b7f6dcbe417de56c8ac5009ff82b25a94ee48350141d274463
@@ -0,0 +1,29 @@
1
+ class QueueWithTimeout
2
+ def initialize
3
+ @mutex = Mutex.new
4
+ @queue = []
5
+ @recieved = ConditionVariable.new
6
+ end
7
+
8
+ def push(x)
9
+ @mutex.synchronize do
10
+ @queue.push x
11
+ @recieved.signal
12
+ end
13
+ end
14
+
15
+ def pop(non_block = false)
16
+ pop_with_timeout(non_block ? 0 : nil)
17
+ end
18
+
19
+ def pop_with_timeout(timeout = nil)
20
+ @mutex.synchronize do
21
+ if @queue.empty?
22
+ @recieved.wait(@mutex, timeout) if timeout != 0
23
+ #if we're still empty after the timeout, raise exception
24
+ raise ThreadError, "queue empty" if @queue.empty?
25
+ end
26
+ @queue.shift
27
+ end
28
+ end
29
+ end
@@ -18,9 +18,12 @@ module Teamd
18
18
  end
19
19
  desc "discover","One shot discovery"
20
20
  def discover
21
- token = Cli.peer.discover
22
- url = "ETCD_DISCOVERY=https://discovery.etcd.io/#{token}"
23
- puts url
21
+ if token = Cli.peer.discover
22
+ url = "ETCD_DISCOVERY=https://discovery.etcd.io/#{token}"
23
+ puts url
24
+ else
25
+ exit(1)
26
+ end
24
27
  end
25
28
  end
26
29
  end
@@ -1,13 +1,18 @@
1
1
  require "socket"
2
2
  require "securerandom"
3
+ require "queue_with_timeout"
3
4
 
4
5
  module Teamd
5
6
  module Discover
6
7
  class Peer
8
+ class Timeout < Exception; end
7
9
  class << self
8
10
  def tid
9
11
  @tid ||= SecureRandom.hex(8)
10
12
  end
13
+ def domain
14
+ Socket.gethostname.split(".",2).last
15
+ end
11
16
  end
12
17
  def initialize
13
18
  @transport ||= Teamd::Discover::Transport.new
@@ -21,9 +26,8 @@ module Teamd
21
26
  process_announcement(message) if message.is_a? Message::ClusterAnnouncement
22
27
  end
23
28
  end
24
- def discover timeout=15
25
- input = Queue.new
26
- wait timeout
29
+ def discover timeout=3
30
+ input = QueueWithTimeout.new
27
31
  Thread.new do
28
32
  begin
29
33
  message = @transport.receive
@@ -36,24 +40,25 @@ module Teamd
36
40
  retry
37
41
  end
38
42
  end
39
- # i,o=IO.pipe
40
43
  Thread.new do
41
44
  loop do
42
45
  @transport.transmit Message.build("discovery" => true)
43
46
  sleep 1
44
47
  end
45
48
  end
46
- input.pop
49
+ begin
50
+ input.pop_with_timeout timeout
51
+ rescue ThreadError
52
+ nil
53
+ end
47
54
  end
48
- def announce
49
- @transport.transmit @registry.pack
55
+ def announce token=nil
56
+ @registry.add(Cluster.new(token: token,name:Peer.domain)) if token
57
+ @transport.transmit Message::ClusterAnnouncement.new clusters: @registry.pack
50
58
  end
51
59
  private
52
60
  def keep_alive
53
- Thread.new{i,_=IO.pipe;IO.select([i])}
54
- end
55
- def wait timeout
56
- Thread.new{sleep(timeout); exit(1);}
61
+ Thread.new{IO.select([IO.pipe[0]])}
57
62
  end
58
63
  def process_discovery message
59
64
  packet = Message::ClusterAnnouncement.new clusters: @registry.pack
@@ -31,7 +31,7 @@ module Teamd
31
31
  def add_cluster_from_environment
32
32
  uri = URI ENV['ETCD_DISCOVERY'] rescue nil
33
33
  token = uri.path.split("/").last rescue nil
34
- domain = Socket.gethostname.split(".",2).last rescue ""
34
+ domain = Peer.domain rescue ""
35
35
  add Cluster.new(name: domain, token: token) if token && domain
36
36
  end
37
37
  def add_clusters_from_files pattern
@@ -1,5 +1,5 @@
1
1
  module Teamd
2
2
  module Discover
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -6,3 +6,16 @@ require "teamd/discover/peer"
6
6
  require "teamd/discover/protocol"
7
7
  require "teamd/discover/registry"
8
8
  require "teamd/discover/transport"
9
+
10
+ module Teamd
11
+ module Discover
12
+ class << self
13
+ def discover timeout=15
14
+ Teamd::Discover::Peer.new.discover timeout
15
+ end
16
+ def announce token
17
+ Teamd::Discover::Peer.new.announce token
18
+ end
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teamd-discover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Kaufmann
@@ -126,6 +126,7 @@ files:
126
126
  - bin/console
127
127
  - bin/setup
128
128
  - exe/teamd-discover
129
+ - lib/queue_with_timeout.rb
129
130
  - lib/teamd/discover.rb
130
131
  - lib/teamd/discover/cli.rb
131
132
  - lib/teamd/discover/cluster.rb