1.5 KiB · text History b868907
defmodule GitGud.Packages.Workers.Prune do
  @moduledoc """
  The nightly retention pass.

  Deletes what the retention policy lets go, while
  `packages.retention_enforced` is on (the default). With it off it
  still runs, and logs what it *would* free — the number the admin view
  shows.

  Scheduled from `config/config.exs`. Daily rather than hourly: a
  registry's disk problem develops over weeks, and a nightly job is
  easier to reason about after the fact than one that fires while
  someone is publishing.
  """

  use Oban.Worker, queue: :default, max_attempts: 3

  require Logger

  alias GitGud.Packages.DiskUsage
  alias GitGud.Packages.Pruner

  @impl Oban.Worker
  def perform(%Oban.Job{args: args}) do
    opts = if args["force"], do: [force: true], else: []

    {:ok, result} = Pruner.prune_all(opts)

    case result do
      %{status: :pruned, pruned: n, bytes: bytes} ->
        Logger.info(
          "retention: pruned #{n} version(s), freed #{DiskUsage.humanize(bytes)}" <>
            failures(result)
        )

      %{pruned: n, bytes: bytes} ->
        Logger.info(
          "retention: reporting only — would prune #{n} version(s), " <>
            "freeing #{DiskUsage.humanize(bytes)} " <>
            "(enable packages.retention_enforced to act on it)"
        )
    end

    :ok
  end

  defp failures(%{failed: []}), do: ""

  defp failures(%{failed: failed}),
    do: "; #{length(failed)} kept after errors: #{inspect(Enum.take(failed, 3))}"
end