Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

/latency.diff Secret

Created April 17, 2016 00:53
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/85196623df0f5c06e3aa237597d8b198 to your computer and use it in GitHub Desktop.
Save anonymous/85196623df0f5c06e3aa237597d8b198 to your computer and use it in GitHub Desktop.
diff --git a/lib/Minion.pm b/lib/Minion.pm
index 29b3860..72afa74 100644
--- a/lib/Minion.pm
+++ b/lib/Minion.pm
@@ -451,6 +451,13 @@ Number of jobs in C<inactive> state.
Number of workers that are currently not processing a job.
+=item latency
+
+ latency => 0.25
+
+Estimated time in seconds for a job enqueued into the C<default> queue to be
+dequeued by a worker.
+
=back
=head2 worker
diff --git a/lib/Minion/Backend.pm b/lib/Minion/Backend.pm
index 38e2b13..4aa526f 100644
--- a/lib/Minion/Backend.pm
+++ b/lib/Minion/Backend.pm
@@ -432,6 +432,13 @@ Number of jobs in C<inactive> state.
Number of workers that are currently not processing a job.
+=item latency
+
+ latency => 0.25
+
+Estimated time in seconds for a job enqueued into the C<default> queue to be
+dequeued by a worker.
+
=back
=head2 unregister_worker
diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm
index 5d2c505..e3ffe83 100644
--- a/lib/Minion/Backend/Pg.pm
+++ b/lib/Minion/Backend/Pg.pm
@@ -150,6 +150,12 @@ sub stats {
my $stats = $self->pg->db->query(
"select state::text || '_jobs', count(state) from minion_jobs group by state
union all
+ select 'latency', coalesce(extract(epoch from (now () - m.delayed)), 0)
+ from (
+ select min(delayed) as delayed from minion_jobs where state = 'inactive'
+ and queue = 'default' and delayed < now()
+ ) as m
+ union all
select 'inactive_workers', count(*) from minion_workers
union all
select 'active_workers', count(distinct worker) from minion_jobs
@@ -600,6 +606,13 @@ Number of jobs in C<inactive> state.
Number of workers that are currently not processing a job.
+=item latency
+
+ latency => 0.25
+
+Estimated time in seconds for a job enqueued into the C<default> queue to be
+dequeued by a worker.
+
=back
=head2 unregister_worker
diff --git a/lib/Minion/Command/minion/job.pm b/lib/Minion/Command/minion/job.pm
index 92e86a5..ae6ea19 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 "Latency: $stats->{latency}";
}
sub _worker {
diff --git a/t/pg.t b/t/pg.t
index 0427039..0a816dc 100644
--- a/t/pg.t
+++ b/t/pg.t
@@ -147,6 +147,7 @@ is $stats->{active_jobs}, 0, 'no active 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';
+is $stats->{latency}, 0, 'no latency';
$worker = $minion->worker->register;
is $minion->stats->{inactive_workers}, 1, 'one inactive worker';
$minion->enqueue('fail');
@@ -180,6 +181,17 @@ is $stats->{active_jobs}, 0, 'no active jobs';
is $stats->{failed_jobs}, 0, 'no failed jobs';
is $stats->{finished_jobs}, 3, 'three finished jobs';
is $stats->{inactive_jobs}, 0, 'no inactive jobs';
+is $stats->{latency}, 0, 'no latency';
+$id = $minion->enqueue(add => [] => {queue => 'test'});
+is $minion->stats->{latency}, 0, 'no latency';
+$minion->job($id)->remove;
+$id = $minion->enqueue(add => [] => {delay => 86400});
+is $minion->stats->{latency}, 0, 'no latency';
+$minion->backend->pg->db->query(
+ "update minion_jobs set delayed = now() - interval '1 day' where id = ?",
+ $id);
+ok $minion->stats->{latency} >= 86400, 'right latency';
+$minion->job($id)->remove;
# List jobs
$id = $minion->enqueue('add');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment