Extract DB queries from MaintenanceLive into context

This commit is contained in:
Claudio Ortolina
2026-03-05 18:01:50 +00:00
parent 4b1f6bb245
commit d4376e5fa0
4 changed files with 75 additions and 22 deletions
+39
View File
@@ -0,0 +1,39 @@
defmodule MusicLibrary.Maintenance do
@moduledoc """
Context for database maintenance operations and background job monitoring.
"""
import Ecto.Query
alias MusicLibrary.BackgroundRepo
alias MusicLibrary.Repo
@doc """
Counts active Oban jobs for the given worker module name.
Active jobs are those in "available", "scheduled", "executing", or "retryable" states.
"""
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)
BackgroundRepo.one(query)
end
@doc """
Runs VACUUM on the main database.
"""
def vacuum do
Repo.vacuum()
end
@doc """
Runs PRAGMA optimize on the main database.
"""
def optimize do
Repo.optimize()
end
end