From f6eedb178360ed046a5e501e6af306672aee0aa5 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Thu, 21 Nov 2024 11:53:04 -0800 Subject: [PATCH] Updated slow indexing to maintain its old schedule if re-enabled --- .../slow_indexing/slow_indexing_helpers.ex | 14 ++++++++- lib/pinchflat/sources/sources.ex | 2 -- .../slow_indexing_helpers_test.exs | 30 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex index 643390f..61ba897 100644 --- a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex +++ b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex @@ -28,10 +28,12 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do Returns {:ok, %Task{}} """ def kickoff_indexing_task(%Source{} = source, job_args \\ %{}, job_opts \\ []) do + job_offset_seconds = 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) + MediaCollectionIndexingWorker.kickoff_with_task(source, job_args, job_opts ++ [schedule_in: job_offset_seconds]) end @doc """ @@ -154,4 +156,14 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do changeset end end + + # Find the difference between the current time and the last time the source was indexed + defp calculate_job_offset_seconds(%Source{last_indexed_at: nil}), do: 0 + + defp calculate_job_offset_seconds(source) do + offset_seconds = DateTime.diff(DateTime.utc_now(), source.last_indexed_at, :second) + index_frequency_seconds = source.index_frequency_minutes * 60 + + max(0, index_frequency_seconds - offset_seconds) + end end diff --git a/lib/pinchflat/sources/sources.ex b/lib/pinchflat/sources/sources.ex index f4482c0..a7da136 100644 --- a/lib/pinchflat/sources/sources.ex +++ b/lib/pinchflat/sources/sources.ex @@ -328,8 +328,6 @@ defmodule Pinchflat.Sources do case {current_changes, applied_changes} do {%{index_frequency_minutes: mins}, %{enabled: true}} when mins > 0 -> - # TODO: consider scheduling the task for the future based on the index_frequency_minutes - # compared to `last_indexed_at` SlowIndexingHelpers.kickoff_indexing_task(source) {%{enabled: true}, %{index_frequency_minutes: mins}} when mins > 0 -> diff --git a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs index c7d9a1c..b3e8004 100644 --- a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs +++ b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs @@ -23,6 +23,36 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do assert_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) end + test "schedules a job for the future based on when the source was last indexed" do + source = source_fixture(index_frequency_minutes: 30, last_indexed_at: now_minus(5, :minutes)) + + assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source) + + [job] = all_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) + + assert_in_delta DateTime.diff(job.scheduled_at, DateTime.utc_now(), :minute), 25, 1 + end + + test "schedules a job immediately if the source was indexed far in the past" do + source = source_fixture(index_frequency_minutes: 30, last_indexed_at: now_minus(60, :minutes)) + + assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source) + + [job] = all_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) + + assert_in_delta DateTime.diff(job.scheduled_at, DateTime.utc_now(), :second), 0, 1 + end + + test "schedules a job immediately if the source has never been indexed" do + source = source_fixture(index_frequency_minutes: 30, last_indexed_at: nil) + + assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source) + + [job] = all_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) + + assert_in_delta DateTime.diff(job.scheduled_at, DateTime.utc_now(), :second), 0, 1 + end + test "creates and attaches a task" do source = source_fixture(index_frequency_minutes: 1)