Permalink
Browse files

add job test and more docs.

  • Loading branch information...
1 parent b7eb49a commit bfd1cfa491ce23a1762f2c738a0fac8e81ce41fa @nekokak committed Jan 24, 2011
Showing with 245 additions and 38 deletions.
  1. +7 −0 Changes
  2. +29 −24 lib/Jonk.pm
  3. +75 −14 lib/Jonk/Job.pm
  4. +134 −0 t/job.t
View
@@ -1,5 +1,12 @@
Revision history for Perl extension Jonk
+0.10
+ 2011-01-24
+ [ INCOMPATIBLE CHANGE!! ]
+ - many api move to Jonk.
+ - Jonk::Client, Jonk::Worker is removed.
+ - create Jonk::Job.
+
0.06
2010-12-16
- FIXED : some pod.
View
@@ -92,7 +92,7 @@ sub _bind_param_attr {
return;
}
-sub _server_unixitme {
+sub _server_unixitime {
my $dbh = shift;
my $driver = $dbh->{Driver}{Name};
@@ -116,7 +116,7 @@ sub _grab_job {
local $self->{dbh}->{RaiseError} = 1;
local $self->{dbh}->{PrintError} = 0;
- my $time = _server_unixitme($self->{dbh});
+ my $time = _server_unixitime($self->{dbh});
my $sth = $callback->($time);
while (my $row = $sth->fetchrow_hashref) {
@@ -206,10 +206,10 @@ sub _delete {
sub _failed {
my ($self, $job_id, $opt) = @_;
- my $retry_delay = $opt->{retry_delay} || 60;
+ my $retry_delay = _server_unixitime($self->{dbh}) + ($opt->{retry_delay} || 60);
try {
my $sth = $self->{dbh}->prepare_cached(
- sprintf('UPDATE %s SET retry_cnt = retry_cnt + 1, run_after = ? WHERE id = ?', $self->{table_name})
+ sprintf('UPDATE %s SET retry_cnt = retry_cnt + 1, run_after = ?, grabbed_until = 0 WHERE id = ?', $self->{table_name})
);
$sth->execute($retry_delay, $job_id);
$sth->finish;
@@ -218,7 +218,6 @@ sub _failed {
$self->{_errstr} = "can't update job from job queue database: $_";
return;
};
-
}
1;
@@ -234,31 +233,31 @@ Jonk - simple job tank manager.
use DBI;
use Jonk;
my $dbh = DBI->connect(...);
- my $jonk = Jonk->new($dbh, {functions => {MyWorker => {}}});
+ my $jonk = Jonk->new($dbh, {functions => [qw/MyWorker/]});
# insert job
{
$jonk->insert('MyWorker', 'arg');
}
- # dequeue job
+ # e
{
- my $job = $jonk->lookup;
- print $job->{func}; # MyWorker
- print $job->{arg}; # arg
- $job->dequeue;
+ my $job = $jonk->find_job;
+ print $job->func; # MyWorker
+ print $job->arg; # arg
+ $job->completed;
}
=head1 DESCRIPTION
-Jonk is simple job tanking system.
+Jonk is simple job queue manager system
Job is saved and taken out. Besides, nothing is done.
You may use Jonk to make original Job Queuing System.
=head1 METHODS
-=head2 my $jonk = Jonk::Worker->new($dbh, [$options]);
+=head2 my $jonk = Jonk::Worker->new($dbh, [\%options]);
Creates a new Jonk object, and returns the object.
@@ -288,7 +287,7 @@ Default 50.
=back
-=head2 my $job_id = $jonk->enqueue($func, $arg);
+=head2 my $job_id = $jonk->insert($func, $arg);
enqueue a job to a database.
returns job.id.
@@ -309,27 +308,27 @@ Please pass data that does serialize if it is necessary.
=back
-=head2 my $job_hash_ref = $jonk->lookup([$job_id]);
+=head2 my $job = $jonk->lookup_job($job_id);
lookup a job from a database.
-returns job hashref data.
-
-Please do deserialize if it is necessary.
-
-$job_id is optional argument.
+returns Jonk::Job object.
=over 4
-=item * $job_id (optional)
+=item * $job_id
lookup specific $job_id's job.
-=item * $options->{enqueue_time_callback}
+=back
+
+=head2 my $job = $jonk->find_job(\%opts);
-specific enqueue_time creation callback.
+=over 4
+
+=item * $opts->{job_find_size}
-Default local time create.
+find job limit size.
=back
@@ -386,6 +385,12 @@ get most recent error infomation.
priority INTEGER NOT NULL DEFAULT 0
)
+=head1 SEE ALSO
+
+L<Qudo>
+
+L<TheSchwartz>
+
=head1 SUPPORT
irc: #jonk@irc.perl.org
View
@@ -6,8 +6,8 @@ use Carp ();
sub new {
my ($class, $jonk, $job) =@_;
bless {
- job => $job,
- _jonk => $jonk,
+ job => $job,
+ _jonk => $jonk,
_completed => 0,
_failed => 0,
_aborted => 0,
@@ -17,9 +17,8 @@ sub new {
sub completed {
my $self = shift;
- if ($self->is_aborted || $self->is_failed) {
- Carp::croak 'job is already (abort|fail)ed.';
- }
+ Carp::croak 'job is already failed.' if $self->is_failed;
+ Carp::croak 'job is already aborted.' if $self->is_aborted;
$self->{_completed} = 1;
$self->{_jonk}->_delete($self->id);
@@ -28,22 +27,20 @@ sub completed {
sub failed {
my ($self, $opt) = @_;
- if ($self->is_complated || $self->is_aborted) {
- Carp::croak 'job is already (complate|abort)ed.';
- }
+ Carp::croak 'job is already complated.' if $self->is_completed;
+ Carp::croak 'job is already aborted.' if $self->is_aborted;
$self->{_failed} = 1;
$self->{_jonk}->_failed(
$self->id => $opt
);
}
-sub abort {
+sub aborted {
my $self = shift;
- if ($self->is_completed || $self->is_failed) {
- Carp::croak 'job is already (complate|fail)ed.';
- }
+ Carp::croak 'job is already complated.' if $self->is_completed;
+ Carp::croak 'job is already failed.' if $self->is_failed;
$self->{_aborted} = 1;
$self->{_jonk}->_delete($self->id);
@@ -65,7 +62,7 @@ sub is_aborted { $_[0]->{_aborted} }
sub DESTROY {
my $self = shift;
unless ($self->is_completed or $self->is_aborted or $self->is_failed) {
- Carp::croak "job is not (complete|fail|abor)ed. this job auto failed.";
+ Carp::cluck "job is not (complete|fail|abor)ed. this job auto failed.";
$self->failed;
}
}
@@ -74,19 +71,83 @@ sub DESTROY {
__END__
+__END__
+
+=head1 NAME
+
+Jonk::Job - Jonk job class.
+
+=head1 SYNOPSIS
+
+ my $job = $jonk->lookup_job($job_id);
+ $job->arg;
+ $job->completed; # complete and delete job
+ $job->failed; # failed and update job for retry
+ $job->aborted; # failed and delete job
+
=head1 METHODS
+=head2 $job->id
+
+get job id.
+
+=head2 $job->func
+
+get job function name
+
+=head2 $job->arg.
+
+get job argument.
+
+=head2 $job->enqueue_time
+
+get job enqueued time.
+
+=head2 $job->grabbed_until
+
+get job grabbed until time.
+
+=head2 $job->retry_cnt
+
+get job retried count
+
+=head2 $job->run_after
+
+get job run after time
+
+=head2 $job->priority
+
+get job priority num
+
+=head2 $job->completed
+
+completed job.
+
+and delete job data from database.
+
=head2 $job->failed([$options]);
+failed job.
+set error message and delete job data from database.
+
=over 4
=item * $options->{retry_delay}
job retry delay sec.
-Default 60.
+Default 60 sec.
=back
+=head2 $job->aborted
+
+aborted job.
+set error message and delete job data from database.
+
+=head2 $job->is_failed
+
+=head2 $job->is_aborted
+
=cut
Oops, something went wrong.

0 comments on commit bfd1cfa

Please sign in to comment.