Class: Nsq::Producer

Inherits:
ClientBase show all
Defined in:
lib/nsq/producer.rb

Instance Attribute Summary collapse

Attributes inherited from ClientBase

#connections

Instance Method Summary collapse

Methods inherited from ClientBase

#connected?, #terminate

Methods included from AttributeLogger

included

Constructor Details

#initialize(opts = {}) ⇒ Producer

Returns a new instance of Producer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nsq/producer.rb', line 7

def initialize(opts = {})
  @connections = {}
  @topic = opts[:topic]
  @discovery_interval = opts[:discovery_interval] || 60
  @ssl_context = opts[:ssl_context]
  @tls_options = opts[:tls_options]
  @tls_v1 = opts[:tls_v1]

  nsqlookupds = []
  if opts[:nsqlookupd]
    nsqlookupds = [opts[:nsqlookupd]].flatten
    discover_repeatedly(
      nsqlookupds: nsqlookupds,
      interval: @discovery_interval
    )

  elsif opts[:nsqd]
    nsqds = [opts[:nsqd]].flatten
    nsqds.each{|d| add_connection(d)}

  else
    add_connection('127.0.0.1:4150')
  end
end

Instance Attribute Details

#topicObject (readonly)

Returns the value of attribute topic.



5
6
7
# File 'lib/nsq/producer.rb', line 5

def topic
  @topic
end

Instance Method Details

#deferred_write(delay, *raw_messages) ⇒ Object

Arg ‘delay’ in seconds



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

def deferred_write(delay, *raw_messages)
  if !@topic
    raise 'No topic specified. Either specify a topic when instantiating the Producer or use write_to_topic.'
  end
  if delay < 0.0
    raise "Delay can't be negative, use a positive float."
  end

  deferred_write_to_topic(@topic, delay, *raw_messages)
end

#deferred_write_to_topic(topic, delay, *raw_messages) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
# File 'lib/nsq/producer.rb', line 69

def deferred_write_to_topic(topic, delay, *raw_messages)
  raise ArgumentError, 'message not provided' if raw_messages.empty?
  messages = raw_messages.map(&:to_s)
  connection = connection_for_write
  messages.each do |msg|
    connection.dpub(topic, (delay * 1000).to_i, msg)
  end
end

#write(*raw_messages) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/nsq/producer.rb', line 32

def write(*raw_messages)
  if !@topic
    raise 'No topic specified. Either specify a topic when instantiating the Producer or use write_to_topic.'
  end

  write_to_topic(@topic, *raw_messages)
end

#write_to_topic(topic, *raw_messages) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nsq/producer.rb', line 52

def write_to_topic(topic, *raw_messages)
  # return error if message(s) not provided
  raise ArgumentError, 'message not provided' if raw_messages.empty?

  # stringify the messages
  messages = raw_messages.map(&:to_s)

  # get a suitable connection to write to
  connection = connection_for_write

  if messages.length > 1
    connection.mpub(topic, messages)
  else
    connection.pub(topic, messages.first)
  end
end