Skip to content

Instantly share code, notes, and snippets.

/retry.diff Secret

Created September 27, 2015 23:22
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 anonymous/1f0b49a682f2a8d862ab to your computer and use it in GitHub Desktop.
Save anonymous/1f0b49a682f2a8d862ab to your computer and use it in GitHub Desktop.
diff --git a/Changes b/Changes
index 1fed86b..60179ec 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
-1.19 2015-09-03
+1.19 2015-09-28
+ - Added support for retrying jobs with a new priority.
+ - Added priority option to retry method in Minion::Job.
+ - Added priority option to retry_job method in Minion::Backend::File.
+ - Added priority option to retry_job method in Minion::Backend::Pg.
1.18 2015-08-30
- Fixed Makefile.PL to be compliant with version 2 of the CPAN distribution
diff --git a/lib/Minion/Backend/File.pm b/lib/Minion/Backend/File.pm
index aa6829a..2c553df 100644
--- a/lib/Minion/Backend/File.pm
+++ b/lib/Minion/Backend/File.pm
@@ -126,7 +126,8 @@ sub retry_job {
my $guard = $self->_exclusive;
return undef unless my $job = $self->_job($id, 'failed', 'finished');
$job->{retries} += 1;
- $job->{delayed} = time + $options->{delay} if $options->{delay};
+ $job->{delayed} = time + $options->{delay} if $options->{delay};
+ $job->{priority} = $options->{priority} if defined $options->{priority};
@$job{qw(retried state)} = (time, 'inactive');
delete @$job{qw(finished result started worker)};
@@ -449,6 +450,12 @@ These options are currently available:
Delay job for this many seconds (from now).
+=item priority
+
+ priority => 5
+
+Job priority.
+
=back
=head2 stats
diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm
index fa014a8..06238b1 100644
--- a/lib/Minion/Backend/Pg.pm
+++ b/lib/Minion/Backend/Pg.pm
@@ -133,11 +133,12 @@ sub retry_job {
return !!$self->pg->db->query(
"update minion_jobs
- set finished = null, result = null, retried = now(),
- retries = retries + 1, started = null, state = 'inactive',
- worker = null, delayed = (now() + (interval '1 second' * ?))
+ set finished = null, priority = coalesce(?, priority), result = null,
+ retried = now(), retries = retries + 1, started = null,
+ state = 'inactive', worker = null,
+ delayed = (now() + (interval '1 second' * ?))
where id = ? and state in ('failed', 'finished')
- returning 1", $options->{delay} // 0, $id
+ returning 1", $options->{priority}, $options->{delay} // 0, $id
)->rows;
}
@@ -439,6 +440,12 @@ These options are currently available:
Delay job for this many seconds (from now).
+=item priority
+
+ priority => 5
+
+Job priority.
+
=back
=head2 stats
diff --git a/lib/Minion/Job.pm b/lib/Minion/Job.pm
index f265096..466f2f8 100644
--- a/lib/Minion/Job.pm
+++ b/lib/Minion/Job.pm
@@ -293,6 +293,12 @@ These options are currently available:
Delay job for this many seconds (from now).
+=item priority
+
+ priority => 5
+
+Job priority.
+
=back
=head2 start
diff --git a/t/file.t b/t/file.t
index 5cf807c..382ee6c 100644
--- a/t/file.t
+++ b/t/file.t
@@ -284,6 +284,23 @@ is $job->id, $id, 'right id';
is $job->info->{priority}, 1, 'right priority';
ok $job->finish, 'job finished';
isnt $worker->dequeue(0)->id, $id, 'different id';
+$id = $minion->enqueue(add => [2, 5]);
+$job = $worker->register->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{priority}, 0, 'right priority';
+ok $job->finish, 'job finished';
+ok $job->retry({priority => 100}), 'job retried with higher priority';
+$job = $worker->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{retries}, 1, 'job has been retried once';
+is $job->info->{priority}, 100, 'high priority';
+ok $job->finish, 'job finished';
+ok $job->retry({priority => 0}), 'job retried with lower priority';
+$job = $worker->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{retries}, 2, 'job has been retried twice';
+is $job->info->{priority}, 0, 'low priority';
+ok $job->finish, 'job finished';
$worker->unregister;
# Delayed jobs
diff --git a/t/pg.t b/t/pg.t
index fbfccdf..207802e 100644
--- a/t/pg.t
+++ b/t/pg.t
@@ -306,6 +306,23 @@ is $job->id, $id, 'right id';
is $job->info->{priority}, 1, 'right priority';
ok $job->finish, 'job finished';
isnt $worker->dequeue(0)->id, $id, 'different id';
+$id = $minion->enqueue(add => [2, 5]);
+$job = $worker->register->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{priority}, 0, 'right priority';
+ok $job->finish, 'job finished';
+ok $job->retry({priority => 100}), 'job retried with higher priority';
+$job = $worker->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{retries}, 1, 'job has been retried once';
+is $job->info->{priority}, 100, 'high priority';
+ok $job->finish, 'job finished';
+ok $job->retry({priority => 0}), 'job retried with lower priority';
+$job = $worker->dequeue(0);
+is $job->id, $id, 'right id';
+is $job->info->{retries}, 2, 'job has been retried twice';
+is $job->info->{priority}, 0, 'low priority';
+ok $job->finish, 'job finished';
$worker->unregister;
# Delayed jobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment