Better routing clean up. Now requires explicit message handling

This commit is contained in:
Bruno Tavares
2014-09-09 11:52:56 +01:00
parent 78c4ca959f
commit 24b1335000
3 changed files with 19 additions and 18 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;
},
);