Class: NsqCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/nsq-cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NsqCluster



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nsq-cluster.rb', line 22

def initialize(opts = {})
  opts = {
    nsqlookupd_count: 0,
    nsqd_count: 0,
    nsqadmin: false,
    nsqd_options: {},
    silent: true
  }.merge(opts)

  @silent = opts[:silent]

  @nsqlookupd = create_nsqlookupds(opts[:nsqlookupd_count])
  @nsqd = create_nsqds(opts[:nsqd_count], opts[:nsqd_options])
  @nsqadmin = create_nsqadmin if opts[:nsqadmin]

  # start everything!
  (@nsqlookupd + @nsqd + [@nsqadmin]).compact.each { |d| d.start }
end

Instance Attribute Details

#nsqadminObject (readonly)

Returns the value of attribute nsqadmin.



20
21
22
# File 'lib/nsq-cluster.rb', line 20

def nsqadmin
  @nsqadmin
end

#nsqdObject (readonly)

Returns the value of attribute nsqd.



20
21
22
# File 'lib/nsq-cluster.rb', line 20

def nsqd
  @nsqd
end

#nsqlookupdObject (readonly)

Returns the value of attribute nsqlookupd.



20
21
22
# File 'lib/nsq-cluster.rb', line 20

def nsqlookupd
  @nsqlookupd
end

Instance Method Details

#create_nsqadminObject



65
66
67
68
69
70
# File 'lib/nsq-cluster.rb', line 65

def create_nsqadmin
  Nsqadmin.new(
    nsqlookupd: @nsqlookupd,
    silent: @silent
  )
end

#create_nsqds(count, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/nsq-cluster.rb', line 53

def create_nsqds(count, options)
  (0...count).map do |idx|
    Nsqd.new(options.merge({
      tcp_port: 4150 + idx * 2,
      http_port: 4151 + idx * 2,
      nsqlookupd: @nsqlookupd,
      silent: @silent
    }))
  end
end

#create_nsqlookupds(count) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/nsq-cluster.rb', line 42

def create_nsqlookupds(count)
  (0...count).map do |idx|
    Nsqlookupd.new(
      tcp_port: 4160 + idx * 2,
      http_port: 4161 + idx * 2,
      silent: @silent
    )
  end
end

#destroyObject



73
74
75
# File 'lib/nsq-cluster.rb', line 73

def destroy
  (@nsqd + @nsqlookupd).each { |d| d.destroy }
end

#nsqlookupd_http_endpointsObject

return an array of http endpoints



79
80
81
# File 'lib/nsq-cluster.rb', line 79

def nsqlookupd_http_endpoints
  @nsqlookupd.map { |lookupd| "http://#{lookupd.host}:#{lookupd.http_port}" }
end