Added fast indexing to source changeset operations
This commit is contained in:
parent
45329f7e99
commit
d43cc78060
3 changed files with 80 additions and 3 deletions
|
|
@ -46,6 +46,7 @@ defmodule Pinchflat.Sources do
|
|||
def create_source(attrs) do
|
||||
%Source{}
|
||||
|> change_source_from_url(attrs)
|
||||
|> maybe_change_indexing_frequency()
|
||||
|> commit_and_handle_tasks()
|
||||
end
|
||||
|
||||
|
|
@ -62,6 +63,7 @@ defmodule Pinchflat.Sources do
|
|||
def update_source(%Source{} = source, attrs) do
|
||||
source
|
||||
|> change_source_from_url(attrs)
|
||||
|> maybe_change_indexing_frequency()
|
||||
|> commit_and_handle_tasks()
|
||||
end
|
||||
|
||||
|
|
@ -151,6 +153,22 @@ defmodule Pinchflat.Sources do
|
|||
change_source(source, Map.merge(changes, collection_changes))
|
||||
end
|
||||
|
||||
defp maybe_change_indexing_frequency(changeset) do
|
||||
fast_index = Ecto.Changeset.get_field(changeset, :fast_index)
|
||||
|
||||
case {changeset.changes, fast_index} do
|
||||
{%{index_frequency_minutes: _}, true} ->
|
||||
Ecto.Changeset.put_change(
|
||||
changeset,
|
||||
:index_frequency_minutes,
|
||||
Source.index_frequency_when_fast_indexing()
|
||||
)
|
||||
|
||||
_ ->
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
defp commit_and_handle_tasks(changeset) do
|
||||
case Repo.insert_or_update(changeset) do
|
||||
{:ok, %Source{} = source} ->
|
||||
|
|
@ -189,9 +207,15 @@ defmodule Pinchflat.Sources do
|
|||
# indexing frequency has been changed and is now greater than 0
|
||||
%{__meta__: %{state: :loaded}} ->
|
||||
case changeset.changes do
|
||||
%{index_frequency_minutes: mins} when mins > 0 -> SourceTasks.kickoff_indexing_task(source)
|
||||
%{index_frequency_minutes: _} -> Tasks.delete_pending_tasks_for(source, "MediaCollectionIndexingWorker")
|
||||
_ -> :ok
|
||||
%{index_frequency_minutes: mins} when mins > 0 ->
|
||||
SourceTasks.kickoff_indexing_task(source)
|
||||
|
||||
%{index_frequency_minutes: _} ->
|
||||
# TODO: delete the recurring RSS task (when I get there)
|
||||
Tasks.delete_pending_tasks_for(source, "MediaCollectionIndexingWorker")
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -65,4 +65,9 @@ defmodule Pinchflat.Sources.Source do
|
|||
|> validate_required(@required_fields)
|
||||
|> unique_constraint([:collection_id, :media_profile_id])
|
||||
end
|
||||
|
||||
@doc false
|
||||
def index_frequency_when_fast_indexing do
|
||||
60 * 24 * 30
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -182,6 +182,36 @@ defmodule Pinchflat.SourcesTest do
|
|||
|
||||
assert_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id})
|
||||
end
|
||||
|
||||
test "fast_index forces the index frequency to be a default value" do
|
||||
expect(YtDlpRunnerMock, :run, &channel_mock/3)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
fast_index: true,
|
||||
index_frequency_minutes: 0
|
||||
}
|
||||
|
||||
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
|
||||
|
||||
assert source.index_frequency_minutes == Source.index_frequency_when_fast_indexing()
|
||||
end
|
||||
|
||||
test "disabling fast index will not change the index frequency" do
|
||||
expect(YtDlpRunnerMock, :run, &channel_mock/3)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
fast_index: false,
|
||||
index_frequency_minutes: 0
|
||||
}
|
||||
|
||||
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
|
||||
|
||||
assert source.index_frequency_minutes == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "update_source/2" do
|
||||
|
|
@ -293,6 +323,24 @@ defmodule Pinchflat.SourcesTest do
|
|||
|
||||
assert source == Sources.get_source!(source.id)
|
||||
end
|
||||
|
||||
test "fast_index forces the index frequency to be a default value" do
|
||||
source = source_fixture(%{fast_index: true})
|
||||
update_attrs = %{index_frequency_minutes: 0}
|
||||
|
||||
assert {:ok, source} = Sources.update_source(source, update_attrs)
|
||||
|
||||
assert source.index_frequency_minutes == Source.index_frequency_when_fast_indexing()
|
||||
end
|
||||
|
||||
test "disabling fast index will not change the index frequency" do
|
||||
source = source_fixture(%{fast_index: false})
|
||||
update_attrs = %{index_frequency_minutes: 0}
|
||||
|
||||
assert {:ok, source} = Sources.update_source(source, update_attrs)
|
||||
|
||||
assert source.index_frequency_minutes == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete_source/2" do
|
||||
|
|
|
|||
Loading…
Reference in a new issue