Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

like Data::OptList::mkopt

  • Loading branch information...
commit b7eb49ae9c8951c8582c99cceec7f938773aa1dc 1 parent 70b9cd7
@nekokak authored
Showing with 28 additions and 14 deletions.
  1. +19 −6 lib/Jonk.pm
  2. +9 −8 t/worker.t
View
25 lib/Jonk.pm
@@ -14,17 +14,30 @@ sub new {
Carp::croak('missing job queue database handle.');
}
+ my $default_grab_for = $opts->{default_grab_for}|| 60*60;
+ my $funcs = +{};
+ my $functions = $opts->{functions}||[];
+ for (my $i = 0; $i < @$functions; $i++) {
+ my $func = $functions->[$i];
+
+ my $value;
+
+ if (not defined $functions->[$i+1]) {$i++ }
+ elsif (ref $functions->[$i+1]) {$value = $functions->[++$i]}
+
+ $value ||= +{grab_for => $default_grab_for};
+
+ $funcs->{$func} = $value;
+ }
bless {
dbh => $dbh,
table_name => $opts->{table_name} || 'job',
- functions => $opts->{functions},
- find_funcs => join(', ', map { "'$_'" } keys %{$opts->{functions}}),
+ functions => $funcs,
+ find_funcs => join(', ', map { "'$_'" } keys %{$funcs}),
job_find_size => $opts->{job_find_size} || 50,
- default_grab_for => $opts->{default_grab_for}||(60*60),
-
- _errstr => undef,
+ _errstr => undef,
insert_time_callback => ($opts->{insert_time_callback}||sub{
my ( $sec, $min, $hour, $mday, $mon, $year, undef, undef, undef ) = localtime(time);
@@ -127,7 +140,7 @@ sub _grab_a_job {
sprintf('UPDATE %s SET grabbed_until = ? WHERE id = ? AND grabbed_until = ?', $self->{table_name}),
);
$sth->execute(
- ($time + ($self->{functions}->{$row->{func}}->{grab_for} || $self->{default_grab_for})),
+ ($time + ($self->{functions}->{$row->{func}}->{grab_for})),
$row->{id},
$row->{grabbed_until}
);
View
17 t/worker.t
@@ -5,7 +5,7 @@ use Jonk;
my $dbh = t::Utils->setup;
subtest 'find_job' => sub {
- my $client = Jonk->new($dbh, {functions => {MyWorker => {}}});
+ my $client = Jonk->new($dbh, {functions => [qw/MyWorker/]});
my $job_id = $client->insert('MyWorker', 'arg');
ok $job_id;
@@ -26,7 +26,7 @@ subtest 'find_job' => sub {
};
subtest 'find_job / with priority' => sub {
- my $client = Jonk->new($dbh, {functions => {MyWorker => {}}});
+ my $client = Jonk->new($dbh, {functions => [qw/MyWorker/]});
$client->insert('MyWorker', 'arg_10', {priority => 10});
$client->insert('MyWorker', 'arg_30', {priority => 30});
@@ -60,7 +60,7 @@ subtest 'find_job / with priority' => sub {
};
subtest 'find_job / with run_after' => sub {
- my $client = Jonk->new($dbh, {functions => {MyWorker => {}}});
+ my $client = Jonk->new($dbh, {functions => [qw/MyWorker/]});
my $time = time() + 2;
$client->insert('MyWorker', 'arg', {run_after => $time});
@@ -86,7 +86,7 @@ subtest 'find_job / with run_after' => sub {
subtest 'find_job / with grabbed_until' => sub {
{
- my $client = Jonk->new($dbh, {functions => {MyWorker => {}}, default_grab_for => 2});
+ my $client = Jonk->new($dbh, {functions => [qw/MyWorker/], default_grab_for => 2});
$client->insert('MyWorker', 'arg');
my $job = $client->find_job();
@@ -115,7 +115,7 @@ subtest 'find_job / with grabbed_until' => sub {
}
{
- my $client = Jonk->new($dbh, {functions => {MyWorker => {grab_for => 5}}, default_grab_for => 2});
+ my $client = Jonk->new($dbh, {functions => ['MyWorker' => {grab_for => 5}], default_grab_for => 2});
$client->insert('MyWorker', 'arg');
my $job = $client->find_job();
@@ -156,7 +156,7 @@ subtest 'find_job / without functions' => sub {
};
subtest 'lookup_job' => sub {
- my $client = Jonk->new($dbh);
+ my $client = Jonk->new($dbh, {functions => [qw/MyWorker/]});
my $job_id = $client->insert('MyWorker', 'arg');
ok $job_id;
@@ -176,7 +176,7 @@ t::Utils->cleanup($dbh);
subtest 'find_job / flexible job table name' => sub {
my $dbh = t::Utils->setup("my_job");
- my $client = Jonk->new($dbh, { table_name => 'my_job', functions => {MyWorker => {}}});
+ my $client = Jonk->new($dbh, { table_name => 'my_job', functions => [qw/MyWorker/] });
my $job_id = $client->insert('MyWorker', 'arg');
ok $job_id;
@@ -196,7 +196,7 @@ subtest 'find_job / flexible job table name' => sub {
subtest 'lookup_job / flexible job table name' => sub {
my $dbh = t::Utils->setup("my_job");
- my $client = Jonk->new($dbh, { table_name => 'my_job', functions => {MyWorker => {}}});
+ my $client = Jonk->new($dbh, { table_name => 'my_job', functions => [qw/MyWorker/] });
my $job_id = $client->insert('MyWorker', 'arg');
ok $job_id;
@@ -213,5 +213,6 @@ subtest 'lookup_job / flexible job table name' => sub {
t::Utils->cleanup($dbh, "my_job");
};
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.