[WIP] added delay between indexing and downloading in some cases

This commit is contained in:
Kieran Eglin 2024-05-27 11:54:06 -07:00
parent 1305a485a0
commit 5d624f545b
No known key found for this signature in database
GPG key ID: 193984967FCF432D
3 changed files with 23 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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