From 4e24bfe96600c2127e1fee46f125cbbe01fcd7d5 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 22 Sep 2025 08:22:36 +0300 Subject: [PATCH] Populate genres via Oban with generous retries This is due to OpenAI erroring or timing out incredibly frequently. --- lib/music_library/records.ex | 9 +++++++++ lib/music_library/worker/populate_genres.ex | 12 ++++++++++++ lib/music_library_web/live/collection_live/show.ex | 9 ++++----- lib/music_library_web/live/wishlist_live/show.ex | 9 ++++----- priv/gettext/default.pot | 12 ------------ priv/gettext/en/LC_MESSAGES/default.po | 12 ------------ 6 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 lib/music_library/worker/populate_genres.ex diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 102160bc..7ca9e2ba 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -226,6 +226,15 @@ defmodule MusicLibrary.Records do |> Repo.update() end + def populate_genres_async(record) do + meta = %{title: record.title, artists: Enum.map(record.artists, & &1.name)} + params = %{"id" => record.id} + + params + |> Worker.PopulateGenres.new(meta: meta) + |> BackgroundRepo.insert() + end + defp get_cover_art_or_default(musicbrainz_id) do case MusicBrainz.get_cover_art({:musicbrainz_id, musicbrainz_id}) do {:error, :cover_not_available} -> {:ok, Assets.Image.fallback_data()} diff --git a/lib/music_library/worker/populate_genres.ex b/lib/music_library/worker/populate_genres.ex new file mode 100644 index 00000000..27ba9928 --- /dev/null +++ b/lib/music_library/worker/populate_genres.ex @@ -0,0 +1,12 @@ +defmodule MusicLibrary.Worker.PopulateGenres do + use Oban.Worker, queue: :heavy_writes, max_attempts: 10 + + @impl Oban.Worker + def perform(%Oban.Job{args: %{"id" => record_id}}) do + record = MusicLibrary.Records.get_record!(record_id) + + with {:ok, updated_record} <- MusicLibrary.Records.populate_genres(record) do + MusicLibrary.Records.notify_update(updated_record) + end + end +end diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index 5c335aa2..886ea459 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -79,19 +79,18 @@ defmodule MusicLibraryWeb.CollectionLive.Show do def handle_event("populate_genres", %{"id" => id}, socket) do record = Records.get_record!(id) - case Records.populate_genres(record) do - {:ok, updated_record} -> + case Records.populate_genres_async(record) do + {:ok, _worker} -> {:noreply, socket - |> put_toast(:info, gettext("Genres populated successfully")) - |> assign(:record, updated_record)} + |> put_toast(:info, gettext("In progress - record will update automatically"))} {:error, reason} -> {:noreply, socket |> put_toast( :error, - gettext("Error populating genres") <> "," <> inspect(reason) + gettext("Error") <> "," <> inspect(reason) )} end end diff --git a/lib/music_library_web/live/wishlist_live/show.ex b/lib/music_library_web/live/wishlist_live/show.ex index 17862f23..4f4f841c 100644 --- a/lib/music_library_web/live/wishlist_live/show.ex +++ b/lib/music_library_web/live/wishlist_live/show.ex @@ -91,19 +91,18 @@ defmodule MusicLibraryWeb.WishlistLive.Show do def handle_event("populate_genres", %{"id" => id}, socket) do record = Records.get_record!(id) - case Records.populate_genres(record) do - {:ok, updated_record} -> + case Records.populate_genres_async(record) do + {:ok, _worker} -> {:noreply, socket - |> put_toast(:info, gettext("Genres populated successfully")) - |> assign(:record, updated_record)} + |> put_toast(:info, gettext("In progress - record will update automatically"))} {:error, reason} -> {:noreply, socket |> put_toast( :error, - gettext("Error populating genres") <> "," <> inspect(reason) + gettext("Error") <> "," <> inspect(reason) )} end end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 6b437b2b..63da01ce 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -346,18 +346,6 @@ msgstr "" msgid "Loading play count" msgstr "" -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Error populating genres" -msgstr "" - -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Genres populated successfully" -msgstr "" - #: lib/music_library_web/live/collection_live/show.html.heex #: lib/music_library_web/live/wishlist_live/show.html.heex #, elixir-autogen, elixir-format diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index df50e27c..3697bc3b 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -346,18 +346,6 @@ msgstr "" msgid "Loading play count" msgstr "" -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Error populating genres" -msgstr "" - -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Genres populated successfully" -msgstr "" - #: lib/music_library_web/live/collection_live/show.html.heex #: lib/music_library_web/live/wishlist_live/show.html.heex #, elixir-autogen, elixir-format