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
|
|
|
|
GenServer.start_link(__MODULE__, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def init(opts) do
|
|
|
|
count = Keyword.fetch!(opts, :count)
|
|
|
|
interval = Keyword.fetch!(opts, :interval)
|
2022-01-17 19:11:26 +00:00
|
|
|
MediaLibrary.expire_songs_older_than(count, interval)
|
2022-01-11 23:17:44 +00:00
|
|
|
|
2022-01-17 19:11:26 +00:00
|
|
|
{:ok, schedule_cleanup(%{count: count, interval: interval})}
|
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-17 19:11:26 +00:00
|
|
|
defp schedule_cleanup(state) do
|
|
|
|
Process.send_after(self(), :remove_songs, @poll_interval)
|
|
|
|
state
|
2022-01-11 23:17:44 +00:00
|
|
|
end
|
|
|
|
end
|