Skip to content

Instantly share code, notes, and snippets.

/stats.diff Secret

Created April 20, 2016 12:02
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/a9feac7241badf8edeb415e331289707 to your computer and use it in GitHub Desktop.
Save anonymous/a9feac7241badf8edeb415e331289707 to your computer and use it in GitHub Desktop.
diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm
index 2676fdf..b540735 100644
--- a/lib/Minion/Backend/Pg.pm
+++ b/lib/Minion/Backend/Pg.pm
@@ -147,21 +147,14 @@ sub retry_job {
sub stats {
my $self = shift;
- my $stats = $self->pg->db->query(
- "select state::text || '_jobs', count(*) 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
- where state = 'active'"
- )->arrays->reduce(sub { $a->{$b->[0]} = $b->[1]; $a }, {});
- $stats->{inactive_workers} -= $stats->{active_workers};
- $stats->{"${_}_jobs"} ||= 0 for qw(inactive active failed finished);
-
- return $stats;
+ $self->pg->db->query(
+ "select json_object_agg(queue, (
+ select jsonb_object_agg(state || '_jobs', (
+ select count(*) from minion_jobs where state = states.state and queue = queues.queue
+ ))
+ from (select unnest(array['active', 'failed', 'finished', 'inactive']::minion_state[]) as state) as states
+ )) from (select distinct queue from minion_jobs) as queues"
+ )->expand->array->[0];
}
sub unregister_worker {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment