Skip to content

Commit

Permalink
Add workers field to stats methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 24, 2022
1 parent 3364462 commit c15352a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

10.25 2022-04-29
10.25 2022-06-24
- Added workers field to stats methods in Minion and Minion::Backend::Pg.

10.24 2022-04-28
- Improved Mojolicious::Plugin::Minion::Admin to log whenever a job gets removed.
Expand Down
6 changes: 6 additions & 0 deletions lib/Minion.pm
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ Number of workers that are currently not processing a job.
Uptime in seconds.
=item workers
workers => 200;
Number of registered workers.
=back
=head2 unlock
Expand Down
6 changes: 6 additions & 0 deletions lib/Minion/Backend.pm
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,12 @@ Number of workers that are currently not processing a job.
Uptime in seconds.
=item workers
workers => 200;
Number of registered workers.
=back
=head2 unlock
Expand Down
12 changes: 9 additions & 3 deletions lib/Minion/Backend/Pg.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Minion::Backend::Pg;
use Mojo::Base 'Minion::Backend';

use Carp qw(croak);
use Carp qw(croak);
use Mojo::File qw(path);
use Mojo::IOLoop;
use Mojo::Pg 4.0;
Expand Down Expand Up @@ -235,11 +235,11 @@ sub stats {
(SELECT COUNT(*) FROM minion_locks WHERE expires > NOW()) AS active_locks,
COUNT(DISTINCT worker) FILTER (WHERE state = 'active') AS active_workers,
(SELECT CASE WHEN is_called THEN last_value ELSE 0 END FROM minion_jobs_id_seq) AS enqueued_jobs,
(SELECT COUNT(*) FROM minion_workers) AS inactive_workers,
(SELECT COUNT(*) FROM minion_workers) AS workers,
EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) AS uptime
FROM minion_jobs"
)->hash;
$stats->{inactive_workers} -= $stats->{active_workers};
$stats->{inactive_workers} = $stats->{workers} - $stats->{active_workers};

return $stats;
}
Expand Down Expand Up @@ -1041,6 +1041,12 @@ Number of workers that are currently not processing a job.
Uptime in seconds.
=item workers
workers => 200;
Number of registered workers.
=back
=head2 unlock
Expand Down
6 changes: 5 additions & 1 deletion t/pg.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use Minion;
use Mojo::IOLoop;
use Mojo::Promise;
use Sys::Hostname qw(hostname);
use Time::HiRes qw(usleep);
use Time::HiRes qw(usleep);

# Isolate tests
require Mojo::Pg;
Expand Down Expand Up @@ -390,6 +390,7 @@ subtest 'Stats' => sub {
);
$minion->add_task(fail => sub { die "Intentional failure!\n" });
my $stats = $minion->stats;
is $stats->{workers}, 0, 'no workers';
is $stats->{active_workers}, 0, 'no active workers';
is $stats->{inactive_workers}, 0, 'no inactive workers';
is $stats->{enqueued_jobs}, 0, 'no enqueued jobs';
Expand All @@ -401,6 +402,7 @@ subtest 'Stats' => sub {
is $stats->{active_locks}, 0, 'no active locks';
ok $stats->{uptime}, 'has uptime';
my $worker = $minion->worker->register;
is $minion->stats->{workers}, 1, 'one worker';
is $minion->stats->{inactive_workers}, 1, 'one inactive worker';
$minion->enqueue('fail');
is $minion->stats->{enqueued_jobs}, 1, 'one enqueued job';
Expand All @@ -409,6 +411,7 @@ subtest 'Stats' => sub {
is $minion->stats->{inactive_jobs}, 2, 'two inactive jobs';
ok my $job = $worker->dequeue(0), 'job dequeued';
$stats = $minion->stats;
is $stats->{workers}, 1, 'one worker';
is $stats->{active_workers}, 1, 'one active worker';
is $stats->{active_jobs}, 1, 'one active job';
is $stats->{inactive_jobs}, 1, 'one inactive job';
Expand All @@ -429,6 +432,7 @@ subtest 'Stats' => sub {
ok $worker->dequeue(0)->finish(['works']), 'job finished';
$worker->unregister;
$stats = $minion->stats;
is $stats->{workers}, 0, 'no workers';
is $stats->{active_workers}, 0, 'no active workers';
is $stats->{inactive_workers}, 0, 'no inactive workers';
is $stats->{active_jobs}, 0, 'no active jobs';
Expand Down

0 comments on commit c15352a

Please sign in to comment.