live_beats/lib/live_beats/songs_cleaner.ex

32 lines
786 B
Elixir
Raw Normal View History

2022-01-11 23:17:44 +00:00
defmodule LiveBeats.SongsCleaner do
@moduledoc """
2022-01-17 19:11:26 +00:00
Expire user songs using a polling interval.
2022-01-11 23:17:44 +00:00
"""
2022-01-17 19:11:26 +00:00
use GenServer
2022-01-11 23:17:44 +00:00
alias LiveBeats.MediaLibrary
2022-01-17 19:11:26 +00:00
@poll_interval :timer.minutes(60)
2022-01-11 23:17:44 +00:00
def start_link(opts) do
2022-01-31 13:41:34 +00:00
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
2022-01-11 23:17:44 +00:00
end
@impl true
def init(opts) do
2022-01-31 13:41:34 +00:00
{count, interval} = Keyword.fetch!(opts, :interval)
{:ok, schedule_cleanup(%{count: count, interval: interval}, 0)}
2022-01-11 23:17:44 +00:00
end
@impl true
def handle_info(:remove_songs, %{count: count, interval: interval} = state) do
2022-01-17 19:11:26 +00:00
MediaLibrary.expire_songs_older_than(count, interval)
{:noreply, schedule_cleanup(state)}
2022-01-11 23:17:44 +00:00
end
2022-01-31 13:41:34 +00:00
defp schedule_cleanup(state, after_ms \\ @poll_interval) do
Process.send_after(self(), :remove_songs, after_ms)
2022-01-17 19:11:26 +00:00
state
2022-01-11 23:17:44 +00:00
end
end