diff --git a/examples/consumer.pl b/examples/consumer.pl index be4751e..ae1df27 100755 --- a/examples/consumer.pl +++ b/examples/consumer.pl @@ -18,9 +18,6 @@ usage("topic and channel are required parameters") unless $topic and $channel; my $cv = AE::cv; -## return undef => mark_as_done_msg() -my $message_cb = $print ? sub { print "$_[1]{message}\n"; return } : sub {return}; - my $t = my $p = 0; my $r = AnyEvent::NSQ::Reader->new( topic => $topic, @@ -28,7 +25,7 @@ my $r = AnyEvent::NSQ::Reader->new( nsqd_tcp_addresses => '127.0.0.1', client_id => "${channel}_consumer/pid_$$", - message_cb => sub { $t++; $p++; $message_cb->(@_) }, + message_cb => \&message_handler, error_cb => sub { warn "$_[1]\n" if $verbose }, disconnect_cb => sub { warn "Disconnected after $t total messages... exiting...\n" if $verbose; $cv->send }, @@ -85,3 +82,14 @@ Usage: consumer.pl [--help|-h] [--print|-p] topic channel exit(1); } + +sub message_handler { + use Data::Dumper; + my ($reader, $message) = @_; + + if ($print) { + print $message->{message}."\n"; + } + + $reader->mark_as_done_msg($message); +} diff --git a/lib/AnyEvent/NSQ/Reader.pm b/lib/AnyEvent/NSQ/Reader.pm index 6cfed56..76d7068 100644 --- a/lib/AnyEvent/NSQ/Reader.pm +++ b/lib/AnyEvent/NSQ/Reader.pm @@ -63,11 +63,6 @@ sub _identified { my $action = $self->{message_cb}->($self, $msg); - ## Action below -1 does nothing, we assume the user took care of it himself - if (not defined $action) { $conn->mark_as_done_msg($_[1]) } - elsif ($action >= -1) { $conn->requeue_msg($_[1], $action) } - - delete($self->{routing}->{$msg->{message_id}}); } ); @@ -77,8 +72,7 @@ sub _identified { sub mark_as_done_msg { my ($self, $msg) = @_; - my $conn = $self->_find_message_connection($msg); - return 0 unless $conn; + my $conn = $self->_find_and_delete_message_connection($msg); $conn->mark_as_done_msg($msg); return 1; @@ -87,8 +81,7 @@ sub mark_as_done_msg { sub requeue_msg { my ($self, $msg, $delay) = @_; - my $conn = $self->_find_message_connection($msg); - return 0 unless $conn; + my $conn = $self->_find_and_delete_message_connection($msg); $conn->requeue_msg($msg, $delay); return 1; @@ -97,22 +90,21 @@ sub requeue_msg { sub touch_message { my ($self, $msg) = @_; - my $conn = $self->_find_message_connection($msg); - return 0 unless $conn; + my $conn = $self->_find_and_delete_message_connection($msg); $conn->touch_msg($msg); return 1; } -sub _find_message_connection { +sub _find_and_delete_message_connection { my ($self, $msg) = @_; my $id = ref($msg) ? $msg->{message_id} : $msg; - my $conn = $self->{routing}->{$id}; + my $conn = delete($self->{routing}->{$id}); if ( !$conn ) { - warn "WARN: Could not find the connection to route msg $id"; + croak "WARN: Could not find the connection to route msg $id"; } return $conn; diff --git a/t/10-reader.t b/t/10-reader.t index 1545b29..b686640 100755 --- a/t/10-reader.t +++ b/t/10-reader.t @@ -17,6 +17,7 @@ subtest 'basic connection' => sub { message_cb => sub { print STDERR "!!!! GOT MESSAGE '$_[1]{message}\n"; + $_[0]->mark_as_done_msg($_[1]); return; }, );