LukasKnuth

LukasKnuth

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?

First Post! Switch mode

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement