LukasKnuth

LukasKnuth

Validate runtime config on application start

In my config/runtime.exs I have the following:

timezone = System.get_env("TZ") || "Etc/UTC"

unless Timex.is_valid_timezone?(timezone) do
  raise """
  The timezone specified in the TZ environment variable is invalid.
  """
end

config :my_app, MyApp, timezone: timezone

My intention is that a valid Timezone will work as expected while an invalid one (such as Europe/Börlin) crashes the application early.In the rest of the App, I can then assume that the user-supplied timezone is always valid.

This works as expected in dev, on prod however, this results in a crash at runtime:

ERROR! Config provider Config.Reader failed with:
** (ArgumentError) errors were found at the given arguments:
  * 1st argument: the table identifier does not refer to an existing ETS table
    (stdlib 6.2) :ets.lookup(:tzdata_current_release, :release_version)
    (tzdata 1.1.3) lib/tzdata/release_reader.ex:74: Tzdata.ReleaseReader.current_release
    (tzdata 1.1.3) lib/tzdata/release_reader.ex:17: Tzdata.ReleaseReader.simple_lookup/1
    (tzdata 1.1.3) lib/tzdata/release_reader.ex:9: Tzdata.ReleaseReader.zone_and_link_li
    (tzdata 1.1.3) lib/tzdata.ex:61: Tzdata.zone_exists?/1
    (timex 3.7.13) lib/timezone/timezone.ex:230: Timex.Timezone.name_of/1
    (timex 3.7.13) lib/timex.ex:857: Timex.is_valid_timezone?/1
    /app/releases/0.1.0/runtime.exs:13: (file)

I’m assuming this happens because the Timex application isn’t started yet when the runtime configuration is evaluated.

So instead, I now have this workaround that achieves the same thing:

defmodule MyApp.Application do
  use Application

  @impl true
  def start(_type, _args) do
    with :ok <- validate_timezone_config() do
      # shortened
      Supervisor.start_link(children, opts)
    end
  end

  defp validate_timezone_config do
    timezone = Briefly.user_timezone()

    case Timex.is_valid_timezone?(timezone) do
      true -> :ok
      false -> {:error, "Configured timezone '#{timezone}' is invalid"}
    end
  end
end

If an invalid timezone is supplied, the app crashes early:

11:39:02.487 [notice] Application briefly exited: Briefly.Application.start(:normal, []) returned an error: "Configured timezone 'Europe/Börlin' is invalid"
** (exit) :terminating
    (kernel 10.2.1) application_controller.erl:511: :application_controller.call/2
    (kernel 10.2.1) application.erl:367: :application."-ensure_all_started/3-lc$^0/1-0-"/1
    (kernel 10.2.1) application.erl:367: :application.ensure_all_started/3
    (mix 1.18.2) lib/mix/tasks/app.start.ex:72: Mix.Tasks.App.Start.start/3
    (mix 1.18.2) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.18.2) lib/mix/tasks/run.ex:129: Mix.Tasks.Run.run/5
    (mix 1.18.2) lib/mix/tasks/run.ex:85: Mix.Tasks.Run.run/1
    (mix 1.18.2) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5

Is this considered an anti-pattern? Is there a more appropriate/easier way to validate runtime configuration during startup?

Most Liked

LostKobrakai

LostKobrakai

You should be able to do Application.ensure_all_started in the runtime config, but that would also mean you cannot configure the started application and its dependencies anymore in runtime.exs. Validating in the application start callback is free of that chicken-egg type problem of what runs first.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement