mirror of
https://github.com/kieraneglin/pinchflat.git
synced 2026-01-23 02:24:24 +00:00
[Performance] Asyncronously delete sources and media profiles (#277)
* [WIP] started on source deletion * Removed unneeded test blocks * Added marked_for_deletion_at to sources and media profiles * Hooked up async deletion to media profiles as well
This commit is contained in:
parent
7a01db05dd
commit
d5ae41cdab
14 changed files with 278 additions and 92 deletions
|
|
@ -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
|
||||
52
test/pinchflat/sources/source_deletion_worker_test.exs
Normal file
52
test/pinchflat/sources/source_deletion_worker_test.exs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
defmodule Pinchflat.Sources.SourceDeletionWorkerTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
|
||||
alias Pinchflat.Sources.SourceDeletionWorker
|
||||
|
||||
setup do
|
||||
stub(UserScriptRunnerMock, :run, fn _event_type, _data -> :ok end)
|
||||
|
||||
{:ok, %{source: source_fixture()}}
|
||||
end
|
||||
|
||||
describe "kickoff/3" do
|
||||
test "starts the worker", %{source: source} do
|
||||
assert [] = all_enqueued(worker: SourceDeletionWorker)
|
||||
assert {:ok, _} = SourceDeletionWorker.kickoff(source)
|
||||
assert [_] = all_enqueued(worker: SourceDeletionWorker)
|
||||
end
|
||||
|
||||
test "can be called with additional job arguments", %{source: source} do
|
||||
job_args = %{"delete_files" => true}
|
||||
|
||||
assert {:ok, _} = SourceDeletionWorker.kickoff(source, job_args)
|
||||
|
||||
assert_enqueued(worker: SourceDeletionWorker, args: %{"id" => source.id, "delete_files" => true})
|
||||
end
|
||||
end
|
||||
|
||||
describe "perform/1" do
|
||||
test "deletes the source but leaves the files", %{source: source} do
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
perform_job(SourceDeletionWorker, %{"id" => source.id})
|
||||
|
||||
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 source and files if specified", %{source: source} do
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
perform_job(SourceDeletionWorker, %{"id" => source.id, "delete_files" => true})
|
||||
|
||||
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
|
||||
|
|
@ -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
|
||||
|
|
@ -33,8 +34,26 @@ defmodule PinchflatWeb.SourceControllerTest do
|
|||
|
||||
describe "index" 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
|
||||
|
||||
|
|
@ -127,51 +146,37 @@ defmodule PinchflatWeb.SourceControllerTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "delete source when just deleting the records" do
|
||||
describe "delete source in all cases" do
|
||||
setup [:create_source]
|
||||
|
||||
test "deletes chosen source and media_items", %{conn: conn, source: source, media_item: media_item} 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 "redirects to the sources page", %{conn: conn, source: source} do
|
||||
conn = delete(conn, ~p"/sources/#{source}")
|
||||
assert redirected_to(conn) == ~p"/sources"
|
||||
end
|
||||
|
||||
test "does not delete the files", %{conn: conn, source: source, media_item: media_item} do
|
||||
test "sets marked_for_deletion_at", %{conn: conn, source: source} do
|
||||
delete(conn, ~p"/sources/#{source}")
|
||||
assert File.exists?(media_item.media_filepath)
|
||||
assert Repo.reload!(source).marked_for_deletion_at
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete source when just deleting the records" do
|
||||
setup [:create_source]
|
||||
|
||||
test "enqueues a job without the delete_files arg", %{conn: conn, source: source} do
|
||||
delete(conn, ~p"/sources/#{source}")
|
||||
|
||||
assert [%{args: %{"delete_files" => false}}] = all_enqueued(worker: SourceDeletionWorker)
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete source when deleting the records and files" do
|
||||
setup [:create_source]
|
||||
|
||||
setup do
|
||||
stub(UserScriptRunnerMock, :run, fn _event_type, _data -> {:ok, "", 0} end)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
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 "redirects to the sources page", %{conn: conn, source: source} do
|
||||
conn = delete(conn, ~p"/sources/#{source}?delete_files=true")
|
||||
assert redirected_to(conn) == ~p"/sources"
|
||||
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…
Add table
Add a link
Reference in a new issue