From 59c65820b6dc5937debef2a81393780f77000861 Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 15 Dec 2025 11:23:49 +0100 Subject: [PATCH 1/5] feat: add button to run reindex --- config/dev.exs | 5 ++ config/prod.exs | 5 ++ config/runtime.exs | 13 +++++ lib/admin/publications/publication_index.ex | 49 +++++++++++++++++++ lib/admin_web/components/layouts.ex | 1 + .../live/publication_search_index_live.ex | 46 +++++++++++++++++ lib/admin_web/router.ex | 1 + 7 files changed, 120 insertions(+) create mode 100644 lib/admin/publications/publication_index.ex create mode 100644 lib/admin_web/live/publication_search_index_live.ex diff --git a/config/dev.exs b/config/dev.exs index 2b0b9d3be..7e0714487 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -100,3 +100,8 @@ config :ex_aws, :s3, # Define the base host to use config :admin, :base_host, "localhost:3114" + +# Publication index (development defaults) +config :admin, + publication_index_url: "http://localhost:3000/api/items/collections/search/rebuild", + publication_index_header_value: "secret" diff --git a/config/prod.exs b/config/prod.exs index c785a6bfc..2d046858f 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -44,3 +44,8 @@ config :admin, :logger, [ # Runtime production configuration, including reading # of environment variables, is done on config/runtime.exs. + +# Publication index (production should provide env vars) +config :admin, + publication_index_url: System.get_env("PUBLICATION_INDEX_URL"), + publication_index_header_value: System.get_env("PUBLICATION_INDEX_HEADER_VALUE") diff --git a/config/runtime.exs b/config/runtime.exs index e1b7aa37b..231386dc4 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -148,6 +148,19 @@ if config_env() == :prod do # Config the File Items bucket name config :admin, :file_items_bucket, System.get_env("FILE_ITEMS_BUCKET_NAME", "file-items") + # Publication index runtime configuration - require in production + publication_index_url = + System.get_env("PUBLICATION_INDEX_URL") || + raise "environment variable PUBLICATION_INDEX_URL is missing. It should point to the library indexing endpoint." + + publication_index_header_value = + System.get_env("PUBLICATION_INDEX_HEADER_VALUE") || + raise "environment variable PUBLICATION_INDEX_HEADER_VALUE is missing. Provide the header value/token for reindexing the library." + + config :admin, :publications, + publication_index_url: publication_index_url, + publication_index_header_value: publication_index_header_value + config :ex_aws, :s3, region: System.get_env("AWS_REGION", "eu-central-1"), # If using custom endpoints like LocalStack or MinIO, path_style: true is often necessary. diff --git a/lib/admin/publications/publication_index.ex b/lib/admin/publications/publication_index.ex new file mode 100644 index 000000000..d856cfca5 --- /dev/null +++ b/lib/admin/publications/publication_index.ex @@ -0,0 +1,49 @@ +defmodule Admin.Publications.PublicationSearchIndex do + @moduledoc """ + Utilities to trigger a publication reindex on an external indexing endpoint. + + It expects the following application config in `:admin`: + + config :admin, :publications, + publication_index_url + publication_index_header_value + + """ + + require Logger + + @doc """ + Trigger a reindex by calling the configured endpoint with the configured header. + + Returns `{:ok, %Req.Response{}}` on success or `{:error, error_code}` on failure. + """ + def reindex do + url = Application.get_env(:admin, :publication_index_url) + + if is_nil(url) do + {:error, :missing_publication_index_url} + else + case Application.fetch_env(:admin, :publication_index_header_value) do + :error -> + {:error, :missing_publication_index_header_value} + + {:ok, header_value} -> + headers = [{"meilisearch-rebuild", header_value}] + + case Req.get(url, headers: headers) do + {:ok, resp} -> + if resp.status in 200..299 do + {:ok, resp} + else + Logger.error("PublicationSearchIndex.reindex failed: #{inspect(resp)}") + {:error, resp.status} + end + + {:error, reason} -> + Logger.error("PublicationSearchIndex.reindex failed: #{inspect(reason)}") + {:error, 500} + end + end + end + end +end diff --git a/lib/admin_web/components/layouts.ex b/lib/admin_web/components/layouts.ex index 21df18475..b4bab89e8 100644 --- a/lib/admin_web/components/layouts.ex +++ b/lib/admin_web/components/layouts.ex @@ -165,6 +165,7 @@ defmodule AdminWeb.Layouts do
  • <.link navigate={~p"/publishers"}>Apps
  • diff --git a/lib/admin_web/live/publication_search_index_live.ex b/lib/admin_web/live/publication_search_index_live.ex new file mode 100644 index 000000000..e1d7c2f34 --- /dev/null +++ b/lib/admin_web/live/publication_search_index_live.ex @@ -0,0 +1,46 @@ +defmodule AdminWeb.PublicationSearchIndexLive do + use AdminWeb, :live_view + + alias Admin.Publications.PublicationSearchIndex + + @impl true + def render(assigns) do + ~H""" + +
    +

    Index Status

    +

    You can start a reindex of the library index. This is useful if your search engine is not up to date.

    + +

    {(@status || "")}

    +
    + +
    +
    +
    + """ + end + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, status: nil, indexing: false)} + end + + @impl true + def handle_event("reindex", _params, socket) do + + case PublicationSearchIndex.reindex() do + {:ok, _resp} -> + {:noreply, assign(socket, indexing: true, status: "Reindex has started. It might take some time to complete.")} + + {:error, reason} -> + {:noreply, assign(socket, indexing: false, status: "Reindex failed with error #{inspect(reason)}")} + end + end +end diff --git a/lib/admin_web/router.ex b/lib/admin_web/router.ex index 7e0da8273..1e50b535f 100644 --- a/lib/admin_web/router.ex +++ b/lib/admin_web/router.ex @@ -78,6 +78,7 @@ defmodule AdminWeb.Router do # published_items live "/published_items/:id/unpublish", PublishedItemLive.Unpublish, :unpublish + live "/published_items/search_index", PublicationSearchIndexLive, :index # apps scope "/apps" do From 4c8d7c3681e3e5b9912c2ba4c6b3a7a3bfb3312c Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 15 Dec 2025 11:58:40 +0100 Subject: [PATCH 2/5] test: add tests for reindex --- lib/admin/publications/publication_index.ex | 27 ++++--- .../publication_search_index_test.exs | 80 +++++++++++++++++++ .../live/publication_index_live_test.exs | 56 +++++++++++++ 3 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 test/admin/publications/publication_search_index_test.exs create mode 100644 test/admin_web/live/publication_index_live_test.exs diff --git a/lib/admin/publications/publication_index.ex b/lib/admin/publications/publication_index.ex index d856cfca5..afc433b0e 100644 --- a/lib/admin/publications/publication_index.ex +++ b/lib/admin/publications/publication_index.ex @@ -12,6 +12,8 @@ defmodule Admin.Publications.PublicationSearchIndex do require Logger + defp http_client, do: Application.get_env(:admin, :publication_index_http_client, Req) + @doc """ Trigger a reindex by calling the configured endpoint with the configured header. @@ -30,19 +32,18 @@ defmodule Admin.Publications.PublicationSearchIndex do {:ok, header_value} -> headers = [{"meilisearch-rebuild", header_value}] - case Req.get(url, headers: headers) do - {:ok, resp} -> - if resp.status in 200..299 do - {:ok, resp} - else - Logger.error("PublicationSearchIndex.reindex failed: #{inspect(resp)}") - {:error, resp.status} - end - - {:error, reason} -> - Logger.error("PublicationSearchIndex.reindex failed: #{inspect(reason)}") - {:error, 500} - end + case http_client().get(url, headers: headers) do + {:ok, resp} -> + if resp.status in 200..299 do + {:ok, resp} + else + Logger.error("PublicationSearchIndex.reindex failed: #{inspect(resp)}") + {:error, resp.status} + end + {:error, reason} -> + Logger.error("PublicationSearchIndex.reindex failed: #{inspect(reason)}") + {:error, 500} + end end end end diff --git a/test/admin/publications/publication_search_index_test.exs b/test/admin/publications/publication_search_index_test.exs new file mode 100644 index 000000000..5bc6ac07a --- /dev/null +++ b/test/admin/publications/publication_search_index_test.exs @@ -0,0 +1,80 @@ +defmodule Admin.Publications.PublicationSearchIndexTest do + use ExUnit.Case, async: true + + alias Admin.Publications.PublicationSearchIndex + + setup do + # ensure we start with a clean client config + Application.delete_env(:admin, :publication_index_http_client) + :ok + end + + test "returns error when url is missing" do + Application.delete_env(:admin, :publication_index_url) + Application.put_env(:admin, :publication_index_header_value, "token") + + assert {:error, :missing_publication_index_url} = PublicationSearchIndex.reindex() + end + + test "returns error when header value is missing" do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.delete_env(:admin, :publication_index_header_value) + + assert {:error, :missing_publication_index_header_value} = PublicationSearchIndex.reindex() + end + + test "returns ok on 2xx response" do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.put_env(:admin, :publication_index_header_value, "token") + + client = + Module.concat([__MODULE__, :SuccessClient]) + + defmodule client do + def get(_url, _opts) do + resp = %Req.Response{status: 200, body: "ok"} + {:ok, resp} + end + end + + Application.put_env(:admin, :publication_index_http_client, client) + + assert {:ok, resp} = PublicationSearchIndex.reindex() + assert resp.status == 200 + end + + test "returns error on non-2xx response" do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.put_env(:admin, :publication_index_header_value, "token") + + client = Module.concat([__MODULE__, :BadResponseClient]) + + defmodule client do + def get(_url, _opts) do + resp = %Req.Response{status: 500, body: "nope"} + {:ok, resp} + end + end + + Application.put_env(:admin, :publication_index_http_client, client) + + assert {:error, 500} = PublicationSearchIndex.reindex() + end + + test "returns error tuple when client errors" do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.put_env(:admin, :publication_index_header_value, "token") + + client = Module.concat([__MODULE__, :ErrClient]) + + defmodule client do + def get(_url, _opts) do + {:error, :econnrefused} + end + end + + Application.put_env(:admin, :publication_index_http_client, client) + + assert {:error, :econnrefused} = PublicationSearchIndex.reindex() + end +end diff --git a/test/admin_web/live/publication_index_live_test.exs b/test/admin_web/live/publication_index_live_test.exs new file mode 100644 index 000000000..ef137f252 --- /dev/null +++ b/test/admin_web/live/publication_index_live_test.exs @@ -0,0 +1,56 @@ +defmodule AdminWeb.PublicationIndexLiveTest do + use AdminWeb.ConnCase, async: true + + import Phoenix.LiveViewTest + + setup %{conn: conn} do + %{conn: conn} = register_and_log_in_user(%{conn: conn}) + :ok + {:ok, conn: conn} + end + + test "shows error when reindex fails", %{conn: conn} do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.put_env(:admin, :publication_index_header_value, "token") + + client = Module.concat([__MODULE__, :FailClient]) + + defmodule client do + def get(_url, _opts), do: {:error, :econnrefused} + end + + Application.put_env(:admin, :publication_index_http_client, client) + + {:ok, view, _html} = live(conn, "/publications/reindex") + + view |> element("button", "Start Reindex") |> render_click() + + assert render(view) =~ "Reindex failed" + + end + + test "show waiting status on success", %{conn: conn} do + Application.put_env(:admin, :publication_index_url, "http://example") + Application.put_env(:admin, :publication_index_header_value, "token") + + client = Module.concat([__MODULE__, :OkClient]) + + defmodule client do + def get(_url, _opts) do + resp = %Req.Response{status: 200, body: "ok"} + {:ok, resp} + end + end + + Application.put_env(:admin, :publication_index_http_client, client) + + {:ok, view, _html} = live(conn, "/publications/reindex") + + view |> element("button", "Start Reindex") |> render_click() + + refute render(view) =~ "Reindex failed" + + assert view |> element("button[disabled]", "Start Reindex") |> has_element?() + + end +end From ca10514902b5cc10e998960da4866bfe09345363 Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 15 Dec 2025 12:03:33 +0100 Subject: [PATCH 3/5] refactor: rename search index --- .../{publication_index.ex => search_index.ex} | 6 +++--- lib/admin_web/live/publication_search_index_live.ex | 4 ++-- .../publications/publication_search_index_test.exs | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) rename lib/admin/publications/{publication_index.ex => search_index.ex} (84%) diff --git a/lib/admin/publications/publication_index.ex b/lib/admin/publications/search_index.ex similarity index 84% rename from lib/admin/publications/publication_index.ex rename to lib/admin/publications/search_index.ex index afc433b0e..27b7e51ac 100644 --- a/lib/admin/publications/publication_index.ex +++ b/lib/admin/publications/search_index.ex @@ -1,4 +1,4 @@ -defmodule Admin.Publications.PublicationSearchIndex do +defmodule Admin.Publications.SearchIndex do @moduledoc """ Utilities to trigger a publication reindex on an external indexing endpoint. @@ -37,11 +37,11 @@ defmodule Admin.Publications.PublicationSearchIndex do if resp.status in 200..299 do {:ok, resp} else - Logger.error("PublicationSearchIndex.reindex failed: #{inspect(resp)}") + Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") {:error, resp.status} end {:error, reason} -> - Logger.error("PublicationSearchIndex.reindex failed: #{inspect(reason)}") + Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") {:error, 500} end end diff --git a/lib/admin_web/live/publication_search_index_live.ex b/lib/admin_web/live/publication_search_index_live.ex index e1d7c2f34..256b610d9 100644 --- a/lib/admin_web/live/publication_search_index_live.ex +++ b/lib/admin_web/live/publication_search_index_live.ex @@ -1,7 +1,7 @@ defmodule AdminWeb.PublicationSearchIndexLive do use AdminWeb, :live_view - alias Admin.Publications.PublicationSearchIndex + alias Admin.Publications.SearchIndex @impl true def render(assigns) do @@ -35,7 +35,7 @@ defmodule AdminWeb.PublicationSearchIndexLive do @impl true def handle_event("reindex", _params, socket) do - case PublicationSearchIndex.reindex() do + case SearchIndex.reindex() do {:ok, _resp} -> {:noreply, assign(socket, indexing: true, status: "Reindex has started. It might take some time to complete.")} diff --git a/test/admin/publications/publication_search_index_test.exs b/test/admin/publications/publication_search_index_test.exs index 5bc6ac07a..83831c08e 100644 --- a/test/admin/publications/publication_search_index_test.exs +++ b/test/admin/publications/publication_search_index_test.exs @@ -1,7 +1,7 @@ defmodule Admin.Publications.PublicationSearchIndexTest do use ExUnit.Case, async: true - alias Admin.Publications.PublicationSearchIndex + alias Admin.Publications.SearchIndex setup do # ensure we start with a clean client config @@ -13,14 +13,14 @@ defmodule Admin.Publications.PublicationSearchIndexTest do Application.delete_env(:admin, :publication_index_url) Application.put_env(:admin, :publication_index_header_value, "token") - assert {:error, :missing_publication_index_url} = PublicationSearchIndex.reindex() + assert {:error, :missing_publication_index_url} = SearchIndex.reindex() end test "returns error when header value is missing" do Application.put_env(:admin, :publication_index_url, "http://example") Application.delete_env(:admin, :publication_index_header_value) - assert {:error, :missing_publication_index_header_value} = PublicationSearchIndex.reindex() + assert {:error, :missing_publication_index_header_value} = SearchIndex.reindex() end test "returns ok on 2xx response" do @@ -39,7 +39,7 @@ defmodule Admin.Publications.PublicationSearchIndexTest do Application.put_env(:admin, :publication_index_http_client, client) - assert {:ok, resp} = PublicationSearchIndex.reindex() + assert {:ok, resp} = SearchIndex.reindex() assert resp.status == 200 end @@ -58,7 +58,7 @@ defmodule Admin.Publications.PublicationSearchIndexTest do Application.put_env(:admin, :publication_index_http_client, client) - assert {:error, 500} = PublicationSearchIndex.reindex() + assert {:error, 500} = SearchIndex.reindex() end test "returns error tuple when client errors" do @@ -75,6 +75,6 @@ defmodule Admin.Publications.PublicationSearchIndexTest do Application.put_env(:admin, :publication_index_http_client, client) - assert {:error, :econnrefused} = PublicationSearchIndex.reindex() + assert {:error, :econnrefused} = SearchIndex.reindex() end end From 57f6145c17a54df3ce610852b7147b50e9e1f2ed Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 15 Dec 2025 12:15:21 +0100 Subject: [PATCH 4/5] refactor: fix format --- lib/admin/publications/search_index.ex | 5 ++- .../live/publication_search_index_live.ex | 39 +++++++++++++++---- .../live/publication_index_live_test.exs | 4 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/admin/publications/search_index.ex b/lib/admin/publications/search_index.ex index 27b7e51ac..367c47d14 100644 --- a/lib/admin/publications/search_index.ex +++ b/lib/admin/publications/search_index.ex @@ -40,9 +40,10 @@ defmodule Admin.Publications.SearchIndex do Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") {:error, resp.status} end + {:error, reason} -> - Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") - {:error, 500} + Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") + {:error, 500} end end end diff --git a/lib/admin_web/live/publication_search_index_live.ex b/lib/admin_web/live/publication_search_index_live.ex index 256b610d9..c93305dbe 100644 --- a/lib/admin_web/live/publication_search_index_live.ex +++ b/lib/admin_web/live/publication_search_index_live.ex @@ -9,16 +9,32 @@ defmodule AdminWeb.PublicationSearchIndexLive do

    Index Status

    -

    You can start a reindex of the library index. This is useful if your search engine is not up to date.

    +

    + You can start a reindex of the library index. This is useful if your search engine is not up to date. +

    -

    {(@status || "")}

    +

    {@status || ""}

    -
    @@ -34,13 +50,20 @@ defmodule AdminWeb.PublicationSearchIndexLive do @impl true def handle_event("reindex", _params, socket) do - case SearchIndex.reindex() do {:ok, _resp} -> - {:noreply, assign(socket, indexing: true, status: "Reindex has started. It might take some time to complete.")} + {:noreply, + assign(socket, + indexing: true, + status: "Reindex has started. It might take some time to complete." + )} {:error, reason} -> - {:noreply, assign(socket, indexing: false, status: "Reindex failed with error #{inspect(reason)}")} + {:noreply, + assign(socket, + indexing: false, + status: "Reindex failed with error #{inspect(reason)}" + )} end end end diff --git a/test/admin_web/live/publication_index_live_test.exs b/test/admin_web/live/publication_index_live_test.exs index ef137f252..b189b0773 100644 --- a/test/admin_web/live/publication_index_live_test.exs +++ b/test/admin_web/live/publication_index_live_test.exs @@ -26,10 +26,9 @@ defmodule AdminWeb.PublicationIndexLiveTest do view |> element("button", "Start Reindex") |> render_click() assert render(view) =~ "Reindex failed" - end - test "show waiting status on success", %{conn: conn} do + test "show waitingstatus on success", %{conn: conn} do Application.put_env(:admin, :publication_index_url, "http://example") Application.put_env(:admin, :publication_index_header_value, "token") @@ -51,6 +50,5 @@ defmodule AdminWeb.PublicationIndexLiveTest do refute render(view) =~ "Reindex failed" assert view |> element("button[disabled]", "Start Reindex") |> has_element?() - end end From 5a1139e7d5ef92423828fc74c5eb1ededf8fed0c Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 18 Dec 2025 16:54:53 +0100 Subject: [PATCH 5/5] refactor: apply PR requested changes --- config/dev.exs | 7 +- config/prod.exs | 4 +- config/runtime.exs | 14 +--- lib/admin/publications/search_index.ex | 73 +++++++++++++------ lib/admin_web/components/layouts.ex | 3 + .../live/publication_search_index_live.ex | 24 ++---- .../publication_search_index_test.exs | 14 ++-- .../live/publication_index_live_test.exs | 6 +- 8 files changed, 78 insertions(+), 67 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 7e0714487..e022eaad1 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -101,7 +101,8 @@ config :ex_aws, :s3, # Define the base host to use config :admin, :base_host, "localhost:3114" +# Define the backend host to use +config :admin, :backend_host, "localhost:3000" + # Publication index (development defaults) -config :admin, - publication_index_url: "http://localhost:3000/api/items/collections/search/rebuild", - publication_index_header_value: "secret" +config :admin, :publication_reindex_headers, [{"meilisearch-rebuild", "secret"}] diff --git a/config/prod.exs b/config/prod.exs index 2d046858f..c520d2b65 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -45,7 +45,5 @@ config :admin, :logger, [ # Runtime production configuration, including reading # of environment variables, is done on config/runtime.exs. -# Publication index (production should provide env vars) config :admin, - publication_index_url: System.get_env("PUBLICATION_INDEX_URL"), - publication_index_header_value: System.get_env("PUBLICATION_INDEX_HEADER_VALUE") + publication_reindex_headers: {"meilisearch-rebuild", System.get_env("PUBLICATION_INDEX_HEADER_VALUE")} diff --git a/config/runtime.exs b/config/runtime.exs index 231386dc4..6e62bf599 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -144,23 +144,11 @@ if config_env() == :prod do end config :admin, base_host: base_host + config :admin, backend_host: base_host # Config the File Items bucket name config :admin, :file_items_bucket, System.get_env("FILE_ITEMS_BUCKET_NAME", "file-items") - # Publication index runtime configuration - require in production - publication_index_url = - System.get_env("PUBLICATION_INDEX_URL") || - raise "environment variable PUBLICATION_INDEX_URL is missing. It should point to the library indexing endpoint." - - publication_index_header_value = - System.get_env("PUBLICATION_INDEX_HEADER_VALUE") || - raise "environment variable PUBLICATION_INDEX_HEADER_VALUE is missing. Provide the header value/token for reindexing the library." - - config :admin, :publications, - publication_index_url: publication_index_url, - publication_index_header_value: publication_index_header_value - config :ex_aws, :s3, region: System.get_env("AWS_REGION", "eu-central-1"), # If using custom endpoints like LocalStack or MinIO, path_style: true is often necessary. diff --git a/lib/admin/publications/search_index.ex b/lib/admin/publications/search_index.ex index 367c47d14..30393f07d 100644 --- a/lib/admin/publications/search_index.ex +++ b/lib/admin/publications/search_index.ex @@ -20,32 +20,57 @@ defmodule Admin.Publications.SearchIndex do Returns `{:ok, %Req.Response{}}` on success or `{:error, error_code}` on failure. """ def reindex do - url = Application.get_env(:admin, :publication_index_url) - - if is_nil(url) do - {:error, :missing_publication_index_url} - else - case Application.fetch_env(:admin, :publication_index_header_value) do - :error -> - {:error, :missing_publication_index_header_value} - - {:ok, header_value} -> - headers = [{"meilisearch-rebuild", header_value}] - - case http_client().get(url, headers: headers) do - {:ok, resp} -> - if resp.status in 200..299 do - {:ok, resp} - else - Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") - {:error, resp.status} - end - - {:error, reason} -> - Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") - {:error, 500} + with {:ok, client, url, headers} <- build_reindex_request() do + + req = client.new(method: :get, url: url, headers: headers) + + IO.puts("Reindex request: #{inspect(req)}") + + case client.request(req) do + %Req.Response{} = resp -> + if resp.status in 200..299 do + {:ok, resp} + else + Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") + {:error, resp.status} end + + {:ok, %Req.Response{} = resp} -> + if resp.status in 200..299 do + {:ok, resp} + else + Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") + {:error, resp.status} + end + + {:error, reason} -> + Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") + {:error, 500} + + other -> + Logger.error("SearchIndex.reindex unexpected response: #{inspect(other)}") + {:error, :unexpected_response} end end end + + @doc false + # Build the HTTP client, url, and headers for the reindex request. + defp build_reindex_request do + + case Application.get_env(:admin, :backend_host) do + nil -> + {:error, :missing_publication_index_url} + + url -> + case Application.get_env(:admin, :publication_reindex_headers) do + :error -> + {:error, :missing_publication_index_header_value} + + headers_value -> + client = http_client() + {:ok, client, "http://#{url}/items/collections/search/rebuild", headers_value} + end + end + end end diff --git a/lib/admin_web/components/layouts.ex b/lib/admin_web/components/layouts.ex index b4bab89e8..df17863d4 100644 --- a/lib/admin_web/components/layouts.ex +++ b/lib/admin_web/components/layouts.ex @@ -213,6 +213,9 @@ defmodule AdminWeb.Layouts do
  • <.link navigate={~p"/published_items/featured"}>Featured
  • +
  • + <.link navigate={~p"/published_items/search_index"}>Search Index +
  • diff --git a/lib/admin_web/live/publication_search_index_live.ex b/lib/admin_web/live/publication_search_index_live.ex index c93305dbe..8b25d2a8f 100644 --- a/lib/admin_web/live/publication_search_index_live.ex +++ b/lib/admin_web/live/publication_search_index_live.ex @@ -8,24 +8,14 @@ defmodule AdminWeb.PublicationSearchIndexLive do ~H"""
    -

    Index Status

    -

    - You can start a reindex of the library index. This is useful if your search engine is not up to date. -

    + <.header> + Index Status + <:subtitle> + You can start a reindex of the library index. This is useful if your search engine is not up to date. + +

    {@status || ""}

    diff --git a/test/admin/publications/publication_search_index_test.exs b/test/admin/publications/publication_search_index_test.exs index 83831c08e..14254a2ef 100644 --- a/test/admin/publications/publication_search_index_test.exs +++ b/test/admin/publications/publication_search_index_test.exs @@ -31,7 +31,9 @@ defmodule Admin.Publications.PublicationSearchIndexTest do Module.concat([__MODULE__, :SuccessClient]) defmodule client do - def get(_url, _opts) do + def new(_opts), do: :req_request + + def request(_req) do resp = %Req.Response{status: 200, body: "ok"} {:ok, resp} end @@ -50,7 +52,9 @@ defmodule Admin.Publications.PublicationSearchIndexTest do client = Module.concat([__MODULE__, :BadResponseClient]) defmodule client do - def get(_url, _opts) do + def new(_opts), do: :req_request + + def request(_req) do resp = %Req.Response{status: 500, body: "nope"} {:ok, resp} end @@ -68,9 +72,9 @@ defmodule Admin.Publications.PublicationSearchIndexTest do client = Module.concat([__MODULE__, :ErrClient]) defmodule client do - def get(_url, _opts) do - {:error, :econnrefused} - end + def new(_opts), do: :req_request + + def request(_req), do: {:error, :econnrefused} end Application.put_env(:admin, :publication_index_http_client, client) diff --git a/test/admin_web/live/publication_index_live_test.exs b/test/admin_web/live/publication_index_live_test.exs index b189b0773..fd1d97d4d 100644 --- a/test/admin_web/live/publication_index_live_test.exs +++ b/test/admin_web/live/publication_index_live_test.exs @@ -16,7 +16,8 @@ defmodule AdminWeb.PublicationIndexLiveTest do client = Module.concat([__MODULE__, :FailClient]) defmodule client do - def get(_url, _opts), do: {:error, :econnrefused} + def new(_opts), do: :req_request + def request(_req), do: {:error, :econnrefused} end Application.put_env(:admin, :publication_index_http_client, client) @@ -35,7 +36,8 @@ defmodule AdminWeb.PublicationIndexLiveTest do client = Module.concat([__MODULE__, :OkClient]) defmodule client do - def get(_url, _opts) do + def new(_opts), do: :req_request + def request(_req) do resp = %Req.Response{status: 200, body: "ok"} {:ok, resp} end