From 1a17434bab34613e36e1af41754aca5b846d693e Mon Sep 17 00:00:00 2001 From: Anna Headley <845363+hackartisan@users.noreply.github.com> Date: Tue, 28 Jan 2025 13:45:02 -0500 Subject: [PATCH 1/2] Revert "Use Mix.env instead of setting up our own" This reverts commit 4351e7866479feda11d49cf651c256825e2762b3. it caused https://github.com/pulibrary/dpul-collections/issues/299 --- config/dev.exs | 3 +++ config/prod.exs | 3 +++ config/test.exs | 3 +++ lib/dpul_collections/application.ex | 2 +- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config/dev.exs b/config/dev.exs index cc62b4bd..30ad5440 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -74,6 +74,9 @@ config :dpul_collections, DpulCollectionsWeb.Endpoint, ] ] +# Set environment +config :dpul_collections, :current_env, :dev + config :dpul_collections, :basic_auth_username, "admin" config :dpul_collections, :basic_auth_password, "admin" diff --git a/config/prod.exs b/config/prod.exs index 0e1d1fb9..fe4f8a80 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -8,6 +8,9 @@ import Config config :dpul_collections, DpulCollectionsWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json" +# Set environment +config :dpul_collections, :current_env, :prod + # Enable dev routes - including mailbox preview and the dashboard. config :dpul_collections, dev_routes: true diff --git a/config/test.exs b/config/test.exs index 20f73faa..4a4d04e4 100644 --- a/config/test.exs +++ b/config/test.exs @@ -36,6 +36,9 @@ config :dpul_collections, DpulCollectionsWeb.Endpoint, # In test we don't send emails. config :dpul_collections, DpulCollections.Mailer, adapter: Swoosh.Adapters.Test +# Set environment +config :dpul_collections, :current_env, :test + # Set basic auth config :dpul_collections, :basic_auth_username, "admin" config :dpul_collections, :basic_auth_password, "test" diff --git a/lib/dpul_collections/application.ex b/lib/dpul_collections/application.ex index 24b26786..015f1eae 100644 --- a/lib/dpul_collections/application.ex +++ b/lib/dpul_collections/application.ex @@ -22,7 +22,7 @@ defmodule DpulCollections.Application do # Start to serve requests, typically the last entry DpulCollectionsWeb.Endpoint, DpulCollections.IndexMetricsTracker - ] ++ environment_children(Mix.env()) + ] ++ environment_children(Application.fetch_env!(:dpul_collections, :current_env)) # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options From 0c7bb8a9a1d2ccd8b627641c517e42750050af1e Mon Sep 17 00:00:00 2001 From: Anna Headley <845363+hackartisan@users.noreply.github.com> Date: Tue, 28 Jan 2025 15:15:24 -0500 Subject: [PATCH 2/2] Use a configured function to determine whether to start indexing closes #299 Adjusts to use best practice of not referring directly to the environment to determine code path --- config/dev.exs | 9 ++++++--- config/prod.exs | 3 --- config/runtime.exs | 12 ++++++++---- config/test.exs | 7 ++++--- lib/dpul_collections/application.ex | 20 ++++---------------- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 30ad5440..3adb4291 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -74,9 +74,6 @@ config :dpul_collections, DpulCollectionsWeb.Endpoint, ] ] -# Set environment -config :dpul_collections, :current_env, :dev - config :dpul_collections, :basic_auth_username, "admin" config :dpul_collections, :basic_auth_password, "admin" @@ -112,6 +109,12 @@ config :dpul_collections, :solr, %{ password: System.get_env("SOLR_PASSWORD") || "pass" } +# only run indexing children if the webserver is running +# wrap this in a function b/c Phoenix.Endpoint.server? is not defined at config time +config :dpul_collections, :start_indexing_pipeline?, fn -> + Phoenix.Endpoint.server?(:dpul_collections, DpulCollectionsWeb.Endpoint) +end + # Configure indexing pipeline writes config :dpul_collections, DpulCollections.IndexingPipeline, [ [ diff --git a/config/prod.exs b/config/prod.exs index fe4f8a80..0e1d1fb9 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -8,9 +8,6 @@ import Config config :dpul_collections, DpulCollectionsWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json" -# Set environment -config :dpul_collections, :current_env, :prod - # Enable dev routes - including mailbox preview and the dashboard. config :dpul_collections, dev_routes: true diff --git a/config/runtime.exs b/config/runtime.exs index 52abb5f5..7319fef2 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -120,10 +120,14 @@ if config_env() == :prod do config :dpul_collections, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY") - if System.get_env("INDEXER") do - config :dpul_collections, :start_indexing_pipeline, true - else - config :dpul_collections, :start_indexing_pipeline, false + # only run indexing children if the webserver is running + # wrap this in a function b/c the dev implementation requires it + config :dpul_collections, :start_indexing_pipeline?, fn -> + if System.get_env("INDEXER") do + true + else + false + end end # Configure basic auth diff --git a/config/test.exs b/config/test.exs index 4a4d04e4..8312ce3d 100644 --- a/config/test.exs +++ b/config/test.exs @@ -36,9 +36,6 @@ config :dpul_collections, DpulCollectionsWeb.Endpoint, # In test we don't send emails. config :dpul_collections, DpulCollections.Mailer, adapter: Swoosh.Adapters.Test -# Set environment -config :dpul_collections, :current_env, :test - # Set basic auth config :dpul_collections, :basic_auth_username, "admin" config :dpul_collections, :basic_auth_password, "test" @@ -67,6 +64,10 @@ config :dpul_collections, :solr, %{ password: "SolrRocks" } +# don't run indexing children +# wrap this in a function b/c the dev implementation requires it +config :dpul_collections, :start_indexing_pipeline?, fn -> false end + # Set this poll interval really small so it triggers in test. config :dpul_collections, :figgy_hydrator, poll_interval: 50 diff --git a/lib/dpul_collections/application.ex b/lib/dpul_collections/application.ex index 015f1eae..5fd06080 100644 --- a/lib/dpul_collections/application.ex +++ b/lib/dpul_collections/application.ex @@ -22,7 +22,7 @@ defmodule DpulCollections.Application do # Start to serve requests, typically the last entry DpulCollectionsWeb.Endpoint, DpulCollections.IndexMetricsTracker - ] ++ environment_children(Application.fetch_env!(:dpul_collections, :current_env)) + ] ++ filter_pipeline_children() # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options @@ -30,23 +30,11 @@ defmodule DpulCollections.Application do Supervisor.start_link(children, opts) end - def environment_children(:test) do - [] - end - # coveralls-ignore-start - # In development, start the indexing pipeline when the phoenix server starts. - def environment_children(:dev) do - if Phoenix.Endpoint.server?(:dpul_collections, DpulCollectionsWeb.Endpoint) do - indexing_pipeline_children() - else - [] - end - end + def filter_pipeline_children() do + fun = Application.fetch_env!(:dpul_collections, :start_indexing_pipeline?) - # In production, start the indexing pipeline if it's configured to be started - def environment_children(:prod) do - if Application.fetch_env!(:dpul_collections, :start_indexing_pipeline) == true do + if fun.() == true do indexing_pipeline_children() else []