Created
October 22, 2010 18:01
-
-
Save vilmibm/641053 to your computer and use it in GitHub Desktop.
rabbitmq parent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use feature 'say'; | |
use JSON; | |
use Net::RabbitMQ; | |
use Data::Dumper; | |
my $queue = shift @ARGV || die "No queue specified"; | |
say "$queue starting..."; | |
my $mq = Net::RabbitMQ->new(); | |
$mq->connect("localhost", {}); | |
$mq->channel_open(1); | |
while ( 1 ) { | |
$mq->consume(1, $queue); | |
my $msg = from_json( $mq->recv()->{body} ); | |
if ($msg->{done}) { | |
say "$queue got shutdown request"; | |
last; | |
} | |
say "$queue got..."; | |
say Dumper $msg; | |
} | |
say "$queue cleaning up."; | |
$mq->disconnect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use warnings; | |
use strict; | |
use feature 'say'; | |
use JSON; | |
use Net::RabbitMQ; | |
# Get requested number of child processes XXX later use GetOpts | |
my $num_children = shift @ARGV || 1; | |
my $mq = Net::RabbitMQ->new(); | |
$mq->connect("localhost", {}); | |
$mq->channel_open(1); | |
# XXX preamble | |
my $jobs = [ | |
{ rick => {never=>'gonna'} }, | |
{ rick => {give=>'you'} }, | |
{ rick => {up=>'never'} }, | |
{ rick => {gonna=>'let'} }, | |
{ rick => {you=>'down'} }, | |
{ rick => {never=>'gonna'} }, | |
{ rick => {run=>'around'} }, | |
{ rick => {'and'=>'desert'}}, | |
{ rick => {you=>'never'} }, | |
{ rick => {gonna=>'make'} }, | |
{ rick => {you=>'cry'} }, | |
{ rick => {'tell'=>'a'} }, | |
{ rick => {lie=>'never'} }, | |
{ rick => {gonna=>'say'} }, | |
{ rick => {goodbye=>'and'} }, | |
{ rick => {hurt=>'you'} } | |
]; | |
for my $i ( 1..$num_children ) { | |
my $queue = "child_$i"; | |
$mq->queue_declare(1, $queue); | |
# could be more sophisticated | |
`perl mqtest_cl.pl $queue > $queue.log &`; | |
} | |
my $i = 1; | |
while ( scalar @$jobs ) { | |
my $job = shift @$jobs; | |
my $json = to_json($job); | |
$mq->publish(1, "child_$i", $json); | |
if ( ++$i > $num_children ) { $i = 1; } | |
} | |
for $i ( 1..$num_children ) { | |
my $json = to_json( { done => 1 } ); | |
$mq->publish(1, "child_$i", $json); | |
} | |
say "parent cleaning up..."; | |
$mq->disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment