Resqueでメッセージキューをやってみた

├── client.pl
├── cpanfile
├── lib
│   └── MyTask
│       └── Echo.pm
├── main.pl
└── worker.pl
requires 'Log::Minimal';
requires 'Path::Tiny';
requires 'Proclet';
requires 'Resque';
requires 'Test::RedisServer';
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Resque;
use Log::Minimal;


my $resque = Resque->new;

my $count = 10;
while ($count--) {
    $resque->push(
        echo => +{
            class => 'MyTask::Echo',
            args  => +[+{count => $count}]
        }
    );
    infof($count);
    sleep 2;
}
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Resque;
use Log::Minimal;
use lib 'lib';


my $worker = Resque->new->worker;

$worker->add_queue('echo');
infof('### start worker');
$worker->work;
infof('### stop worker');
package MyTask::Echo;
use utf8;
use strict;
use warnings;
use Log::Minimal;


sub perform {
    my $job = shift;
    debugf('dequeue: %s', ddf($job->args));
    infof($job->args->[0]{count});
}

1;
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Path::Tiny qw(path tempdir);
use Test::RedisServer;
use Proclet;


my $redis = Test::RedisServer->new(
    auto_start => 0,
    tmpdir     => tempdir(),
);
$ENV{REDIS_SERVER} = $redis->connect_info;

my $proclet = Proclet->new(color => 1);
$proclet->service(
    code => sub {
        $redis->exec;
    },
    worker => 1,
    tag    => 'redis'
);
$proclet->service(
    code   => [qw{carton exec -- perl worker.pl}],
    worker => 5,
    tag    => 'worker'
);
$proclet->service(
    code   => [qw{carton exec -- perl client.pl}],
    worker => 1,
    tag    => 'client'
);
$proclet->run;
$ brew install redis
$ carton install
$ carton exec -- perl main.pl

Comments

comments powered by Disqus