Rename ScrobbleRules.Worker to Worker.ApplyScrobbleRules

This commit is contained in:
Claudio Ortolina
2025-07-07 20:14:32 +01:00
parent 81a16e409a
commit f37719f272
3 changed files with 7 additions and 7 deletions
@@ -0,0 +1,51 @@
defmodule MusicLibrary.Worker.ApplyScrobbleRules do
@moduledoc """
Oban worker that periodically applies all enabled scrobble rules.
This worker runs every 30 minutes and applies all enabled transformation rules
to the scrobbled_tracks table. It logs the results and handles errors gracefully.
"""
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
alias MusicLibrary.ScrobbleRules
require Logger
@impl Oban.Worker
def perform(%Oban.Job{args: _}) do
results = ScrobbleRules.apply_all_rules()
log_results(results)
end
defp log_results(results) do
{applied, errors} =
Enum.split_with(results, fn
{:ok, _} -> true
{:error, _} -> false
end)
total_applied = length(applied)
total_errors = length(errors)
total_tracks_updated =
applied
|> Enum.map(fn {:ok, {_, _, count}} -> count end)
|> Enum.sum()
Logger.info(fn ->
"Scrobble rules application completed: " <>
"applied #{total_applied} rules, " <>
"#{total_errors} errors, " <>
"#{total_tracks_updated} tracks updated"
end)
Enum.each(errors, fn {:error, {type, match_value, reason}} ->
Logger.error(fn ->
"failed to apply #{type} rule " <>
"with match #{match_value} " <>
"with reason #{inspect(reason)}"
end)
end)
end
end