nsq-cluster 0.2.0 → 0.2.1

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: bb2f06489d4c76e38f2dbf70297322f3a715e0ef
4
- data.tar.gz: 601c63715a0a16028dae656bdf3e8da83cb6df7f
3
+ metadata.gz: ea9ecab09ef0e8a1a4f7fdd3b2974d9cd4fdebaf
4
+ data.tar.gz: 0dfce152f7eaff2f132018b25e36be8411e8c0d6
5
5
  SHA512:
6
- metadata.gz: 8564b8c8a1215bacee880cf3c4ae0f8799e4b2bad7fa2cba869b44ed5d6c7233e0d1df3efdaaa4fed84722926128880c70dd3b4d4c2a321e745a0e1cb4fe7036
7
- data.tar.gz: 7366cc51ba05e8ad2e6402a1bc5446d62d159b5702d68189f7fb0202ff4e6f8e0ec5c73158c19e400ae9d4df54d285d6e974b64b22557245c167a67c508e7136
6
+ metadata.gz: ee04feb92da1d0d50008527183d28df993f3651585148facd50c77a763454af276b0833aa3ec9815afd0eefab6568deddb00e962f1f1c9f364c3b4e8dfaef2f7
7
+ data.tar.gz: bbe511eda616cfd82a5c6d09af1be2e215e1782840f8dc46da1dd1233116e9492266f52638be46114f7beac494340616d724611002fbe3e10489d6439b664cb5
data/README.md CHANGED
@@ -6,6 +6,9 @@ Easily start up a local NSQ cluster. This is great for testing.
6
6
  # Start a cluster of 3 nsqd's and 2 nsqlookupd's
7
7
  cluster = NsqCluster.new(nsqd_count: 3, nsqlookupd_count: 2)
8
8
 
9
+ # Optionaally, block until the cluster is up and running
10
+ cluster.block_until_running
11
+
9
12
  # Stop the 3rd nsqd instance
10
13
  cluster.nsqd.last.stop
11
14
 
data/bin/nsq-cluster CHANGED
@@ -24,9 +24,8 @@ OptionParser.new do |opts|
24
24
  end
25
25
  end.parse!
26
26
 
27
- print 'Starting cluster ... '
28
27
  cluster = NsqCluster.new(options)
29
- puts 'started'
28
+ cluster.block_until_running
30
29
 
31
30
  Signal.trap('INT') do
32
31
  print 'Shutting down ... '
data/lib/nsq-cluster.rb CHANGED
@@ -11,12 +11,15 @@
11
11
  # cluster.destroy
12
12
  #
13
13
 
14
+ require 'socket'
15
+ require 'timeout'
16
+
14
17
  require_relative 'nsq-cluster/nsqlookupd'
15
18
  require_relative 'nsq-cluster/nsqd'
16
19
  require_relative 'nsq-cluster/nsqadmin'
17
20
 
18
21
  class NsqCluster
19
-
22
+ PORTSCAN_INTERVAL = 0.01
20
23
  attr_reader :nsqd, :nsqlookupd, :nsqadmin
21
24
 
22
25
  def initialize(opts = {})
@@ -81,4 +84,52 @@ class NsqCluster
81
84
  @nsqlookupd.map { |lookupd| "http://#{lookupd.host}:#{lookupd.http_port}" }
82
85
  end
83
86
 
87
+
88
+ def block_until_running(timeout = 3)
89
+ puts "Waiting for cluster to launch..." unless @silent
90
+ begin
91
+ Timeout::timeout(timeout) do
92
+ service_ports.each do |port, protocol|
93
+ wait_for_port(port, protocol)
94
+ end
95
+ puts "Cluster launched." unless @silent
96
+ end
97
+ rescue Timeout::Error
98
+ puts "ERROR: Cluster did not fully launch within #{timeout} seconds."
99
+ end
100
+ end
101
+
102
+
103
+ private
104
+ def service_ports
105
+ ports = {}
106
+ ports[nsqadmin.http_port] = :http if nsqadmin
107
+ nsqlookupd.each do |n|
108
+ ports[n.tcp_port] = :tcp
109
+ ports[n.http_port] = :http
110
+ end
111
+ nsqd.each do |n|
112
+ ports[n.tcp_port] = :tcp
113
+ ports[n.http_port] = :http
114
+ end
115
+ ports
116
+ end
117
+
118
+
119
+ def wait_for_port(port, protocol)
120
+ port_open = false
121
+ until port_open do
122
+ begin
123
+ sock = TCPSocket.new('127.0.0.1', port)
124
+ port_open = true
125
+ puts "#{protocol.to_s.upcase} port #{port} open." unless @silent
126
+ if protocol == :tcp
127
+ puts "You may safely ignore: 'ERROR: failed to read protocol version - EOF'" unless @silent
128
+ end
129
+ sock.close
130
+ rescue Errno::ECONNREFUSED
131
+ end
132
+ sleep PORTSCAN_INTERVAL if !port_open
133
+ end
134
+ end
84
135
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsq-cluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wistia