Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DPUB to send deferred messages #23

Merged
merged 3 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.3.1
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GEM
nokogiri (>= 1.5.10)
rake
rdoc
json (1.8.1)
json (1.8.3)
jwt (1.0.0)
mini_portile (0.6.0)
multi_json (1.10.1)
Expand Down Expand Up @@ -71,3 +71,6 @@ DEPENDENCIES
jeweler (~> 2.0)
nsq-cluster (~> 1.1)
rspec (~> 3.0)

BUNDLED WITH
1.12.5
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ producer.write('some-message')
# Write a bunch of messages to NSQ (uses mpub)
producer.write('one', 'two', 'three', 'four', 'five')

# Write a deferred message to NSQ (uses dpub)

# Message deferred of 10s
producer.deferred_write(10, 'one')

# Message deferred of 1250ms
producer.deferred_write(1.25, 'one')


# Close the connection
producer.terminate
```
Expand Down
3 changes: 3 additions & 0 deletions lib/nsq/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def pub(topic, message)
write ["PUB #{topic}\n", message.bytesize, message].pack('a*l>a*')
end

def dpub(topic, delay_in_ms, message)
write ["DPUB #{topic} #{delay_in_ms}\n", message.bytesize, message].pack('a*l>a*')
end

def mpub(topic, messages)
body = messages.map do |message|
Expand Down
20 changes: 19 additions & 1 deletion lib/nsq/producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def initialize(opts = {})
at_exit{terminate}
end


def write(*raw_messages)
if !@topic
raise 'No topic specified. Either specify a topic when instantiating the Producer or use write_to_topic.'
Expand All @@ -38,6 +37,17 @@ def write(*raw_messages)
write_to_topic(@topic, *raw_messages)
end

# Arg 'delay' in seconds
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

def write_to_topic(topic, *raw_messages)
# return error if message(s) not provided
Expand All @@ -56,6 +66,14 @@ def write_to_topic(topic, *raw_messages)
end
end

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

private
def connection_for_write
Expand Down
17 changes: 16 additions & 1 deletion spec/lib/nsq/producer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'json'

describe Nsq::Producer do

def message_count(topic = @producer.topic)
topics_info = JSON.parse(@nsqd.stats.body)['data']['topics']
topic_info = topics_info.select{|t| t['topic_name'] == topic }.first
Expand Down Expand Up @@ -71,6 +70,22 @@ def new_consumer(topic = TOPIC)
expect(message_count).to eq(10)
end

it 'can queue a deferred message' do
@producer.deferred_write 1.0, 1
wait_for{message_count==1}
expect(message_count).to eq(1)
end

it 'can queue multiple deferred messages' do
@producer.deferred_write 1.0, 1, 2, 3
wait_for{message_count==3}
expect(message_count).to eq(3)
end

it 'raises an exception if delay is negative' do
expect {@producer.deferred_write -10, 1}.to raise_error
end

it 'shouldn\'t raise an error when nsqd is down' do
@nsqd.stop

Expand Down