Skip to content

Instantly share code, notes, and snippets.

/stats.diff Secret

Created April 16, 2016 16:59
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/ffbb9c0da1b5f2bac5cad0d94e6ba361 to your computer and use it in GitHub Desktop.
Save anonymous/ffbb9c0da1b5f2bac5cad0d94e6ba361 to your computer and use it in GitHub Desktop.
diff --git a/Changes b/Changes
index 05a8924..cb136c3 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,6 @@
-5.04 2016-04-15
+5.04 2016-04-16
+ - Added delayed_jobs field to stats method in Minion and Minion::Backend::Pg.
- Updated Mojo::Pg version requirement to 2.18.
5.03 2016-04-10
diff --git a/lib/Minion.pm b/lib/Minion.pm
index 29b3860..a9bce71 100644
--- a/lib/Minion.pm
+++ b/lib/Minion.pm
@@ -427,6 +427,13 @@ Number of jobs in C<active> state.
Number of workers that are currently processing a job.
+=item delayed_jobs
+
+ delayed_jobs => 100
+
+Number of jobs in C<inactive> state that are scheduled to run at specific time
+in the future.
+
=item failed_jobs
failed_jobs => 100
diff --git a/lib/Minion/Backend.pm b/lib/Minion/Backend.pm
index 38e2b13..6c87bb4 100644
--- a/lib/Minion/Backend.pm
+++ b/lib/Minion/Backend.pm
@@ -408,6 +408,13 @@ Number of jobs in C<active> state.
Number of workers that are currently processing a job.
+=item delayed_jobs
+
+ delayed_jobs => 100
+
+Number of jobs in C<inactive> state that are scheduled to run at specific time
+in the future.
+
=item failed_jobs
failed_jobs => 100
diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm
index 5d2c505..63fd507 100644
--- a/lib/Minion/Backend/Pg.pm
+++ b/lib/Minion/Backend/Pg.pm
@@ -150,6 +150,9 @@ sub stats {
my $stats = $self->pg->db->query(
"select state::text || '_jobs', count(state) from minion_jobs group by state
union all
+ select 'delayed_jobs', count(*) from minion_jobs
+ where state = 'inactive' and delayed > now()
+ union all
select 'inactive_workers', count(*) from minion_workers
union all
select 'active_workers', count(distinct worker) from minion_jobs
@@ -576,6 +579,13 @@ Number of jobs in C<active> state.
Number of workers that are currently processing a job.
+=item delayed_jobs
+
+ delayed_jobs => 100
+
+Number of jobs in C<inactive> state that are scheduled to run at specific time
+in the future.
+
=item failed_jobs
failed_jobs => 100
diff --git a/lib/Minion/Command/minion/job.pm b/lib/Minion/Command/minion/job.pm
index 92e86a5..19b6474 100644
--- a/lib/Minion/Command/minion/job.pm
+++ b/lib/Minion/Command/minion/job.pm
@@ -88,6 +88,7 @@ sub _stats {
say "Active jobs: $stats->{active_jobs}";
say "Failed jobs: $stats->{failed_jobs}";
say "Finished jobs: $stats->{finished_jobs}";
+ say "Delayed jobs: $stats->{delayed_jobs}";
}
sub _worker {
diff --git a/t/pg.t b/t/pg.t
index 0427039..4589230 100644
--- a/t/pg.t
+++ b/t/pg.t
@@ -144,6 +144,7 @@ my $stats = $minion->stats;
is $stats->{active_workers}, 0, 'no active workers';
is $stats->{inactive_workers}, 0, 'no inactive workers';
is $stats->{active_jobs}, 0, 'no active jobs';
+is $stats->{delayed_jobs}, 0, 'no delayed jobs';
is $stats->{failed_jobs}, 0, 'no failed jobs';
is $stats->{finished_jobs}, 0, 'no finished jobs';
is $stats->{inactive_jobs}, 0, 'no inactive jobs';
@@ -334,6 +335,7 @@ $worker->unregister;
$id = $minion->enqueue(add => [2, 1] => {delay => 100});
is $worker->register->dequeue(0), undef, 'too early for job';
ok $minion->job($id)->info->{delayed} > time, 'delayed timestamp';
+is $minion->stats->{delayed_jobs}, 1, 'one delayed job';
$minion->backend->pg->db->query(
"update minion_jobs set delayed = now() - interval '1 day' where id = ?",
$id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment