App-Basis-Queue
view release on metacpan or search on metacpan
options => {
'verbose|v' => 'Output useful information',
'queue|q=s' => { desc => 'queue to add things to', required => 1 },
'size|s' => 'Disply the number of unprocessed items in a task queue',
'peek|p=i' => { desc =>
'Display the next few items in a task queue',
},
'exec|e=s' => {
desc =>
"command to run with the message, use count to limit, default "
. EXEC_DEFAULT,
},
'activates|a=s' => {
desc =>
'Parsable UTC datetime after which the message should be valid',
},
'count|c=i' => 'Number of messages to read',
}
) ;
my $msg = join( ' ', @ARGV ) ;
verbose( "first up msg is $msg") ;
if ( $opt{test} ) {
set_verbose(1) ;
set_testing(1) ;
}
show_usage( "No config file found", 1) if( ! -f QUEUE_CONFIG) ;
# lets have the config named after this program
my $cfg = App::Basis::Config->new(
filename => QUEUE_CONFIG,
die_on_error => 1
) ;
my $q = $cfg->get("queue") ;
msg_exit( "Could not find valid config in " . QUEUE_CONFIG, 2 )
if ( !$q ) ;
my ( $activates, $epoch ) = parse_datetime( $opt{activates} ) ;
$q->{prefix} ||= "/task" ;
$q->{prefix} .= "/" if ( $opt{queue} !~ /^\// ) ;
$opt{queue} = $q->{prefix} . $opt{queue} ;
$opt{queue} =~ s|//|/|g ;
my $theq
= connect_queue( $q->{dsn}, $q->{user}, $q->{password}, $opt{queue} ) ;
msg_exit( "Could not connect to queue $q->{dsn}", 2 ) if ( !$theq ) ;
if ( !$theq ) {
msg_exit( "Could not connect to queue $q->{dsn}", 2 ) ;
}
# get the things out of the way that are information only
# if asking for size or peeking, then there is no message adding or sending
if ( $opt{size} || $opt{peek} ) {
my $s = $theq->queue_size() ;
if ( $opt{size} ) {
say inflect "<#n:$s> <N:items> <V:were> found in the queue" ;
} else {
if ($s) {
my $count = 1 ;
say "-" x 80 ;
foreach my $msg ( $theq->peek( count => PEEK_DEFAULT ) ) {
say $count++ . ":\n$msg->{data}" ;
say "-" x 80 ;
}
} else {
say "The queue is empty" ;
}
}
} elsif( $opt{exec}) {
# read $opt{count} items from the queue and run them individually against the exec program
} else {
# optionally ready from stdin, allowing piping to script
if( $msg eq '-') {
$msg = "" ;
foreach (<STDIN>) {
$msg .= $_ ;
}
# remove any final linefeed
chomp $msg ;
}
# if we have a message then this should be added to the queue asap
if ($msg) {
my $resp = $theq->add(
data => { $msg },
activates => $activates
) ;
} else {
verbose("Parameters are required, or message queue is empty") ;
exit 0 ;
}
}
( run in 0.420 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )