Snippets/SimpleJobQueue

client.pl

#!/usr/bin/perl

use Redis;

my $domain = shift;

my $r = Redis->new();

my $uuid = rand(999_999_999);

print "SENDING REQUEST\n";
$r->lpush( "traceroute-queue", join "\0", $uuid, $domain );

print "WAITING FOR RESULTS\n";
my ($queue, $result) = $r->blpop( "result-$uuid", 0 );

print "RESULT:\n";
print $result;

print "CLEANUP\n";
$r->del( "result-$uuid" );

worker.pl

#!/usr/bin/perl

use Redis;

my $r = Redis->new();

while ( 1 ) {
    print "WAITING\n";
    my ($queue, $job) = $r->blpop( "traceroute-queue", 0 );

    print "FOUND JOB\n";
    my ( $uuid, $domain ) = split /\0/, $job;
    $domain =~ s/[^a-zA-Z0-9._-]//g;

    print "WORKING\n";
    my $result = `traceroute "$domain"`;

    print "RETURNING RESULT\n";
    $r->lpush( "result-$uuid", $result );
}