Improve performance of job counting in maintenance

- Use a single query for all workers
- Replace the manual Ecto query against Oban.Job with Oban.Job.query/1
This commit is contained in:
Claudio Ortolina
2026-04-30 14:39:40 +01:00
parent 16f6bc78d9
commit 3849b338f3
3 changed files with 62 additions and 18 deletions
+12 -9
View File
@@ -10,20 +10,23 @@ defmodule MusicLibrary.Maintenance do
alias MusicLibrary.BackgroundRepo
alias MusicLibrary.Repo
@active_states ~w(available scheduled executing retryable)
@doc """
Counts active Oban jobs for the given worker module name.
Returns a map of worker module names to their count of active Oban jobs.
Active jobs are those in "available", "scheduled", "executing", or "retryable" states.
"""
@spec count_active_jobs(String.t()) :: non_neg_integer() | nil
def count_active_jobs(worker) do
query =
from j in Oban.Job,
where: j.worker == ^worker,
where: j.state in ["available", "scheduled", "executing", "retryable"],
select: count(j.id)
@spec count_active_jobs_by_worker() :: %{String.t() => non_neg_integer()}
def count_active_jobs_by_worker do
q =
from j in subquery(Oban.Job.query(state: @active_states)),
group_by: j.worker,
select: {j.worker, count(j.id)}
BackgroundRepo.one(query)
q
|> BackgroundRepo.all()
|> Map.new()
end
@doc """