Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save xtetsuji/9739696 to your computer and use it in GitHub Desktop.

Select an option

Save xtetsuji/9739696 to your computer and use it in GitHub Desktop.
Jonk, a perl one of jobqueue system's sample.

Jonk は RDBMS を利用したジョブキューですが、それは入れ物でしかありません。 ジョブをエンキューしてデキューするだけです。実際にデキューしたものをどう 実行するかは各プログラマーに任されています。

Jonk is job queue system on RDBMS. But it is only a box. It enqueue and dequeue job data. Actually how to execute its job data is leaved to you.

Jonk には、Qudo にあったようなテーブル作成の ヘルパースクリプトもありません。

Jonk does not have helper script to create RDBMS table like having Qudo.

自分で作成する必要があります。

You have to create to need table.

$ sqlite3 /tmp/jonk.db 
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>     CREATE TABLE job (
   ...>         id           INTEGER PRIMARY KEY ,
   ...>         func         text,
   ...>         arg          text,
   ...>         enqueue_time text
   ...>     );
sqlite> .quit
$ sqlite3 /tmp/jonk.db .table 
job

実行しても、たんにエンキューとデキューしかしないことが分かります。

When you execute it, you understand it only enqueue and dequeue.

$ ./jonk-worker.pl & 
[6] 56555
$ ./jonk-client.pl 
$ job is $VAR1 = {
          'id' => 1,
          'func' => 'worker_key',
          'enqueue_time' => '2014-03-24 21:59:05',
          'arg' => 'job_data_here'
        };

そのため、簡潔なかわりに、ワーカースクリプトは明示的に 無限ループを作ってデキューをしたりスリープ処理をする必要があります。 これはあなたがQudoのような高機能なジョブキューを自作するのに 役立つスケルトンです。

So some scripts are simple, but worker script have to create infinite loop and deueue and suitable sleep process clealy. There are skeleton when you want to use high specification job queue like Qudo.

See http://search.cpan.org/dist/Jonk for detail.

#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Jonk::Client;
my $dbh = DBI->connect('dbi:SQLite:/tmp/jonk.db','','');
my $jonk = Jonk::Client->new($dbh);
my $job_id = $jonk->enqueue('worker_key','job_data_here');
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Jonk::Worker;
use My::JonkWorker;
my $dbh = DBI->connect('dbi:SQLite:/tmp/jonk.db','','');
my $jonk = Jonk::Worker->new($dbh => {functions => [qw/worker_key/]});
while (1) {
if (my $job = $jonk->dequeue) {
My::JonkWorker->work($job);
} else {
sleep(3); # wait for 3 sec.
}
}
package My::JonkWorker;
use strict;
use warnings;
use Data::Dumper;
sub work {
my ($class, $job) = @_;
print "job is " . Dumper($job) . "\n";
}
1;
-- for SQLite
-- See: http://search.cpan.org/dist/Jonk/lib/Jonk.pm for other RDBMS.
CREATE TABLE job (
id INTEGER PRIMARY KEY ,
func text,
arg text,
enqueue_time text
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment