mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-05-20 01:58:08 +00:00
35 lines
834 B
Elixir
35 lines
834 B
Elixir
defmodule LiveBeats.SongsCleaner do
|
|
@moduledoc """
|
|
Expire user songs using a polling interval.
|
|
"""
|
|
use GenServer
|
|
|
|
alias LiveBeats.MediaLibrary
|
|
|
|
@poll_interval :timer.minutes(60)
|
|
|
|
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)
|
|
MediaLibrary.expire_songs_older_than(count, interval)
|
|
|
|
{:ok, schedule_cleanup(%{count: count, interval: interval})}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info(:remove_songs, %{count: count, interval: interval} = state) do
|
|
MediaLibrary.expire_songs_older_than(count, interval)
|
|
{:noreply, schedule_cleanup(state)}
|
|
end
|
|
|
|
defp schedule_cleanup(state) do
|
|
Process.send_after(self(), :remove_songs, @poll_interval)
|
|
state
|
|
end
|
|
end
|