Media indexing worker test

This commit is contained in:
Kieran Eglin 2024-02-01 21:11:31 -08:00
parent bbae39b523
commit 5ec0c0646c
No known key found for this signature in database
GPG key ID: 193984967FCF432D
3 changed files with 31 additions and 31 deletions

View file

@ -23,10 +23,10 @@ defmodule Pinchflat.Media do
Returns [%MediaItem{}, ...].
"""
def list_pending_media_items_for(%Channel{} = channel) do
def list_pending_media_items_for(%Channel{} = source) do
from(
m in MediaItem,
where: m.channel_id == ^channel.id and is_nil(m.media_filepath)
where: m.source_id == ^source.id and is_nil(m.media_filepath)
)
|> Repo.all()
end

View file

@ -37,24 +37,24 @@ defmodule Pinchflat.Workers.MediaIndexingWorker do
Returns :ok | {:ok, %Task{}}
"""
def perform(%Oban.Job{args: %{"id" => channel_id}}) do
channel = MediaSource.get_source!(channel_id)
def perform(%Oban.Job{args: %{"id" => source_id}}) do
source = MediaSource.get_source!(source_id)
if channel.index_frequency_minutes <= 0 do
if source.index_frequency_minutes <= 0 do
:ok
else
index_media_and_reschedule(channel)
index_media_and_reschedule(source)
end
end
defp index_media_and_reschedule(channel) do
MediaSource.index_media_items(channel)
enqueue_video_downloads(channel)
defp index_media_and_reschedule(source) do
MediaSource.index_media_items(source)
enqueue_video_downloads(source)
channel
source
|> Map.take([:id])
|> MediaIndexingWorker.new(schedule_in: channel.index_frequency_minutes * 60)
|> Tasks.create_job_with_task(channel)
|> MediaIndexingWorker.new(schedule_in: source.index_frequency_minutes * 60)
|> Tasks.create_job_with_task(source)
|> case do
{:ok, task} -> {:ok, task}
{:error, :duplicate_job} -> {:ok, :job_exists}
@ -67,8 +67,8 @@ defmodule Pinchflat.Workers.MediaIndexingWorker do
# or somehow got de-queued.
#
# I'm not sure of a case where this would happen, but it's cheap insurance.
defp enqueue_video_downloads(channel) do
channel
defp enqueue_video_downloads(source) do
source
|> Media.list_pending_media_items_for()
|> Enum.each(fn media_item ->
media_item

View file

@ -12,36 +12,36 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
setup :verify_on_exit!
describe "perform/1" do
test "it does not do any indexing if the channel shouldn't be indexed" 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: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
end
test "it does not reschedule if the channel shouldn't be indexed" do
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: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => channel.id})
refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id})
end
test "it indexes the channel 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)
source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
end
test "it kicks off a download job for each pending media item" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "video1"} end)
source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
assert [_] = all_enqueued(worker: VideoDownloadWorker)
end
@ -50,8 +50,8 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "video1"} end)
source = source_fixture(index_frequency_minutes: 10)
media_item_fixture(%{channel_id: channel.id, media_filepath: nil})
perform_job(MediaIndexingWorker, %{id: channel.id})
media_item_fixture(%{source_id: source.id, media_filepath: nil})
perform_job(MediaIndexingWorker, %{id: source.id})
assert [_, _] = all_enqueued(worker: VideoDownloadWorker)
end
@ -60,7 +60,7 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "video1\nvideo1"} end)
source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
# Only one job should be enqueued, since the second video is a duplicate
assert [_] = all_enqueued(worker: VideoDownloadWorker)
@ -70,12 +70,12 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
assert_enqueued(
worker: MediaIndexingWorker,
args: %{"id" => channel.id},
scheduled_at: now_plus(channel.index_frequency_minutes, :minutes)
args: %{"id" => source.id},
scheduled_at: now_plus(source.index_frequency_minutes, :minutes)
)
end
@ -86,7 +86,7 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
task_count_fetcher = fn -> Enum.count(Tasks.list_tasks()) end
assert_changed([from: 0, to: 1], task_count_fetcher, fn ->
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
end)
end
@ -96,14 +96,14 @@ defmodule Pinchflat.Workers.MediaIndexingWorkerTest do
source = source_fixture(index_frequency_minutes: 10)
media_item_fetcher = fn ->
channel
source
|> Repo.preload(:media_items)
|> Map.get(:media_items)
|> Enum.map(fn media_item -> media_item.media_id end)
end
assert_changed([from: [], to: ["video1", "video2"]], media_item_fetcher, fn ->
perform_job(MediaIndexingWorker, %{id: channel.id})
perform_job(MediaIndexingWorker, %{id: source.id})
end)
end
end