Add logic to backfill scrobbled_tracks

Chokes on duplicate primary keys
This commit is contained in:
Claudio Ortolina
2025-05-31 20:12:36 +01:00
parent a27b38f192
commit 750f0a74d1
4 changed files with 52 additions and 7 deletions
@@ -0,0 +1,29 @@
defmodule LastFm.Worker.BackfillScrobbledTracks do
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
alias MusicLibrary.BackgroundRepo
@backfill_delay 5
@batch_size 100
@impl Oban.Worker
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
{@batch_size, nil} ->
next_to_uts = LastFm.lowest_scrobbled_at_uts()
%{"to_uts" => next_to_uts}
|> new(schedule_in: @backfill_delay)
|> BackgroundRepo.insert()
{other_count, nil} when is_integer(other_count) ->
:ok
error ->
error
end
end
end