Populate genres via Oban with generous retries

This is due to OpenAI erroring or timing out incredibly frequently.
This commit is contained in:
Claudio Ortolina
2025-09-22 08:22:36 +03:00
parent 5ecbed6c43
commit 4e24bfe966
6 changed files with 29 additions and 34 deletions
+9
View File
@@ -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()}
@@ -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
@@ -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
@@ -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
-12
View File
@@ -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
-12
View File
@@ -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