Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix guard method in Minion not to release already expired locks
  • Loading branch information
kraih committed Dec 11, 2017
1 parent 2f81043 commit 737e6ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

8.07 2017-12-12
8.07 2017-12-11
- Fixed guard method in Minion not to release already expired locks.

8.06 2017-12-11
- Added active_locks field to stats methods in Minion, Minion::Backend and
Expand Down
14 changes: 9 additions & 5 deletions lib/Minion.pm
Expand Up @@ -8,6 +8,7 @@ use Minion::Worker;
use Mojo::Date;
use Mojo::Loader 'load_class';
use Mojo::Server;
use Mojo::Util 'steady_time';
use Scalar::Util 'weaken';

has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
Expand Down Expand Up @@ -47,9 +48,10 @@ sub foreground {
}

sub guard {
my ($self, $lock) = (shift, shift);
return undef unless $self->lock($lock, @_);
return Minion::_Guard->new(minion => $self, lock => $lock);
my ($self, $name, $duration, $options) = @_;
my $time = steady_time + $duration;
return undef unless $self->lock($name, $duration, $options);
return Minion::_Guard->new(minion => $self, name => $name, time => $time);
}

sub job {
Expand Down Expand Up @@ -124,7 +126,9 @@ sub _delegate {
package Minion::_Guard;
use Mojo::Base -base;

sub DESTROY { $_[0]{minion}->unlock($_[0]{lock}) }
use Mojo::Util 'steady_time';

sub DESTROY { $_[0]{minion}->unlock($_[0]{name}) if steady_time < $_[0]{time} }

1;

Expand Down Expand Up @@ -556,7 +560,7 @@ An expiration time of C<0> can be used to check if a named lock already exists
without creating one.
# Check if the lock "foo" already exists
say 'Lock exists' unless $minion->lock('foo', 0, {limit => 1});
say 'Lock exists' unless $minion->lock('foo', 0);
These options are currently available:
Expand Down
7 changes: 7 additions & 0 deletions t/pg.t
Expand Up @@ -209,6 +209,13 @@ ok !$minion->guard('foo', 3600), 'not locked again';
undef $guard;
ok $minion->guard('foo', 3600, {limit => 1}), 'locked again';
ok $minion->guard('foo', 3600, {limit => 1}), 'locked again';
ok $guard = $minion->guard('bar', 3600, {limit => 2}), 'locked';
ok my $guard2 = $minion->guard('bar', 0, {limit => 2}), 'locked';
ok my $guard3 = $minion->guard('bar', 3600, {limit => 2}), 'locked';
undef $guard2;
ok !$minion->guard('bar', 3600, {limit => 2}), 'not locked again';
undef $guard;
undef $guard3;

# Reset
$minion->reset->repair;
Expand Down

0 comments on commit 737e6ca

Please sign in to comment.