Fixed bug where indexing job wouldn't run for the first time
This commit is contained in:
parent
8b2d7ec759
commit
381eaed40a
2 changed files with 46 additions and 24 deletions
|
|
@ -15,8 +15,9 @@ defmodule Pinchflat.Workers.MediaIndexingWorker do
|
||||||
@doc """
|
@doc """
|
||||||
The ID is that of a source _record_, not a YouTube channel/playlist ID. Indexes
|
The ID is that of a source _record_, not a YouTube channel/playlist ID. Indexes
|
||||||
the provided source, kicks off downloads for each new MediaItem, and
|
the provided source, kicks off downloads for each new MediaItem, and
|
||||||
reschedules the job to run again in the future (as determined by the
|
reschedules the job to run again in the future. It will ALWAYS index a source
|
||||||
souce's `index_frequency_minutes` field).
|
if it's never been indexed before, but rescheduling is determined by the
|
||||||
|
`index_frequency_minutes` field.
|
||||||
|
|
||||||
README: Re-scheduling here works a little different than you may expect.
|
README: Re-scheduling here works a little different than you may expect.
|
||||||
The reschedule time is relative to the time the job has actually _completed_.
|
The reschedule time is relative to the time the job has actually _completed_.
|
||||||
|
|
@ -39,18 +40,31 @@ defmodule Pinchflat.Workers.MediaIndexingWorker do
|
||||||
def perform(%Oban.Job{args: %{"id" => source_id}}) do
|
def perform(%Oban.Job{args: %{"id" => source_id}}) do
|
||||||
source = Sources.get_source!(source_id)
|
source = Sources.get_source!(source_id)
|
||||||
|
|
||||||
if source.index_frequency_minutes <= 0 do
|
case {source.index_frequency_minutes, source.last_indexed_at} do
|
||||||
:ok
|
{index_freq, _} when index_freq > 0 ->
|
||||||
else
|
# If the indexing is on a schedule simply run indexing and reschedule
|
||||||
index_media_and_reschedule(source)
|
index_media(source)
|
||||||
|
reschedule_indexing(source)
|
||||||
|
|
||||||
|
{_, nil} ->
|
||||||
|
# If the source has never been indexed, index it once
|
||||||
|
# even if it's not meant to reschedule
|
||||||
|
index_media(source)
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
# If the source HAS been indexed and is not meant to reschedule,
|
||||||
|
# perform a no-op
|
||||||
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp index_media_and_reschedule(source) do
|
defp index_media(source) do
|
||||||
SourceTasks.index_media_items(source)
|
SourceTasks.index_media_items(source)
|
||||||
# This method handles the case where a source is set to not download media
|
# This method handles the case where a source is set to not download media
|
||||||
SourceTasks.enqueue_pending_media_tasks(source)
|
SourceTasks.enqueue_pending_media_tasks(source)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp reschedule_indexing(source) do
|
||||||
source
|
source
|
||||||
|> Map.take([:id])
|
|> Map.take([:id])
|
||||||
|> MediaIndexingWorker.new(schedule_in: source.index_frequency_minutes * 60)
|
|> MediaIndexingWorker.new(schedule_in: source.index_frequency_minutes * 60)
|
||||||
|
|
|
||||||
|
|
@ -12,23 +12,6 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
|
||||||
setup :verify_on_exit!
|
setup :verify_on_exit!
|
||||||
|
|
||||||
describe "perform/1" do
|
describe "perform/1" do
|
||||||
test "it does not do any indexing if the source shouldn't be indexed" do
|
|
||||||
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot -> {:ok, ""} end)
|
|
||||||
|
|
||||||
source = source_fixture(index_frequency_minutes: -1)
|
|
||||||
|
|
||||||
perform_job(MediaIndexingWorker, %{id: source.id})
|
|
||||||
end
|
|
||||||
|
|
||||||
test "it does not reschedule if the source shouldn't be indexed" do
|
|
||||||
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot -> {:ok, ""} end)
|
|
||||||
|
|
||||||
source = source_fixture(index_frequency_minutes: -1)
|
|
||||||
perform_job(MediaIndexingWorker, %{id: source.id})
|
|
||||||
|
|
||||||
refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id})
|
|
||||||
end
|
|
||||||
|
|
||||||
test "it indexes the source if it should be indexed" do
|
test "it indexes the source if it should be indexed" do
|
||||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, ""} end)
|
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, ""} end)
|
||||||
|
|
||||||
|
|
@ -37,6 +20,31 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
|
||||||
perform_job(MediaIndexingWorker, %{id: source.id})
|
perform_job(MediaIndexingWorker, %{id: source.id})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it indexes the source no matter what if the source has never been indexed before" do
|
||||||
|
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, ""} end)
|
||||||
|
|
||||||
|
source = source_fixture(index_frequency_minutes: 0, last_indexed_at: nil)
|
||||||
|
|
||||||
|
perform_job(MediaIndexingWorker, %{id: source.id})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it does not do any indexing if the source has been indexed and shouldn't be rescheduled" do
|
||||||
|
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot -> {:ok, ""} end)
|
||||||
|
|
||||||
|
source = source_fixture(index_frequency_minutes: -1, last_indexed_at: DateTime.utc_now())
|
||||||
|
|
||||||
|
perform_job(MediaIndexingWorker, %{id: source.id})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it does not reschedule if the source shouldn't be indexed" do
|
||||||
|
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, ""} end)
|
||||||
|
|
||||||
|
source = source_fixture(index_frequency_minutes: -1)
|
||||||
|
perform_job(MediaIndexingWorker, %{id: source.id})
|
||||||
|
|
||||||
|
refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id})
|
||||||
|
end
|
||||||
|
|
||||||
test "it kicks off a download job for each pending media item" do
|
test "it kicks off a download job for each pending media item" do
|
||||||
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, source_attributes_return_fixture()} end)
|
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, source_attributes_return_fixture()} end)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue