@kyanny's blog

My thoughts, my life. Views/opinions are my own.

TheSchwartz を始めて触った 実験

http://d.hatena.ne.jp/tokuhirom/20070501/1177997739 ほとんどこれどおりにやって、動かしてみただけ。

  • funcmap テーブルに Worker 名が追加されるのは、 worker.pl とかを起動した初回のタイミングっぽい。 worker.pl を起動せずに client.pl ばかり叩いて job が追加されずにハマった。
  • Worker.pm のなかで $job->arg->{foo} とかをふつうに print しようとすると何も表示されなかった。 Dumper すると見える。なんでだろう?
  • $job->completed() をコメントアウトしたら job が処理されたあともテーブルから消えずに残るのでは? と思って試してみたけど消えてしまった。

lib/Worker.pm

package Worker;
use strict;
use base qw(TheSchwartz::Worker);
use Data::Dumper;

sub work {
    my ($class, $job) = @_;
    #print @{ [ Dumper($job->arg) ] };
    my $arg = $job->arg;
    print Dumper $arg;
    my $foo = $arg->{foo};
    #print Dumper $foo;
    print Dumper $foo;
    my $time = $arg->{time};
    print Dumper $time;
    #print Dumper $job->arg;
    #print $job->arg->{foo};
    $job->completed();
}

1;

lib/Client.pm

package Client;
use strict;
use base qw(TheSchwartz);
use YAML;
use Data::Dumper;

sub new {
    my ($class, $yaml) = @_;
    my $config = YAML::LoadFile($yaml);
    return $class->SUPER::new(databases => $config->{databases});
}

1;

bin/worker.pl

#!/usr/bin/perl
use strict;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use Worker;
use Client;

my $config = "$Bin/../etc/config.yaml";

my $client = Client->new($config);
$client->can_do('Worker');
$client->work();

bin/client.pl

#!/usr/bin/perl
use strict;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use Client;

my $config = "$Bin/../etc/config.yaml";

my $client = Client->new($config);
my $args = {
    foo => "bar",
    time => time(),
};
$client->insert("Worker" => $args);

etc/config.yaml

databases:
  - dsn: dbi:mysql:queue
    user: root
    pass: