Hooked up async deletion to media profiles as well
This commit is contained in:
parent
f66c7c2751
commit
858faaf321
7 changed files with 151 additions and 79 deletions
38
lib/pinchflat/profiles/media_profile_deletion_worker.ex
Normal file
38
lib/pinchflat/profiles/media_profile_deletion_worker.ex
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
defmodule Pinchflat.Profiles.MediaProfileDeletionWorker do
|
||||
@moduledoc false
|
||||
|
||||
use Oban.Worker,
|
||||
queue: :local_data,
|
||||
tags: ["media_profiles", "local_data"]
|
||||
|
||||
require Logger
|
||||
|
||||
alias __MODULE__
|
||||
alias Pinchflat.Profiles
|
||||
|
||||
@doc """
|
||||
Starts the profile deletion worker. Does not attach it to a task like `kickoff_with_task/2`
|
||||
since deletion also cancels all tasks for the profile
|
||||
|
||||
Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}
|
||||
"""
|
||||
def kickoff(profile, job_args \\ %{}, job_opts \\ []) do
|
||||
%{id: profile.id}
|
||||
|> Map.merge(job_args)
|
||||
|> MediaProfileDeletionWorker.new(job_opts)
|
||||
|> Oban.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a profile and optionally deletes its files
|
||||
|
||||
Returns :ok
|
||||
"""
|
||||
@impl Oban.Worker
|
||||
def perform(%Oban.Job{args: %{"id" => profile_id} = args}) do
|
||||
delete_files = Map.get(args, "delete_files", false)
|
||||
profile = Profiles.get_media_profile!(profile_id)
|
||||
|
||||
Profiles.delete_media_profile(profile, delete_files: delete_files)
|
||||
end
|
||||
end
|
||||
|
|
@ -5,10 +5,12 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do
|
|||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Profiles
|
||||
alias Pinchflat.Profiles.MediaProfile
|
||||
alias Pinchflat.Profiles.MediaProfileDeletionWorker
|
||||
|
||||
def index(conn, _params) do
|
||||
media_profiles =
|
||||
MediaProfile
|
||||
|> where([mp], is_nil(mp.marked_for_deletion_at))
|
||||
|> order_by(asc: :name)
|
||||
|> Repo.all()
|
||||
|
||||
|
|
@ -70,19 +72,15 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do
|
|||
end
|
||||
|
||||
def delete(conn, %{"id" => id} = params) do
|
||||
delete_files = Map.get(params, "delete_files", false)
|
||||
# This awkward comparison converts the string to a boolean
|
||||
delete_files = Map.get(params, "delete_files", "") == "true"
|
||||
media_profile = Profiles.get_media_profile!(id)
|
||||
{:ok, _media_profile} = Profiles.delete_media_profile(media_profile, delete_files: delete_files)
|
||||
|
||||
flash_message =
|
||||
if delete_files do
|
||||
"Media profile, its sources, and its files deleted successfully."
|
||||
else
|
||||
"Media profile and its sources deleted successfully. Files were not deleted."
|
||||
end
|
||||
{:ok, _} = Profiles.update_media_profile(media_profile, %{marked_for_deletion_at: DateTime.utc_now()})
|
||||
MediaProfileDeletionWorker.kickoff(media_profile, %{delete_files: delete_files})
|
||||
|
||||
conn
|
||||
|> put_flash(:info, flash_message)
|
||||
|> put_flash(:info, "Media Profile deletion started. This may take a while to complete.")
|
||||
|> redirect(to: ~p"/media_profiles")
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@ defmodule PinchflatWeb.Sources.SourceController do
|
|||
source_query =
|
||||
from s in Source,
|
||||
as: :source,
|
||||
where: is_nil(s.marked_for_deletion_at),
|
||||
inner_join: mp in assoc(s, :media_profile),
|
||||
where: is_nil(s.marked_for_deletion_at) and is_nil(mp.marked_for_deletion_at),
|
||||
preload: [media_profile: mp],
|
||||
order_by: [asc: s.custom_name],
|
||||
select: map(s, ^Source.__schema__(:fields)),
|
||||
|
|
@ -126,17 +126,15 @@ defmodule PinchflatWeb.Sources.SourceController do
|
|||
end
|
||||
|
||||
def delete(conn, %{"id" => id} = params) do
|
||||
delete_files = Map.get(params, "delete_files", false)
|
||||
# This awkward comparison converts the string to a boolean
|
||||
delete_files = Map.get(params, "delete_files", "") == "true"
|
||||
source = Sources.get_source!(id)
|
||||
|
||||
{:ok, _} = Sources.update_source(source, %{marked_for_deletion_at: DateTime.utc_now()})
|
||||
SourceDeletionWorker.kickoff(source, %{delete_files: delete_files})
|
||||
|
||||
conn
|
||||
|> put_flash(
|
||||
:info,
|
||||
"Source deletion started. This may take a while to complete for large sources."
|
||||
)
|
||||
|> put_flash(:info, "Source deletion started. This may take a while to complete.")
|
||||
|> redirect(to: ~p"/sources")
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
defmodule Pinchflat.Profiles.MediaProfileDeletionWorkerTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
import Pinchflat.ProfilesFixtures
|
||||
|
||||
alias Pinchflat.Profiles.MediaProfileDeletionWorker
|
||||
|
||||
setup do
|
||||
stub(UserScriptRunnerMock, :run, fn _event_type, _data -> :ok end)
|
||||
|
||||
{:ok, %{profile: media_profile_fixture()}}
|
||||
end
|
||||
|
||||
describe "kickoff/3" do
|
||||
test "starts the worker", %{profile: profile} do
|
||||
assert [] = all_enqueued(worker: MediaProfileDeletionWorker)
|
||||
assert {:ok, _} = MediaProfileDeletionWorker.kickoff(profile)
|
||||
assert [_] = all_enqueued(worker: MediaProfileDeletionWorker)
|
||||
end
|
||||
|
||||
test "can be called with additional job arguments", %{profile: profile} do
|
||||
job_args = %{"delete_files" => true}
|
||||
|
||||
assert {:ok, _} = MediaProfileDeletionWorker.kickoff(profile, job_args)
|
||||
|
||||
assert_enqueued(worker: MediaProfileDeletionWorker, args: %{"id" => profile.id, "delete_files" => true})
|
||||
end
|
||||
end
|
||||
|
||||
describe "perform/1" do
|
||||
test "deletes the profile, sources, and media but leaves the files", %{profile: profile} do
|
||||
source = source_fixture(%{media_profile_id: profile.id})
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
perform_job(MediaProfileDeletionWorker, %{"id" => profile.id})
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(profile) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
assert File.exists?(media_item.media_filepath)
|
||||
end
|
||||
|
||||
test "deletes the profile, sources, and media and files if specified", %{profile: profile} do
|
||||
source = source_fixture(%{media_profile_id: profile.id})
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
perform_job(MediaProfileDeletionWorker, %{"id" => profile.id, "delete_files" => true})
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(profile) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
refute File.exists?(media_item.media_filepath)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -4,8 +4,6 @@ defmodule Pinchflat.Sources.SourceDeletionWorkerTest do
|
|||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Sources.SourceDeletionWorker
|
||||
|
||||
setup do
|
||||
|
|
@ -36,8 +34,8 @@ defmodule Pinchflat.Sources.SourceDeletionWorkerTest do
|
|||
|
||||
perform_job(SourceDeletionWorker, %{"id" => source.id})
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Sources.get_source!(source.id) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Media.get_media_item!(media_item.id) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
assert File.exists?(media_item.media_filepath)
|
||||
end
|
||||
|
||||
|
|
@ -46,8 +44,8 @@ defmodule Pinchflat.Sources.SourceDeletionWorkerTest do
|
|||
|
||||
perform_job(SourceDeletionWorker, %{"id" => source.id, "delete_files" => true})
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Sources.get_source!(source.id) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Media.get_media_item!(media_item.id) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
refute File.exists?(media_item.media_filepath)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
defmodule PinchflatWeb.MediaProfileControllerTest do
|
||||
use PinchflatWeb.ConnCase
|
||||
|
||||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
import Pinchflat.ProfilesFixtures
|
||||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Settings
|
||||
alias Pinchflat.Profiles.MediaProfileDeletionWorker
|
||||
|
||||
@create_attrs %{name: "some name", output_path_template: "output_template.{{ ext }}"}
|
||||
@update_attrs %{
|
||||
|
|
@ -23,8 +22,17 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
|||
|
||||
describe "index" do
|
||||
test "lists all media_profiles", %{conn: conn} do
|
||||
profile = media_profile_fixture()
|
||||
conn = get(conn, ~p"/media_profiles")
|
||||
|
||||
assert html_response(conn, 200) =~ "Media Profiles"
|
||||
assert html_response(conn, 200) =~ profile.name
|
||||
end
|
||||
|
||||
test "omits profiles that have marked_for_deletion_at set", %{conn: conn} do
|
||||
profile = media_profile_fixture(marked_for_deletion_at: DateTime.utc_now())
|
||||
conn = get(conn, ~p"/media_profiles")
|
||||
refute html_response(conn, 200) =~ profile.name
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -102,34 +110,28 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "delete media_profile when just deleting the records" do
|
||||
describe "delete media_profile in all cases" do
|
||||
setup [:create_media_profile]
|
||||
|
||||
test "deletes chosen media_profile and its associations", %{conn: conn, media_profile: media_profile} do
|
||||
source = source_fixture(media_profile_id: media_profile.id)
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
conn = delete(conn, ~p"/media_profiles/#{media_profile}")
|
||||
assert redirected_to(conn) == ~p"/media_profiles"
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_profile) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
end
|
||||
|
||||
test "redirects to the media_profiles page", %{conn: conn, media_profile: media_profile} do
|
||||
conn = delete(conn, ~p"/media_profiles/#{media_profile}")
|
||||
|
||||
assert redirected_to(conn) == ~p"/media_profiles"
|
||||
end
|
||||
|
||||
test "doesn't delete any files", %{conn: conn, media_profile: media_profile} do
|
||||
source = source_fixture(media_profile_id: media_profile.id)
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
test "sets marked_for_deletion_at", %{conn: conn, media_profile: media_profile} do
|
||||
delete(conn, ~p"/media_profiles/#{media_profile}")
|
||||
assert Repo.reload!(media_profile).marked_for_deletion_at
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete media_profile when just deleting the records" do
|
||||
setup [:create_media_profile]
|
||||
|
||||
test "enqueues a job without the delete_files arg", %{conn: conn, media_profile: media_profile} do
|
||||
delete(conn, ~p"/media_profiles/#{media_profile}")
|
||||
|
||||
assert File.exists?(media_item.media_filepath)
|
||||
assert [%{args: %{"delete_files" => false}}] = all_enqueued(worker: MediaProfileDeletionWorker)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -142,31 +144,10 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
|||
:ok
|
||||
end
|
||||
|
||||
test "deletes chosen media_profile and its associations", %{conn: conn, media_profile: media_profile} do
|
||||
source = source_fixture(media_profile_id: media_profile.id)
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
conn = delete(conn, ~p"/media_profiles/#{media_profile}?delete_files=true")
|
||||
assert redirected_to(conn) == ~p"/media_profiles"
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_profile) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
end
|
||||
|
||||
test "redirects to the media_profiles page", %{conn: conn, media_profile: media_profile} do
|
||||
conn = delete(conn, ~p"/media_profiles/#{media_profile}?delete_files=true")
|
||||
|
||||
assert redirected_to(conn) == ~p"/media_profiles"
|
||||
end
|
||||
|
||||
test "deletes the files", %{conn: conn, media_profile: media_profile} do
|
||||
source = source_fixture(media_profile_id: media_profile.id)
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
test "enqueues a job with the delete_files arg", %{conn: conn, media_profile: media_profile} do
|
||||
delete(conn, ~p"/media_profiles/#{media_profile}?delete_files=true")
|
||||
|
||||
refute File.exists?(media_item.media_filepath)
|
||||
assert [%{args: %{"delete_files" => true}}] = all_enqueued(worker: MediaProfileDeletionWorker)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ defmodule PinchflatWeb.SourceControllerTest do
|
|||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Settings
|
||||
alias Pinchflat.Sources.SourceDeletionWorker
|
||||
alias Pinchflat.Downloading.MediaDownloadWorker
|
||||
alias Pinchflat.Metadata.SourceMetadataStorageWorker
|
||||
alias Pinchflat.SlowIndexing.MediaCollectionIndexingWorker
|
||||
|
|
@ -35,12 +36,23 @@ defmodule PinchflatWeb.SourceControllerTest do
|
|||
test "lists all sources", %{conn: conn} do
|
||||
source = source_fixture()
|
||||
conn = get(conn, ~p"/sources")
|
||||
|
||||
assert html_response(conn, 200) =~ "Sources"
|
||||
assert html_response(conn, 200) =~ source.custom_name
|
||||
end
|
||||
|
||||
test "omits sources that have marked_for_deletion_at set", %{conn: conn} do
|
||||
source = source_fixture(marked_for_deletion_at: DateTime.utc_now())
|
||||
conn = get(conn, ~p"/sources")
|
||||
|
||||
refute html_response(conn, 200) =~ source.custom_name
|
||||
end
|
||||
|
||||
test "omits sources who's media profile has marked_for_deletion_at set", %{conn: conn} do
|
||||
media_profile = media_profile_fixture(marked_for_deletion_at: DateTime.utc_now())
|
||||
source = source_fixture(media_profile_id: media_profile.id)
|
||||
conn = get(conn, ~p"/sources")
|
||||
|
||||
refute html_response(conn, 200) =~ source.custom_name
|
||||
end
|
||||
end
|
||||
|
|
@ -149,32 +161,22 @@ defmodule PinchflatWeb.SourceControllerTest do
|
|||
end
|
||||
|
||||
describe "delete source when just deleting the records" do
|
||||
test "deletes chosen source and media_items", %{conn: conn, source: source, media_item: media_item} do
|
||||
setup [:create_source]
|
||||
|
||||
test "enqueues a job without the delete_files arg", %{conn: conn, source: source} do
|
||||
delete(conn, ~p"/sources/#{source}")
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
end
|
||||
|
||||
test "does not delete the files", %{conn: conn, source: source, media_item: media_item} do
|
||||
delete(conn, ~p"/sources/#{source}")
|
||||
assert File.exists?(media_item.media_filepath)
|
||||
assert [%{args: %{"delete_files" => false}}] = all_enqueued(worker: SourceDeletionWorker)
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete source when deleting the records and files" do
|
||||
setup [:create_source]
|
||||
|
||||
test "deletes chosen source and media_items", %{conn: conn, source: source, media_item: media_item} do
|
||||
test "enqueues a job without the delete_files arg", %{conn: conn, source: source} do
|
||||
delete(conn, ~p"/sources/#{source}?delete_files=true")
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(source) end
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(media_item) end
|
||||
end
|
||||
|
||||
test "deletes the files", %{conn: conn, source: source, media_item: media_item} do
|
||||
delete(conn, ~p"/sources/#{source}?delete_files=true")
|
||||
refute File.exists?(media_item.media_filepath)
|
||||
assert [%{args: %{"delete_files" => true}}] = all_enqueued(worker: SourceDeletionWorker)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue