Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the filter and aggregate query in stats call with subqueries … #133

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/Minion/Backend/Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,19 @@ sub retry_job {
sub stats {
my $self = shift;

my $stats = $self->pg->db->query(
"SELECT COUNT(*) FILTER (WHERE state = 'inactive' AND (expires IS NULL OR expires > NOW())) AS inactive_jobs,
COUNT(*) FILTER (WHERE state = 'active') AS active_jobs, COUNT(*) FILTER (WHERE state = 'failed') AS failed_jobs,
COUNT(*) FILTER (WHERE state = 'finished') AS finished_jobs,
COUNT(*) FILTER (WHERE state = 'inactive' AND delayed > NOW()) AS delayed_jobs,
(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 workers,
EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) AS uptime
FROM minion_jobs"
)->hash;
my $stats = $self->pg->db->query(qq{
select
(select count(*) from minion_jobs mj where state = 'inactive' and (expires IS NULL OR expires > NOW())) as inactive_jobs,
(select count(*) from minion_jobs mj where state = 'active') as active_jobs,
(select count(*) from minion_jobs mj where state = 'failed') as failed_jobs,
(select count(*) from minion_jobs mj where state = 'finished') as finished_jobs,
(select count(*) from minion_jobs mj where state = 'inactive' and delayed > NOW()) as delayed_jobs,
(select COUNT(*) from minion_locks where expires > NOW()) as active_locks,
(select count(DISTINCT worker) from minion_jobs mj 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 workers,
EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) as uptime
})->hash;
kraih marked this conversation as resolved.
Show resolved Hide resolved
$stats->{inactive_workers} = $stats->{workers} - $stats->{active_workers};

return $stats;
Expand Down
Loading