TheSchwartzを使ってみた
TheSchwartzについては以下がわかりやすい。
TheSchwartzは非同期かつ分散した環境で各種ジョブを処理させるという枠組みを再実装して利用しやすくしたもので,MySQL(SQLiteもサポート)上のデータベースにジョブを格納し,各ワーカープールで処理を行って結果を返す,という単純なフレームワークです。
第11回 OSCON 2007レポート みんなが注目している技術は?:サンフランシスコで昼食を|gihyo.jp … 技術評論社
あとは以下のスライドでもっと詳しく知ることができる(71ページから)
http://www.danga.com/words/2007_04_linuxfest_nw/linuxfest.pdf
gearmanというジョブキューとTheSchwartzとの違いは以下のエントリがわかりやすい。
http://d.hatena.ne.jp/tokuhirom/20071017/1192589429
以上を勉強したところで実際に動かしてみる。
workerとclientとDBを作って動かす。
theschwartz_worker.pl
#!/usr/bin/perl package Worker; use strict; use warnings; use base qw(TheSchwartz::Worker); use YAML; use Carp; sub work { my ($class, $job) = @_; print "Workin' hard or hardly workin' ? Hyuk!!\n"; print Dump $job->arg; my $file = $job->arg->{foo} . '.dat'; if (open my $fh, '>', $file) { print $fh $job->arg->{foo}; close $fh; } else { Carp::croak "Cannot open $file: $!"; } $job->completed(); } package main; use strict; use warnings; use TheSchwartz; my $client = TheSchwartz->new( databases => [{ dsn => 'dbi:mysql:theschwartz', user => 'root', pass => ''}], ); $client->can_do('Worker'); $client->work();
theschwartz_client.pl
#!/usr/bin/perl use strict; use warnings; use TheSchwartz; use YAML; my $client = TheSchwartz->new( databases => [{dsn => 'dbi:mysql:theschwartz', user => 'root', pass => ''}], ); my $handle = $client->insert('Worker' => {foo => 'bar'}); print Dump $handle;
DBを作る。
$ wget -O theschwartz_schema.sql http://search.cpan.org/src/BRADFITZ/TheSchwartz-1.04/doc/schema.sql $ mysqladmin -uroot create theschwartz $ mysql -uroot theschwartz <theschwartz_schema.sql
あとはworkerを起動してからもうひとつのコンソールでclientを起動する。
worker側の結果
$ ./theschwartz_worker.pl Workin' hard or hardly workin' ? Hyuk!! --- foo: bar
client側の結果
$ ./theschwartz_client.pl --- !!perl/hash:TheSchwartz::JobHandle client: !!perl/array:TheSchwartz (中略) dsn_hashed: fd7123c32cac091b75a5f62b2c48f344 jobid: 11
あとschwartzmonというツールがあるようで、これを使うとエラーを調べたりできるらしい。
$ schwartzmon errors --database=theschwartz --user=root Tue Dec 4 02:18:53 2007 [11]: Undefined subroutine &Worker::foo called at ./theschwartz_worker.pl line 16.
疑問点。
workerを起動させておくためにはなにか使ったりするのかな。daemontoolsとか?
==> TheSchwartzのworkerはdaemontoolsで動かすことにしたよ - libnitsuji.so
-
- -