Move BackfillScrobbledTracks worker under MusicLibrary namespace
This commit is contained in:
@@ -198,7 +198,7 @@ stubbed via `Req.Test` (configured in `config/test.exs`).
|
|||||||
| `ArtistRefreshAllMusicBrainzData` | music_brainz | Manual / cron (bulk refresh via Artists.Batch) |
|
| `ArtistRefreshAllMusicBrainzData` | music_brainz | Manual / cron (bulk refresh via Artists.Batch) |
|
||||||
| `ArtistRefreshAllDiscogsData` | discogs | Manual / cron (bulk refresh via Artists.Batch) |
|
| `ArtistRefreshAllDiscogsData` | discogs | Manual / cron (bulk refresh via Artists.Batch) |
|
||||||
| `ArtistRefreshAllWikipediaData` | wikipedia | Manual / cron (bulk refresh via Artists.Batch) |
|
| `ArtistRefreshAllWikipediaData` | wikipedia | Manual / cron (bulk refresh via Artists.Batch) |
|
||||||
| `LastFm.Worker.BackfillScrobbledTracks` | heavy_writes | Manual (self-chaining batch import) |
|
| `BackfillScrobbledTracks` | heavy_writes | Manual (self-chaining batch import) |
|
||||||
| `SendRecordsOnThisDayEmail` | default | Cron (daily "records on this day" email) |
|
| `SendRecordsOnThisDayEmail` | default | Cron (daily "records on this day" email) |
|
||||||
|
|
||||||
### Cron Workers
|
### Cron Workers
|
||||||
|
|||||||
+1
-16
@@ -3,8 +3,7 @@ defmodule LastFm do
|
|||||||
Last.fm API facade for scrobbling and listening history.
|
Last.fm API facade for scrobbling and listening history.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias LastFm.{API, Scrobble, Track, Worker}
|
alias LastFm.{API, Scrobble, Track}
|
||||||
alias MusicLibrary.{BackgroundRepo, Repo}
|
|
||||||
|
|
||||||
@spec get_tracks(keyword()) :: {:ok, [Track.t()]} | {:error, term()}
|
@spec get_tracks(keyword()) :: {:ok, [Track.t()]} | {:error, term()}
|
||||||
def get_tracks(opts) do
|
def get_tracks(opts) do
|
||||||
@@ -12,20 +11,6 @@ defmodule LastFm do
|
|||||||
API.get_recent_tracks(opts, last_fm_config)
|
API.get_recent_tracks(opts, last_fm_config)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec lowest_scrobbled_at_uts() :: integer() | nil
|
|
||||||
def lowest_scrobbled_at_uts do
|
|
||||||
Repo.aggregate(Track, :min, :scrobbled_at_uts)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec backfill_scrobbled_tracks() :: {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
|
|
||||||
def backfill_scrobbled_tracks do
|
|
||||||
to_uts = lowest_scrobbled_at_uts()
|
|
||||||
|
|
||||||
%{"to_uts" => to_uts}
|
|
||||||
|> Worker.BackfillScrobbledTracks.new()
|
|
||||||
|> BackgroundRepo.insert()
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec get_artist_info(String.t(), String.t()) :: {:ok, LastFm.Artist.t()} | {:error, term()}
|
@spec get_artist_info(String.t(), String.t()) :: {:ok, LastFm.Artist.t()} | {:error, term()}
|
||||||
def get_artist_info(musicbrainz_id, name) do
|
def get_artist_info(musicbrainz_id, name) do
|
||||||
last_fm_config = last_fm_config()
|
last_fm_config = last_fm_config()
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ defmodule MusicLibrary.ListeningStats do
|
|||||||
|
|
||||||
alias MusicLibrary.{
|
alias MusicLibrary.{
|
||||||
Artists,
|
Artists,
|
||||||
|
BackgroundRepo,
|
||||||
Collection,
|
Collection,
|
||||||
ListeningStats.Refresh,
|
ListeningStats.Refresh,
|
||||||
Records.ArtistRecord,
|
Records.ArtistRecord,
|
||||||
Records.Record,
|
Records.Record,
|
||||||
Repo,
|
Repo,
|
||||||
Wishlist
|
Wishlist,
|
||||||
|
Worker
|
||||||
}
|
}
|
||||||
|
|
||||||
@pagination Application.compile_env!(:music_library, :pagination)
|
@pagination Application.compile_env!(:music_library, :pagination)
|
||||||
@@ -79,6 +81,20 @@ defmodule MusicLibrary.ListeningStats do
|
|||||||
Refresh.refresh()
|
Refresh.refresh()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec lowest_scrobbled_at_uts() :: integer() | nil
|
||||||
|
def lowest_scrobbled_at_uts do
|
||||||
|
Repo.aggregate(Track, :min, :scrobbled_at_uts)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec backfill_scrobbled_tracks() :: {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
|
||||||
|
def backfill_scrobbled_tracks do
|
||||||
|
to_uts = lowest_scrobbled_at_uts()
|
||||||
|
|
||||||
|
%{"to_uts" => to_uts}
|
||||||
|
|> Worker.BackfillScrobbledTracks.new()
|
||||||
|
|> BackgroundRepo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
@spec artist_play_count(String.t()) :: non_neg_integer()
|
@spec artist_play_count(String.t()) :: non_neg_integer()
|
||||||
def artist_play_count(artist_musicbrainz_id) do
|
def artist_play_count(artist_musicbrainz_id) do
|
||||||
from(t in Track,
|
from(t in Track,
|
||||||
|
|||||||
+10
-6
@@ -1,19 +1,23 @@
|
|||||||
defmodule LastFm.Worker.BackfillScrobbledTracks do
|
defmodule MusicLibrary.Worker.BackfillScrobbledTracks do
|
||||||
|
@moduledoc """
|
||||||
|
Oban worker that backfills scrobbled tracks from Last.fm in batches.
|
||||||
|
|
||||||
|
Self-chaining: after importing a full batch, enqueues itself with the
|
||||||
|
next `to_uts` timestamp to continue backfilling.
|
||||||
|
"""
|
||||||
|
|
||||||
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
|
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
|
||||||
|
|
||||||
alias MusicLibrary.BackgroundRepo
|
alias MusicLibrary.{BackgroundRepo, ListeningStats}
|
||||||
|
|
||||||
@backfill_delay 1
|
@backfill_delay 1
|
||||||
@batch_size 200
|
@batch_size 200
|
||||||
|
|
||||||
@impl Oban.Worker
|
@impl Oban.Worker
|
||||||
def perform(%{args: %{"to_uts" => to_uts}}) do
|
def perform(%{args: %{"to_uts" => to_uts}}) do
|
||||||
# importing is an all or nothing operation, which means that we can
|
|
||||||
# use the returning count to determine if we reached the end of the backfilling
|
|
||||||
# process.
|
|
||||||
case LastFm.Import.batch(to_uts: to_uts, limit: @batch_size) do
|
case LastFm.Import.batch(to_uts: to_uts, limit: @batch_size) do
|
||||||
{:ok, @batch_size} ->
|
{:ok, @batch_size} ->
|
||||||
next_to_uts = LastFm.lowest_scrobbled_at_uts()
|
next_to_uts = ListeningStats.lowest_scrobbled_at_uts()
|
||||||
|
|
||||||
%{"to_uts" => next_to_uts}
|
%{"to_uts" => next_to_uts}
|
||||||
|> new(schedule_in: @backfill_delay)
|
|> new(schedule_in: @backfill_delay)
|
||||||
Reference in New Issue
Block a user