Skip to content

Commit

Permalink
Use a configured function to determine whether to start indexing
Browse files Browse the repository at this point in the history
closes #299

Adjusts to use best practice of not referring directly to the
environment to determine code path
  • Loading branch information
hackartisan committed Jan 28, 2025
1 parent 1a17434 commit 0f61ddd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
9 changes: 6 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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, [
[
Expand Down
3 changes: 0 additions & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
21 changes: 4 additions & 17 deletions lib/dpul_collections/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,18 @@ 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
opts = [strategy: :one_for_one, name: DpulCollections.Supervisor]
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

# 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
def filter_pipeline_children() do
fun = Application.fetch_env!(:dpul_collections, :start_indexing_pipeline?)
if fun.() == true do
indexing_pipeline_children()
else
[]
Expand Down

0 comments on commit 0f61ddd

Please sign in to comment.