Standardize fetch_* to refresh_* in Artists context

Closes #132
This commit is contained in:
Claudio Ortolina
2026-03-25 11:52:43 +00:00
parent e4eccf4606
commit 0ccaf46ebd
8 changed files with 27 additions and 32 deletions
+14 -19
View File
@@ -135,8 +135,8 @@ defmodule MusicLibrary.Artists do
Repo.delete_all(from ai in ArtistInfo, where: ai.id == ^artist_id) Repo.delete_all(from ai in ArtistInfo, where: ai.id == ^artist_id)
end end
@spec fetch_artist_info(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()} @spec refresh_artist_info(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()}
def fetch_artist_info(artist_id) do def refresh_artist_info(artist_id) do
with {:ok, musicbrainz_artist} <- MusicBrainz.get_artist(artist_id) do with {:ok, musicbrainz_artist} <- MusicBrainz.get_artist(artist_id) do
if discogs_id = MusicBrainz.Artist.get_discogs_id(musicbrainz_artist) do if discogs_id = MusicBrainz.Artist.get_discogs_id(musicbrainz_artist) do
with {:ok, discogs_artist} <- Discogs.get_artist(discogs_id) do with {:ok, discogs_artist} <- Discogs.get_artist(discogs_id) do
@@ -195,8 +195,8 @@ defmodule MusicLibrary.Artists do
enqueue_worker(Worker.ArtistRefreshDiscogsData, %{"id" => artist_info.id}) enqueue_worker(Worker.ArtistRefreshDiscogsData, %{"id" => artist_info.id})
end end
@spec fetch_wikipedia_data(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()} @spec refresh_wikipedia_data(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()}
def fetch_wikipedia_data(artist_id) do def refresh_wikipedia_data(artist_id) do
artist_info = get_artist_info!(artist_id) artist_info = get_artist_info!(artist_id)
if wikidata_id = ArtistInfo.wikidata_id(artist_info) do if wikidata_id = ArtistInfo.wikidata_id(artist_info) do
@@ -214,11 +214,6 @@ defmodule MusicLibrary.Artists do
end end
end end
@spec refresh_wikipedia_data(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()}
def refresh_wikipedia_data(artist_id) do
fetch_wikipedia_data(artist_id)
end
@spec refresh_wikipedia_data_async(ArtistInfo.t()) :: @spec refresh_wikipedia_data_async(ArtistInfo.t()) ::
{:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()} {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
def refresh_wikipedia_data_async(artist_info) do def refresh_wikipedia_data_async(artist_info) do
@@ -245,8 +240,8 @@ defmodule MusicLibrary.Artists do
Repo.all(q) Repo.all(q)
end end
@spec fetch_image(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()} @spec refresh_image(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()}
def fetch_image(artist_id) do def refresh_image(artist_id) do
artist_info = get_artist_info!(artist_id) artist_info = get_artist_info!(artist_id)
with {:ok, image} <- ArtistInfo.extract_image(artist_info), with {:ok, image} <- ArtistInfo.extract_image(artist_info),
@@ -260,8 +255,8 @@ defmodule MusicLibrary.Artists do
end end
end end
@spec fetch_lastfm_data(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()} @spec refresh_lastfm_data(String.t()) :: {:ok, ArtistInfo.t()} | {:error, term()}
def fetch_lastfm_data(artist_id) do def refresh_lastfm_data(artist_id) do
artist_info = get_artist_info!(artist_id) artist_info = get_artist_info!(artist_id)
name = get_in(artist_info.musicbrainz_data, ["name"]) || "" name = get_in(artist_info.musicbrainz_data, ["name"]) || ""
@@ -278,20 +273,20 @@ defmodule MusicLibrary.Artists do
end end
end end
@spec fetch_lastfm_data_async(String.t()) :: @spec refresh_lastfm_data_async(String.t()) ::
{:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()} {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
def fetch_lastfm_data_async(artist_id) do def refresh_lastfm_data_async(artist_id) do
enqueue_worker(Worker.FetchArtistLastFmData, %{"id" => artist_id}) enqueue_worker(Worker.FetchArtistLastFmData, %{"id" => artist_id})
end end
@spec fetch_artist_info_async(String.t()) :: @spec refresh_artist_info_async(String.t()) ::
{:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()} {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
def fetch_artist_info_async(artist_id) do def refresh_artist_info_async(artist_id) do
enqueue_worker(Worker.FetchArtistInfo, %{"id" => artist_id}) enqueue_worker(Worker.FetchArtistInfo, %{"id" => artist_id})
end end
@spec fetch_image_async(String.t()) :: {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()} @spec refresh_image_async(String.t()) :: {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
def fetch_image_async(artist_id) do def refresh_image_async(artist_id) do
enqueue_worker(Worker.FetchArtistImage, %{"id" => artist_id}) enqueue_worker(Worker.FetchArtistImage, %{"id" => artist_id})
end end
+1 -1
View File
@@ -33,7 +33,7 @@ defmodule MusicLibrary.Artists.Batch do
@spec refresh_lastfm_data() :: {:ok, [String.t()]} @spec refresh_lastfm_data() :: {:ok, [String.t()]}
def refresh_lastfm_data do def refresh_lastfm_data do
Batch.run_on_all(from(r in ArtistInfo), "artist_info", fn artist_info -> Batch.run_on_all(from(r in ArtistInfo), "artist_info", fn artist_info ->
Artists.fetch_lastfm_data_async(artist_info.id) Artists.refresh_lastfm_data_async(artist_info.id)
end) end)
end end
end end
+1 -1
View File
@@ -393,7 +393,7 @@ defmodule MusicLibrary.Records do
record record
|> Record.artist_ids() |> Record.artist_ids()
|> Enum.each(fn artist_id -> |> Enum.each(fn artist_id ->
Artists.fetch_artist_info_async(artist_id) Artists.refresh_artist_info_async(artist_id)
end) end)
{:ok, record} {:ok, record}
@@ -3,7 +3,7 @@ defmodule MusicLibrary.Worker.FetchArtistImage do
@impl Oban.Worker @impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => artist_id}}) do def perform(%Oban.Job{args: %{"id" => artist_id}}) do
case MusicLibrary.Artists.fetch_image(artist_id) do case MusicLibrary.Artists.refresh_image(artist_id) do
{:ok, _artist_info} -> {:ok, _artist_info} ->
:ok :ok
@@ -3,11 +3,11 @@ defmodule MusicLibrary.Worker.FetchArtistInfo do
@impl Oban.Worker @impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => artist_id}}) do def perform(%Oban.Job{args: %{"id" => artist_id}}) do
with {:ok, _artist_info} <- MusicLibrary.Artists.fetch_artist_info(artist_id), with {:ok, _artist_info} <- MusicLibrary.Artists.refresh_artist_info(artist_id),
{:ok, _artist_info} <- MusicLibrary.Artists.fetch_wikipedia_data(artist_id), {:ok, _artist_info} <- MusicLibrary.Artists.refresh_wikipedia_data(artist_id),
{:ok, _artist_info} <- MusicLibrary.Artists.fetch_image(artist_id) do {:ok, _artist_info} <- MusicLibrary.Artists.refresh_image(artist_id) do
# fetch_lastfm_data returns {:ok, _} even on API errors, so it won't block embeddings # refresh_lastfm_data returns {:ok, _} even on API errors, so it won't block embeddings
MusicLibrary.Artists.fetch_lastfm_data(artist_id) MusicLibrary.Artists.refresh_lastfm_data(artist_id)
MusicLibrary.Records.regenerate_artist_embeddings(artist_id) MusicLibrary.Records.regenerate_artist_embeddings(artist_id)
end end
end end
@@ -3,7 +3,7 @@ defmodule MusicLibrary.Worker.FetchArtistLastFmData do
@impl Oban.Worker @impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => artist_id}}) do def perform(%Oban.Job{args: %{"id" => artist_id}}) do
case MusicLibrary.Artists.fetch_lastfm_data(artist_id) do case MusicLibrary.Artists.refresh_lastfm_data(artist_id) do
{:ok, _artist_info} -> :ok {:ok, _artist_info} -> :ok
error -> error error -> error
end end
@@ -514,7 +514,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
@impl true @impl true
def handle_event("refresh_artist_info", _params, socket) do def handle_event("refresh_artist_info", _params, socket) do
case Artists.fetch_artist_info(socket.assigns.artist.musicbrainz_id) do case Artists.refresh_artist_info(socket.assigns.artist.musicbrainz_id) do
{:ok, artist_info} -> {:ok, artist_info} ->
{:noreply, {:noreply,
socket socket
@@ -556,7 +556,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
def handle_event("refresh_lastfm_data", _params, socket) do def handle_event("refresh_lastfm_data", _params, socket) do
musicbrainz_id = socket.assigns.artist.musicbrainz_id musicbrainz_id = socket.assigns.artist.musicbrainz_id
case Artists.fetch_lastfm_data(musicbrainz_id) do case Artists.refresh_lastfm_data(musicbrainz_id) do
{:ok, artist_info} -> {:ok, artist_info} ->
musicbrainz_id musicbrainz_id
|> Records.get_artist_records() |> Records.get_artist_records()
@@ -579,7 +579,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
end end
def handle_event("refresh_artist_image", _params, socket) do def handle_event("refresh_artist_image", _params, socket) do
case Artists.fetch_image(socket.assigns.artist.musicbrainz_id) do case Artists.refresh_image(socket.assigns.artist.musicbrainz_id) do
{:ok, artist_info} -> {:ok, artist_info} ->
{:noreply, {:noreply,
socket socket
+1 -1
View File
@@ -94,7 +94,7 @@ defmodule MusicLibrary.ArtistsTest do
Req.Test.json(conn, Discogs.Fixtures.Artist.get_artist()) Req.Test.json(conn, Discogs.Fixtures.Artist.get_artist())
end) end)
assert {:ok, artist_info} = Artists.fetch_artist_info(steven_wilson_musicbrainz_id) assert {:ok, artist_info} = Artists.refresh_artist_info(steven_wilson_musicbrainz_id)
assert artist_info.id == steven_wilson_musicbrainz_id assert artist_info.id == steven_wilson_musicbrainz_id
assert artist_info.musicbrainz_data == MusicBrainz.Fixtures.Artist.get_artist() assert artist_info.musicbrainz_data == MusicBrainz.Fixtures.Artist.get_artist()