Init SongsCleaner on startup

This commit is contained in:
Berenice Medel 2022-01-11 17:17:44 -06:00
parent ec6606f29c
commit ffee2bf6f3
3 changed files with 40 additions and 1 deletions

View file

@ -25,7 +25,8 @@ defmodule LiveBeats.Application do
presence: LiveBeatsWeb.Presence,
name: PresenceClient},
# Start the Endpoint (http/https)
LiveBeatsWeb.Endpoint
LiveBeatsWeb.Endpoint,
{LiveBeats.SongsCleaner, count: 1, interval: "month"}
# Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg}

View file

@ -367,6 +367,9 @@ defmodule LiveBeats.MediaLibrary do
end
def delete_expired_songs(count, interval) do
#for substracting the interval of time when from_now/2 is invoked
count = count * -1
Ecto.Multi.new()
|> Ecto.Multi.delete_all(
:delete_expired_songs,

View file

@ -0,0 +1,35 @@
defmodule LiveBeats.SongsCleaner do
@moduledoc """
Remove user songs that were added ... ago
"""
alias LiveBeats.MediaLibrary
use GenServer
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
end
@impl true
def init(opts) do
schedule_cleanup()
count = Keyword.fetch!(opts, :count)
interval = Keyword.fetch!(opts, :interval)
MediaLibrary.delete_expired_songs(count, interval)
{:ok, %{count: count, interval: interval}}
end
@impl true
def handle_info(:remove_songs, %{count: count, interval: interval} = state) do
MediaLibrary.delete_expired_songs(count, interval)
schedule_cleanup()
{:noreply, state}
end
defp schedule_cleanup do
Process.send_after(self(), :remove_songs, :timer.hours(3))
end
end