Skip to content

Instantly share code, notes, and snippets.

@kitak
Last active December 24, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitak/6838702 to your computer and use it in GitHub Desktop.
Save kitak/6838702 to your computer and use it in GitHub Desktop.
TheSchwaltzをつかってみた
use strict;
use warnings;
use TheSchwartz;
use DateTime;
my $client = TheSchwartz->new(
databases => [{
dsn => 'dbi:mysql:TheSchwartz',
user => 'root',
pass => '',
}],
verbose => 1,
);
my $dt = DateTime->now(time_zone => 'Asia/Tokyo');
my $test_data = {
name => 'kitak',
now => $dt->strftime('%Y%m%d%H%M%S'),
};
$client->insert('MyWorker', $test_data);
requires 'TheSchwartz';
requires 'DateTime';
requires 'DBD::mysql';
package MyWorker;
use strict;
use warnings;
use base qw( TheSchwartz::Worker );
use Data::Dumper;
sub work {
my ($class, $job) = @_;
print "process...\n";
print Dumper $job->arg;
$job->completed();
}
package main;
use strict;
use warnings;
use TheSchwartz;
my $worker = TheSchwartz->new(
databases => [{
dsn => 'dbi:mysql:TheSchwartz',
user => 'root',
pass => '',
}],
verbose => 1,
);
$worker->can_do('MyWorker');
$worker->work(30);

gistの他のファイルを同じディクトリに設置しておく。

carton install
# schema.sqlが欲しかったので、パッケージを落としてきて解凍。docディレクトリにある
wget http://search.cpan.org/CPAN/authors/id/B/BR/BRADFITZ/TheSchwartz-1.07.tar.gz
# データベース、テーブルをつくる
mysql -u root
% craete database TheSchwartz
mysql -u root TheSchwartz < schema.sql
carton exec perl worker.pl
# 別の端末で
carton exec perl client.pl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment