From de2f4736246a5d990c74f12c79d8d395ffc8b4c8 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Mon, 31 Jan 2022 08:41:34 -0500 Subject: [PATCH] Only clean songs if server owns file --- lib/live_beats/application.ex | 2 +- lib/live_beats/media_library.ex | 3 +++ lib/live_beats/songs_cleaner.ex | 13 +++++-------- lib/live_beats_web/templates/layout/live.html.heex | 2 +- test/support/fixtures/media_library_fixtures.ex | 3 +++ test/test_helper.exs | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/live_beats/application.ex b/lib/live_beats/application.ex index 4d52e9a..f310e6e 100644 --- a/lib/live_beats/application.ex +++ b/lib/live_beats/application.ex @@ -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} diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 3863b90..c845673 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -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} ) ) diff --git a/lib/live_beats/songs_cleaner.ex b/lib/live_beats/songs_cleaner.ex index 5d3ec1b..686dd2a 100644 --- a/lib/live_beats/songs_cleaner.ex +++ b/lib/live_beats/songs_cleaner.ex @@ -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 diff --git a/lib/live_beats_web/templates/layout/live.html.heex b/lib/live_beats_web/templates/layout/live.html.heex index c659972..eeca001 100644 --- a/lib/live_beats_web/templates/layout/live.html.heex +++ b/lib/live_beats_web/templates/layout/live.html.heex @@ -165,7 +165,7 @@ <%= @inner_content %>
-
+
<%= if @region do %><% end %>
diff --git a/test/support/fixtures/media_library_fixtures.ex b/test/support/fixtures/media_library_fixtures.ex index c4b3835..170ba0b 100644 --- a/test/support/fixtures/media_library_fixtures.ex +++ b/test/support/fixtures/media_library_fixtures.ex @@ -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 }) ) diff --git a/test/test_helper.exs b/test/test_helper.exs index 882e32e..cba88c4 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,2 @@ ExUnit.start() -Ecto.Adapters.SQL.Sandbox.mode(LiveBeats.Repo, :manual) +Ecto.Adapters.SQL.Sandbox.mode(LiveBeats.Repo, :auto)