Only clean songs if server owns file

This commit is contained in:
Chris McCord 2022-01-31 08:41:34 -05:00
parent 826cf1d4f9
commit de2f473624
6 changed files with 14 additions and 11 deletions

View file

@ -29,7 +29,7 @@ defmodule LiveBeats.Application do
name: PresenceClient},
# Start the Endpoint (http/https)
LiveBeatsWeb.Endpoint,
{LiveBeats.SongsCleaner, count: 7, interval: :day}
{LiveBeats.SongsCleaner, interval: {7, :day}}
# Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg}

View file

@ -360,11 +360,14 @@ defmodule LiveBeats.MediaLibrary do
end
def expire_songs_older_than(count, interval) when interval in [:month, :day, :second] do
server_ip = LiveBeats.config([:files, :server_ip])
Ecto.Multi.new()
|> Ecto.Multi.delete_all(
:delete_expired_songs,
from(s in Song,
where: s.inserted_at < from_now(^(-count), ^to_string(interval)),
where: s.server_ip == ^server_ip,
select: %{user_id: s.user_id, mp3_filepath: s.mp3_filepath}
)
)

View file

@ -9,16 +9,13 @@ defmodule LiveBeats.SongsCleaner do
@poll_interval :timer.minutes(60)
def start_link(opts) do
GenServer.start_link(__MODULE__, opts)
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
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})}
{count, interval} = Keyword.fetch!(opts, :interval)
{:ok, schedule_cleanup(%{count: count, interval: interval}, 0)}
end
@impl true
@ -27,8 +24,8 @@ defmodule LiveBeats.SongsCleaner do
{:noreply, schedule_cleanup(state)}
end
defp schedule_cleanup(state) do
Process.send_after(self(), :remove_songs, @poll_interval)
defp schedule_cleanup(state, after_ms \\ @poll_interval) do
Process.send_after(self(), :remove_songs, after_ms)
state
end
end

View file

@ -165,7 +165,7 @@
<%= @inner_content %>
</main>
<div class="relative">
<div class="fixed bottom-0 right-0 bg-gray-900 text-gray-200 px-2 rounded-tl-md text-sm w-[114px] min-w-max" phx-update="ignore">
<div id="ping-container" class="fixed bottom-0 right-0 bg-gray-900 text-gray-200 px-2 rounded-tl-md text-sm w-[114px] min-w-max" phx-update="ignore">
<span id="ping" phx-hook="Ping"></span>
<%= if @region do %><img class="inline w-5 h-5 absolute right-0" src={"https://fly.io/ui/images/#{@region}.svg"} /><% end %>
</div>

View file

@ -10,6 +10,8 @@ defmodule LiveBeats.MediaLibraryFixtures do
Generate a song.
"""
def song_fixture(attrs \\ %{}) do
{:ok, server_ip} = EctoNetwork.INET.cast(LiveBeats.config([:files, :server_ip]))
{:ok, song} =
struct!(
Song,
@ -23,6 +25,7 @@ defmodule LiveBeats.MediaLibraryFixtures do
mp3_url: "//example.com/mp3.mp3",
mp3_filename: "mp3.mp3",
mp3_filepath: "/data/mp3.mp3",
server_ip: server_ip,
status: :stopped
})
)

View file

@ -1,2 +1,2 @@
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(LiveBeats.Repo, :manual)
Ecto.Adapters.SQL.Sandbox.mode(LiveBeats.Repo, :auto)