Files
perl-anyevent-nsq/examples/consumer.pl
Pedro Melo 5bd01740ee Improve consumer.pl:
* use Getopt::Long to enable/disable help, verbose and print mode;
* add proper usage message;
* keep track of total messages received.

Signed-off-by: Pedro Melo <melo@simplicidade.org>
2014-07-21 05:10:39 +01:00

59 lines
1.4 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use AnyEvent;
use AnyEvent::NSQ::Reader;
use Getopt::Long;
my ($topic, $channel, $help, $verbose, $print);
GetOptions('help' => \$help, 'verbose' => \$verbose, 'print' => \$print) or usage();
usage() if $help;
($topic, $channel) = @ARGV;
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 = 0;
my $r = AnyEvent::NSQ::Reader->new(
topic => $topic,
channel => $channel,
nsqd_tcp_addresses => '127.0.0.1',
client_id => "${channel}_consumer/pid_$$",
message_cb => sub { $t++; $message_cb->(@_, $t) },
error_cb => sub { warn "$_[1]\n" if $verbose },
disconnect_cb => sub { warn "Got disconnected after $t total messages... exiting...\n" if $verbose; $cv->send },
);
$cv->recv;
sub usage {
print "Error: @_\n" if @_;
print <<" EOU";
Usage: consumer.pl [--help|-h] [--print|-p] topic channel
Consume messages from channel <channel> linked to topic <topic>.
Topic and channel parameters are mandatory.
Options:
--help or -h Prints this message and exits
--verbose or -v Writes debug information about connections,
disconnections and errors
--print or -p Prints each received message
EOU
exit(1);
}