diff --git a/lib/pinchflat/downloading/downloading_helpers.ex b/lib/pinchflat/downloading/downloading_helpers.ex index cb679a8..7660761 100644 --- a/lib/pinchflat/downloading/downloading_helpers.ex +++ b/lib/pinchflat/downloading/downloading_helpers.ex @@ -27,13 +27,19 @@ defmodule Pinchflat.Downloading.DownloadingHelpers do Returns :ok """ - def enqueue_pending_download_tasks(%Source{download_media: true} = source) do + def enqueue_pending_download_tasks(source, opts \\ []) + + def enqueue_pending_download_tasks(%Source{download_media: true} = source, opts) do + # TODO: test + # TODO: doc + kickoff_delay = Keyword.get(opts, :kickoff_delay, 0) + source |> Media.list_pending_media_items_for() - |> Enum.each(&MediaDownloadWorker.kickoff_with_task/1) + |> Enum.each(&MediaDownloadWorker.kickoff_with_task(&1, %{}, schedule_in: kickoff_delay)) end - def enqueue_pending_download_tasks(%Source{download_media: false}) do + def enqueue_pending_download_tasks(%Source{download_media: false}, _opts) do :ok end @@ -55,13 +61,16 @@ defmodule Pinchflat.Downloading.DownloadingHelpers do Returns {:ok, %Task{}} | {:error, :should_not_download} | {:error, any()} """ - def kickoff_download_if_pending(%MediaItem{} = media_item) do + def kickoff_download_if_pending(%MediaItem{} = media_item, opts \\ []) do + # TODO: test + # TODO: doc + kickoff_delay = Keyword.get(opts, :kickoff_delay, 0) media_item = Repo.preload(media_item, :source) if media_item.source.download_media && Media.pending_download?(media_item) do Logger.info("Kicking off download for media item ##{media_item.id} (#{media_item.media_id})") - MediaDownloadWorker.kickoff_with_task(media_item) + MediaDownloadWorker.kickoff_with_task(media_item, %{}, schedule_in: kickoff_delay) else {:error, :should_not_download} end diff --git a/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex b/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex index 684ef49..108e245 100644 --- a/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex +++ b/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex @@ -42,7 +42,9 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do end end) - DownloadingHelpers.enqueue_pending_download_tasks(source) + # Wait 5s before enqueuing downloads to give the post-indexing user script a chance to run + # TODO: test + DownloadingHelpers.enqueue_pending_download_tasks(source, kickoff_delay: 5) Enum.filter(maybe_new_media_items, & &1) end diff --git a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex index 2434b92..b47e1c3 100644 --- a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex +++ b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex @@ -72,7 +72,9 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do end) Sources.update_source(source, %{last_indexed_at: DateTime.utc_now()}) - DownloadingHelpers.enqueue_pending_download_tasks(source) + # Wait 5s before enqueuing downloads to give the post-indexing user script a chance to run + # TODO: test + DownloadingHelpers.enqueue_pending_download_tasks(source, kickoff_delay: 5) result end @@ -125,7 +127,9 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do case Media.create_media_item_from_backend_attrs(source, media_attrs) do {:ok, %MediaItem{} = media_item} -> - DownloadingHelpers.kickoff_download_if_pending(media_item) + # Wait 5s before enqueuing downloads to give the post-indexing user script a chance to run + # TODO: test + DownloadingHelpers.kickoff_download_if_pending(media_item, kickoff_delay: 5) {:error, changeset} -> changeset