From 591e495d29c7bb0bc69ff46ca81eb51a756e44fe Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Tue, 21 Jan 2025 15:34:16 -0800 Subject: [PATCH] Sets sources to fast index on creation --- .../slow_indexing/slow_indexing_helpers.ex | 1 - lib/pinchflat/sources/sources.ex | 4 +++ .../slow_indexing_helpers_test.exs | 20 ------------- test/pinchflat/sources_test.exs | 28 +++++++++++++++++++ 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex index ed70007..60d0446 100644 --- a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex +++ b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex @@ -39,7 +39,6 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do def kickoff_indexing_task(%Source{} = source, job_args \\ %{}, job_opts \\ []) do job_offset_seconds = if job_args[:force], do: 0, else: calculate_job_offset_seconds(source) - Tasks.delete_pending_tasks_for(source, "FastIndexingWorker") Tasks.delete_pending_tasks_for(source, "MediaCollectionIndexingWorker", include_executing: true) MediaCollectionIndexingWorker.kickoff_with_task(source, job_args, job_opts ++ [schedule_in: job_offset_seconds]) diff --git a/lib/pinchflat/sources/sources.ex b/lib/pinchflat/sources/sources.ex index 99b4a56..ad8365c 100644 --- a/lib/pinchflat/sources/sources.ex +++ b/lib/pinchflat/sources/sources.ex @@ -300,6 +300,10 @@ defmodule Pinchflat.Sources do %{__meta__: %{state: :built}} -> SlowIndexingHelpers.kickoff_indexing_task(source) + if Ecto.Changeset.get_field(changeset, :fast_index) do + FastIndexingHelpers.kickoff_indexing_task(source) + end + # If the record has been persisted, only run indexing if the # indexing frequency has been changed and is now greater than 0 %{__meta__: %{state: :loaded}} -> diff --git a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs index 2ef57ef..6e00622 100644 --- a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs +++ b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs @@ -96,26 +96,6 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end end - test "deletes any pending media tasks for the source" do - source = source_fixture() - {:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id})) - task = task_fixture(source_id: source.id, job_id: job.id) - - assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source) - - assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end - end - - test "deletes any fast indexing tasks for the source" do - source = source_fixture() - {:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id})) - task = task_fixture(source_id: source.id, job_id: job.id) - - assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source) - - assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end - end - test "can be called with additional job arguments" do source = source_fixture(index_frequency_minutes: 1) job_args = %{"force" => true} diff --git a/test/pinchflat/sources_test.exs b/test/pinchflat/sources_test.exs index 766c71f..e8e02ae 100644 --- a/test/pinchflat/sources_test.exs +++ b/test/pinchflat/sources_test.exs @@ -294,6 +294,34 @@ defmodule Pinchflat.SourcesTest do assert_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) end + test "creation will schedule a fast indexing job if the fast_index option is set" do + expect(YtDlpRunnerMock, :run, &channel_mock/5) + + valid_attrs = %{ + media_profile_id: media_profile_fixture().id, + original_url: "https://www.youtube.com/channel/abc123", + fast_index: true + } + + assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs) + + assert_enqueued(worker: FastIndexingWorker, args: %{"id" => source.id}) + end + + test "creation will not schedule a fast indexing job if the fast_index option is not set" do + expect(YtDlpRunnerMock, :run, &channel_mock/5) + + valid_attrs = %{ + media_profile_id: media_profile_fixture().id, + original_url: "https://www.youtube.com/channel/abc123", + fast_index: false + } + + assert {:ok, %Source{}} = Sources.create_source(valid_attrs) + + refute_enqueued(worker: FastIndexingWorker) + end + test "creation schedules an index test even if the index frequency is 0" do expect(YtDlpRunnerMock, :run, &channel_mock/5)