fix(portal): Properly check background jobs (#8986)

The `background_jobs_enabled` config in an ENV var that needs to be set
for a specific configuration key. It's not set on the top-level
`:domain` config by default.

Instead, it's used to enable / disable specific modules to start by the
application's Supervisor.

The `Domain.Events.ReplicationConnection` module is updated in this PR
to follow this convention.
This commit is contained in:
Jamil
2025-05-01 09:32:43 -07:00
committed by GitHub
parent 593a288f64
commit 299fbcd096
3 changed files with 40 additions and 42 deletions

View File

@@ -41,41 +41,9 @@ defmodule Domain.Application do
# Observability
Domain.Telemetry
] ++ background_children()
end
defp background_children do
if background_jobs_enabled?() do
[
# Job system
{Oban, Application.fetch_env!(:domain, Oban)},
# WAL replication
replication_child_spec()
]
else
[]
end
end
defp replication_child_spec do
{connection_opts, config} =
Application.fetch_env!(:domain, Domain.Events.ReplicationConnection)
|> Keyword.pop(:connection_opts)
init_state = %{
connection_opts: connection_opts,
instance: struct(Domain.Events.ReplicationConnection, config)
}
%{
id: Domain.Events.ReplicationConnection,
start: {Domain.Events.ReplicationConnection, :start_link, [init_state]},
restart: :transient,
# Allow up to 240 restarts in 20 minutes - covers duration of a deploy
max_restarts: 240,
max_seconds: 1200
}
] ++
oban() ++
replication()
end
defp configure_logger do
@@ -109,12 +77,40 @@ defmodule Domain.Application do
end
end
defp background_jobs_enabled? do
# The web and api applications also start Domain's supervision tree so that they may
# use the same database connection pool and other shared resources. We want to restrict
# some children to only run in the domain node.
# For now, we use BACKGROUND_JOBS_ENABLED to determine if we are running in the domain node.
# We could also use the node's name, but this doesn't work as well in dev/test environments.
Application.get_env(:domain, :background_jobs_enabled, false)
# TODO: Configure Oban workers to only run on domain nodes
defp oban do
[{Oban, Application.fetch_env!(:domain, Oban)}]
end
defp replication do
config = Application.fetch_env!(:domain, Domain.Events.ReplicationConnection)
if config[:enabled] do
[
replication_child_spec()
]
else
[]
end
end
defp replication_child_spec do
{connection_opts, config} =
Application.fetch_env!(:domain, Domain.Events.ReplicationConnection)
|> Keyword.pop(:connection_opts)
init_state = %{
connection_opts: connection_opts,
instance: struct(Domain.Events.ReplicationConnection, config)
}
%{
id: Domain.Events.ReplicationConnection,
start: {Domain.Events.ReplicationConnection, :start_link, [init_state]},
restart: :transient,
# Allow up to 240 restarts in 20 minutes - covers duration of a deploy
max_restarts: 240,
max_seconds: 1200
}
end
end

View File

@@ -31,6 +31,7 @@ config :domain, Domain.Repo,
start_apps_before_migration: [:ssl, :logger_json]
config :domain, Domain.Events.ReplicationConnection,
enabled: true,
connection_opts: [
hostname: "localhost",
port: 5432,

View File

@@ -28,6 +28,7 @@ if config_env() == :prod do
)
config :domain, Domain.Events.ReplicationConnection,
enabled: compile_config!(:background_jobs_enabled),
connection_opts: [
hostname: compile_config!(:database_host),
port: compile_config!(:database_port),