Hi,
I have a Phoenix app that runs some background tasks managed by a supervisor. This is set up in the application.ex
start function like so:
def start(_type, _args) do
Logger.add_backend(Sentry.LoggerBackend)
children = [
# Start the Ecto repository
MyApp.Repo,
# Start the Telemetry supervisor
MyAppWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: MyApp.PubSub},
# Start the Endpoint (http/https)
MyAppWeb.Endpoint,
MyAppWeb.Supervisor # here's where the background tasks are managed
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
This works great. However, I’ve since created a new app on Fly representing a staging environment. In staging, I don’t want these background tasks to run-- they involve requests to external APIs, and running them here would make me hit my rate limit twice as fast.
I figured I could prevent these tasks from running in the staging app by setting an environment variable (ENV
) and doing something like the following:
def start(_type, _args) do
Logger.add_backend(Sentry.LoggerBackend)
children = [
# Start the Ecto repository
MyApp.Repo,
# Start the Telemetry supervisor
MyAppWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: MyApp.PubSub},
# Start the Endpoint (http/https)
MyAppWeb.Endpoint
]
children = case System.get_env("ENV") do
"staging" -> children
_ -> children ++ [MyAppWeb.Supervisor] # include supervisor if not staging
end
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
This works locally, and the deployment goes through fine. That said, when I deploy this change the app stops working and the site hangs. I haven’t seen anything in the logs to suggest any issues have occurred. I’m not sure why everything breaks with this change, but from trial and error this modification must be the problem.
For what it’s worth, I also tried connecting to the instance via fly ssh console
and got an error saying host unavailable: host was not found in DNS
.
Am I doing something wrong here/is there another way I should conditionally start a Supervisor? Any suggestions are much appreciated.