Files
music_library/lib/music_library/batch.ex
T
Claudio Ortolina 7cf9b4e7f8 First pass at uniformed types, specs and docs
- spec public functions (skipping controllers, views, live views and
components)
- use types instead of explanations in docs
- remove redundant docs
- fix typos
2026-03-06 08:33:11 +00:00

35 lines
843 B
Elixir

defmodule MusicLibrary.Batch do
alias MusicLibrary.Repo
require Logger
@spec run_on_all(Ecto.Queryable.t(), String.t(), (struct() ->
:ok | {:ok, term()} | {:error, term()})) ::
{:ok, [String.t()]}
def run_on_all(queryable, label, fun) do
stream = Repo.stream(queryable, max_rows: 50)
Repo.transaction(
fn ->
Enum.reduce(stream, [], fn record, acc ->
case fun.(record) do
{:error, reason} ->
Logger.error(
"Failed to run function on #{label} #{record.id} with #{inspect(reason)}"
)
[record.id | acc]
:ok ->
acc
{:ok, _result} ->
acc
end
end)
end,
timeout: :infinity
)
end
end