Skip to content

Commit

Permalink
allow locks to be filtered by name
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 8, 2017
1 parent 4bb27ca commit ee1ecf3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/Minion/Backend.pm
Expand Up @@ -382,10 +382,23 @@ Id of worker that is processing the job.
=head2 list_locks
my $results = $backend->list_locks($offset, $limit);
my $results = $backend->list_locks($offset, $limit, {name => 'foo'});
Returns information about locks in batches. Meant to be overloaded in a
subclass.
These options are currently available:
=over 2
=item name
name => 'name'
List only locks with this name.
=back
These fields are currently available:
=over 2
Expand Down
18 changes: 16 additions & 2 deletions lib/Minion/Backend/Pg.pm
Expand Up @@ -72,12 +72,13 @@ sub list_jobs {
}

sub list_locks {
my ($self, $offset, $limit) = @_;
my ($self, $offset, $limit, $options) = @_;

my $locks = $self->pg->db->query(
'select name, extract(epoch from expires) as expires,
count(*) over() as total from minion_locks
order by expires limit ? offset ?', $limit, $offset
where (name = $1 or $1 is null) order by expires limit $2 offset $3',
$options->{name}, $limit, $offset
)->expand->hashes->to_array;
return _total('locks', $locks);
}
Expand Down Expand Up @@ -617,9 +618,22 @@ Id of worker that is processing the job.
=head2 list_locks
my $results = $backend->list_locks($offset, $limit);
my $results = $backend->list_locks($offset, $limit, {name => 'foo'});
Returns information about locks in batches.
These options are currently available:
=over 2
=item name
name => 'name'
List only locks with this name.
=back
These fields are currently available:
=over 2
Expand Down
4 changes: 3 additions & 1 deletion lib/Minion/Command/minion/job.pm
Expand Up @@ -49,7 +49,7 @@ sub run {

# Locks
return $minion->unlock($unlock) if $unlock;
return $self->_list_locks($offset, $limit) if $locks;
return $self->_list_locks($offset, $limit, {name => $id}) if $locks;

# List jobs
return $self->_list_jobs($offset, $limit, $options) unless defined $id;
Expand Down Expand Up @@ -117,6 +117,8 @@ Minion::Command::minion::job - Minion job command
./myapp.pl minion job -e foo -P 10023 -P 10024 -p 5 -q important
./myapp.pl minion job -R -d 10 10023
./myapp.pl minion job --remove 10023
./myapp.pl minion job -L
./myapp.pl minion job -L some_lock
./myapp.pl minion job -b jobs -a '[12]'
./myapp.pl minion job -b jobs -a '[12]' 23 24 25
Expand Down
7 changes: 7 additions & 0 deletions t/pg.t
Expand Up @@ -184,6 +184,13 @@ is $results->{locks}[0]{name}, 'test', 'right name';
like $results->{locks}[0]{expires}, qr/^[\d.]+$/, 'expires';
is $results->{locks}[1], undef, 'no more locks';
is $results->{total}, 3, 'three results';
$results = $minion->backend->list_locks(0, 10, {name => 'yada'});
is $results->{locks}[0]{name}, 'yada', 'right name';
like $results->{locks}[0]{expires}, qr/^[\d.]+$/, 'expires';
is $results->{locks}[1]{name}, 'yada', 'right name';
like $results->{locks}[1]{expires}, qr/^[\d.]+$/, 'expires';
is $results->{locks}[2], undef, 'no more locks';
is $results->{total}, 2, 'two results';
$minion->unlock($_) for qw(yada yada test);
is $minion->backend->list_locks(0, 10)->{total}, 0, 'no results';

Expand Down

0 comments on commit ee1ecf3

Please sign in to comment.