nsq-cluster 0.2.0 → 0.2.1
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 +4 -4
- data/README.md +3 -0
- data/bin/nsq-cluster +1 -2
- data/lib/nsq-cluster.rb +52 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea9ecab09ef0e8a1a4f7fdd3b2974d9cd4fdebaf
|
4
|
+
data.tar.gz: 0dfce152f7eaff2f132018b25e36be8411e8c0d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|