Starting child processes depending on the environment

almirsarajcic

almirsarajcic

Created 4 months ago

Sometimes, you don’t want to run every part of your Elixir application in all Mix environments.
For example, you might want to skip some background jobs when running in the test environment.

To accomplish that, you can conditionally start child processes in your application.

def start(_type, _args) do
  children =
    [
      # ...
    ] ++ more_children()

  # ...
end

defp more_children(env \\ Application.get_env(:my_app, :env))

defp more_children(:test), do: []

defp more_children(_env) do
  [
    MyApp.Scheduler
  ]
end

Note: you can’t use Mix.env() here because Mix is unavailable in Elixir releases.
Check out https://elixirdrops.net/d/2KpcFEAG