From bbae39b5231a855de9f69f7686d4d57981d114e9 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Thu, 1 Feb 2024 21:06:55 -0800 Subject: [PATCH] channel tasks test --- lib/pinchflat/tasks/channel_tasks.ex | 12 ++++++------ test/pinchflat/tasks/channel_tasks_test.exs | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/pinchflat/tasks/channel_tasks.ex b/lib/pinchflat/tasks/channel_tasks.ex index a2b1cd2..827f9a8 100644 --- a/lib/pinchflat/tasks/channel_tasks.ex +++ b/lib/pinchflat/tasks/channel_tasks.ex @@ -8,21 +8,21 @@ defmodule Pinchflat.Tasks.ChannelTasks do alias Pinchflat.Workers.MediaIndexingWorker @doc """ - Starts tasks for indexing a channel's media. + Starts tasks for indexing a source's media. Returns {:ok, :should_not_index} | {:ok, %Task{}}. """ - def kickoff_indexing_task(%Channel{} = channel) do - Tasks.delete_pending_tasks_for(channel) + def kickoff_indexing_task(%Channel{} = source) do + Tasks.delete_pending_tasks_for(source) - if channel.index_frequency_minutes <= 0 do + if source.index_frequency_minutes <= 0 do {:ok, :should_not_index} else - channel + source |> Map.take([:id]) # Schedule this one immediately, but future ones will be on an interval |> MediaIndexingWorker.new() - |> Tasks.create_job_with_task(channel) + |> Tasks.create_job_with_task(source) |> case do # This should never return {:error, :duplicate_job} since we just deleted # any pending tasks. I'm being assertive about it so it's obvious if I'm wrong diff --git a/test/pinchflat/tasks/channel_tasks_test.exs b/test/pinchflat/tasks/channel_tasks_test.exs index d244774..d5a3f4c 100644 --- a/test/pinchflat/tasks/channel_tasks_test.exs +++ b/test/pinchflat/tasks/channel_tasks_test.exs @@ -12,32 +12,32 @@ defmodule Pinchflat.Tasks.ChannelTasksTest do test "it does not schedule a job if the interval is <= 0" do source = source_fixture(index_frequency_minutes: -1) - assert {:ok, :should_not_index} = ChannelTasks.kickoff_indexing_task(channel) + assert {:ok, :should_not_index} = ChannelTasks.kickoff_indexing_task(source) - refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => channel.id}) + refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id}) end test "it schedules a job if the interval is > 0" do source = source_fixture(index_frequency_minutes: 1) - assert {:ok, _} = ChannelTasks.kickoff_indexing_task(channel) + assert {:ok, _} = ChannelTasks.kickoff_indexing_task(source) - assert_enqueued(worker: MediaIndexingWorker, args: %{"id" => channel.id}) + assert_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id}) end test "it creates and attaches a task if the interval is > 0" do source = source_fixture(index_frequency_minutes: 1) - assert {:ok, %Task{} = task} = ChannelTasks.kickoff_indexing_task(channel) + assert {:ok, %Task{} = task} = ChannelTasks.kickoff_indexing_task(source) - assert task.channel_id == channel.id + assert task.source_id == source.id end - test "it deletes any pending tasks for the channel" do + test "it deletes any pending tasks for the source" do source = source_fixture() - task = task_fixture(channel_id: channel.id) + task = task_fixture(source_id: source.id) - assert {:ok, _} = ChannelTasks.kickoff_indexing_task(channel) + assert {:ok, _} = ChannelTasks.kickoff_indexing_task(source) assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end end