hansonkd

hansonkd

Best way to validate runtime config before supervisor startup

HI,

I would like to know what the best way to validate runtime configuration before the supervisor starts their children. Essentially I need to check that if one environment variable is set, another is also set, if not the application should exit with 1:

defmodule MyApp.MyAppSupervisor do
  # Automatically defines child_spec/1
  use Supervisor
  alias Vapor.Provider.{File, Env}

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    providers = [
      %Env{bindings: [
              {:proxy_enable, "PROXY_ENABLE", default: false, map: fn s -> String.upcase(s) == "TRUE" end},
              {:proxy_upstream, "PROXY_UPSTREAM", default: ""},
              {:proxy_environment, "PROXY_ENVIRONMENT", default: 0, map: &String.to_integer/1},
              {:proxy_timeout, "PROXY_TIMEOUT", default: 5000, map: &String.to_integer/1},
              {:proxy_max_connections, "PROXY_MAX_CONNECTIONS", default: 100, map: &String.to_integer/1},
          ]},
      # %File{path: "config.yaml", bindings: [kafka_brokers: "kafka.brokers"]},
    ]

    # If values could not be found we raise an exception and halt the boot
    # process
    config = Vapor.load!(providers)


    Application.put_env(:my_app, MyApp.PlugProxy, [
      upstream: config[:proxy_upstream],
      environment: config[:proxy_environment],
      timeout: config[:proxy_timeout]
    ])

    children = [
    ]

    children =
      if config.proxy_enable do

        if config.proxy_upstream == "" do
          IO.puts "You must configure Proxy Upstream"
          exit {:shutdown, 1}
        end


        child_specs =
          [
            Cables.child_spec(:http_proxy_pool, config.proxy_upstream, :http, max_connections: config.proxy_max_connections),
            MyApp.ProxyEndpoint
          ]
        children ++ child_specs
      else
        children
      end

    Supervisor.init(children, strategy: :one_for_one)
  end
end

However, I get a rather ugly output and also a crash_dump.

>>> mix release
...
>>>  _build/dev/rel/myapp/bin/myapp start
You must configure Proxy Upstream
[info] Application myapp exited: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyApp.MyAppSupervisor
    ** (EXIT) shutdown
[os_mon] memory supervisor port (memsup): Erlang has closed
[os_mon] cpu supervisor port (cpu_sup): Erlang has closed
{"Kernel pid terminated",application_controller,"{application_start_failure,myapp,{{shutdown,{failed_to_start_child,'Elixir.MyApp.MyAppSupervisor',shutdown}},{'Elixir.MyApp.Application',start,[normal,[]]}}}"}
Kernel pid terminated (application_controller) ({application_start_failure,myapp,{{shutdown,{failed_to_start_child,'Elixir.MyApp.MyAppSupervisor',shutdown}},{'Elixir.MyApp

Crash dump is being written to: erl_crash.dump...done

What is the best way to not show the error message and prevent it from writing a crash dump?

I would like the output of my application to be:

>>>  _build/dev/rel/myapp/bin/myapp start
You must configure Proxy Upstream
>>> ehco $?
1

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement