defmodule GitGud.Repositories.Workers.LanguageStats do
@moduledoc """
Computes per-language byte/file breakdown for a commit and stores it
in `git_commit_language_stats`.
Enqueued from:
* `CommitBackfill` — when a new HEAD lands on a branch.
* `RepoLive.Show` — lazily when a viewer hits a commit that's never
been computed (e.g., legacy commits from before this slice).
Idempotent — `unique` on `(repository_id, sha)` so duplicate enqueues
collapse, and the writer uses `on_conflict: :replace` so a re-run
overwrites stale numbers if the underlying caches changed.
"""
use Oban.Worker,
queue: :git_cache,
max_attempts: 3,
unique: [
fields: [:worker, :args],
keys: [:repository_id, :sha_hex],
period: :infinity,
states: [:available, :scheduled, :executing]
]
alias GitGud.Git
alias GitGud.Repositories
@impl Oban.Worker
def perform(%Oban.Job{args: %{"repository_id" => rid, "sha_hex" => hex}}) do
repo = Repositories.get_repository!(rid)
with {:ok, sha} <- Git.from_hex(hex),
{:ok, _} <- Repositories.compute_and_store_language_stats(repo, sha) do
:ok
end
end
@doc "Build a job for `(repo, sha_binary)`."
def new_job(%{repository_id: rid, sha: sha}) when is_binary(sha) do
__MODULE__.new(%{
"repository_id" => rid,
"sha_hex" => Git.to_hex(sha)
})
end
end
1.4 KiB · text
5af55d9