diff --git a/lib/pinchflat/metadata/source_metadata_storage_worker.ex b/lib/pinchflat/metadata/source_metadata_storage_worker.ex index eda6297..b922f62 100644 --- a/lib/pinchflat/metadata/source_metadata_storage_worker.ex +++ b/lib/pinchflat/metadata/source_metadata_storage_worker.ex @@ -38,9 +38,9 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorker do - The NFO file for the source (if specified) - Downloads and stores source images (if specified) - The worker is kicked off after a source is inserted/updated - this can - take an unknown amount of time so don't rely on this data being here - before, say, the first indexing or downloading task is complete. + The worker is kicked off after a source is inserted or it's original_url + is updated - this can take an unknown amount of time so don't rely on this + data being here before, say, the first indexing or downloading task is complete. Returns :ok """ diff --git a/lib/pinchflat/sources/sources.ex b/lib/pinchflat/sources/sources.ex index f9a3658..ab6fc2e 100644 --- a/lib/pinchflat/sources/sources.ex +++ b/lib/pinchflat/sources/sources.ex @@ -237,7 +237,7 @@ defmodule Pinchflat.Sources do if run_post_commit_tasks do maybe_handle_media_tasks(changeset, source) maybe_run_indexing_task(changeset, source) - run_metadata_storage_task(source) + maybe_run_metadata_storage_task(changeset, source) end {:ok, source} @@ -276,9 +276,20 @@ defmodule Pinchflat.Sources do end end - # This runs every time to pick up any changes to the metadata - defp run_metadata_storage_task(source) do - SourceMetadataStorageWorker.kickoff_with_task(source) + defp maybe_run_metadata_storage_task(changeset, source) do + case {changeset.data, changeset.changes} do + # If the changeset is new (not persisted), fetch metadata no matter what + {%{__meta__: %{state: :built}}, _} -> + SourceMetadataStorageWorker.kickoff_with_task(source) + + # If the record has been persisted, only fetch metadata if the + # original_url has changed + {_, %{original_url: _}} -> + SourceMetadataStorageWorker.kickoff_with_task(source) + + _ -> + :ok + end end defp maybe_update_slow_indexing_task(changeset, source) do diff --git a/test/pinchflat/sources_test.exs b/test/pinchflat/sources_test.exs index 3e4a1da..4fb3509 100644 --- a/test/pinchflat/sources_test.exs +++ b/test/pinchflat/sources_test.exs @@ -514,14 +514,24 @@ defmodule Pinchflat.SourcesTest do assert source.index_frequency_minutes == 0 end - test "updating will kickoff a metadata storage worker" do + test "updating will kickoff a metadata storage worker if the original_url changes" do + expect(YtDlpRunnerMock, :run, &playlist_mock/3) source = source_fixture() - update_attrs = %{name: "some updated name"} + update_attrs = %{original_url: "https://www.youtube.com/channel/cba321"} assert {:ok, %Source{} = source} = Sources.update_source(source, update_attrs) assert_enqueued(worker: SourceMetadataStorageWorker, args: %{"id" => source.id}) end + + test "updating will not kickoff a metadata storage worker other attrs change" do + source = source_fixture() + update_attrs = %{name: "some new name"} + + assert {:ok, %Source{}} = Sources.update_source(source, update_attrs) + + refute_enqueued(worker: SourceMetadataStorageWorker) + end end describe "update_source/3 when testing options" do