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}, name: PresenceClient},
# Start the Endpoint (http/https) # Start the Endpoint (http/https)
LiveBeatsWeb.Endpoint, LiveBeatsWeb.Endpoint,
{LiveBeats.SongsCleaner, count: 7, interval: :day} {LiveBeats.SongsCleaner, interval: {7, :day}}
# Start a worker by calling: LiveBeats.Worker.start_link(arg) # Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg} # {LiveBeats.Worker, arg}

View file

@ -360,11 +360,14 @@ defmodule LiveBeats.MediaLibrary do
end end
def expire_songs_older_than(count, interval) when interval in [:month, :day, :second] do 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.new()
|> Ecto.Multi.delete_all( |> Ecto.Multi.delete_all(
:delete_expired_songs, :delete_expired_songs,
from(s in Song, from(s in Song,
where: s.inserted_at < from_now(^(-count), ^to_string(interval)), 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} 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) @poll_interval :timer.minutes(60)
def start_link(opts) do def start_link(opts) do
GenServer.start_link(__MODULE__, opts) GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end end
@impl true @impl true
def init(opts) do def init(opts) do
count = Keyword.fetch!(opts, :count) {count, interval} = Keyword.fetch!(opts, :interval)
interval = Keyword.fetch!(opts, :interval) {:ok, schedule_cleanup(%{count: count, interval: interval}, 0)}
MediaLibrary.expire_songs_older_than(count, interval)
{:ok, schedule_cleanup(%{count: count, interval: interval})}
end end
@impl true @impl true
@ -27,8 +24,8 @@ defmodule LiveBeats.SongsCleaner do
{:noreply, schedule_cleanup(state)} {:noreply, schedule_cleanup(state)}
end end
defp schedule_cleanup(state) do defp schedule_cleanup(state, after_ms \\ @poll_interval) do
Process.send_after(self(), :remove_songs, @poll_interval) Process.send_after(self(), :remove_songs, after_ms)
state state
end end
end end

View file

@ -165,7 +165,7 @@
<%= @inner_content %> <%= @inner_content %>
</main> </main>
<div class="relative"> <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> <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 %> <%= if @region do %><img class="inline w-5 h-5 absolute right-0" src={"https://fly.io/ui/images/#{@region}.svg"} /><% end %>
</div> </div>

View file

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

View file

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