How to Update Elixir Project Configuration with TOML in Runtime.
Distributed Systems Development A-Z Guide.
Give us a message if you’re interested in Blockchain and FinTech software development or just say Hi at Pharos Production Inc.
Or follow us on Youtube to know more about Software Architecture, Distributed Systems, Blockchain, High-load Systems, Microservices, and Enterprise Design Patterns.
What is TOML? TOML is a configuration file format language that is intended to be minimal and easy to read. TOML stands for “Tom’s Obvious, Minimal Language,” which refers to the creator Tom Preston-Werner. TOML is cool and you can find more about syntax in TOML repo. But here we will explain how to integrate TOML configurations into Elixir project, release it with a production environment settings and reload configuration without stopping the server.
We have created a new project with usual.
$> mix new tutorial_toml
Let’s add TOML library and Distillery to make a release and use TOML.
Let’s configure release settings. In the root of your project create rel directory. We need only 2 files for the basic release functionality. Config.exs contains release settings and config_prod.toml overrides configuration of the Elixir project.
In config directory of the project, we have 3 files — common configuration, dev env configuration, and prod configuration.
We’re interested in prod.exs. Here we have a single value — demo_value. As you will see later, this value is completely overridden by TOML.
TOML file in rel contains values that reflect configuration settings. Here you can see app name in brackets, it’s the same as atom name of the app. And a key with a corresponding value, same as we have in prod config of the project. But the difference is in value itself to show that all values from project config are completely overridden.
To check the demo value we will use just one function from the root module of the project.
Now, let’s look at release configuration file rel->config.exs. In the first place, we generate Erlang magic cookie. We will not explain what is it in this tutorial, because its related to distributed Erlang cluster configuration.
Next is a release overall settings. Here we can find a default release selector and automatically fetched environment. We use tutorial_toml name for the default release here.
Next thing is release config. We set version and set of applications. In our project, there is only one application that should be included in the release.
The last part of the release config file is environment settings. Dev_mode, include_erts, include_system_libs, include_src, cookie — are settings that don’t related to TOML. We’re interested in next settings:
overlays — we copy existing toml file into etc directory of the release. Also we can ref link it instead of copying by using :link atom.
To build a release from the root folder of the project execute statement below. Distillery will do all things for us including TOML transformation into erlang’s sys.config. Here Mix environment is set to prod and release configuration is tutorial_prod.
How to Update Elixir Project Configuration with TOML in Runtime.
Distributed & High-Load Systems Development.
What is TOML? TOML is a configuration file format language that is intended to be minimal and easy to read. TOML stands for “Tom’s Obvious, Minimal Language,” which refers to the creator Tom Preston-Werner. TOML is cool and you can find more about syntax in TOML repo. But here we will explain how to integrate TOML configurations into Elixir project, release it with a production environment settings and reload configuration without stopping the server.
We have created a new project with usual.
$> mix new tutorial_toml
Let’s add TOML library and Distillery to make a release and use TOML.
Let’s configure release settings. In the root of your project create rel directory. We need only 2 files for the basic release functionality. Config.exs contains release settings and config_prod.toml overrides configuration of the Elixir project.
In config directory of the project, we have 3 files — common configuration, dev env configuration, and prod configuration.
We’re interested in prod.exs. Here we have a single value — demo_value. As you will see later, this value is completely overridden by TOML.
TOML file in rel contains values that reflect configuration settings. Here you can see app name in brackets, it’s the same as atom name of the app. And a key with a corresponding value, same as we have in prod config of the project. But the difference is in value itself to show that all values from project config are completely overridden.
To check the demo value we will use just one function from the root module of the project.
Now, let’s look at release configuration file rel->config.exs. In the first place, we generate Erlang magic cookie. We will not explain what is it in this tutorial, because its related to distributed Erlang cluster configuration.
Next is a release overall settings. Here we can find a default release selector and automatically fetched environment. We use tutorial_toml name for the default release here.
Next thing is release config. We set version and set of applications. In our project, there is only one application that should be included in the release.
The last part of the release config file is environment settings. Dev_mode, include_erts, include_system_libs, include_src, cookie, overlay_vars — are settings that don’t related to TOML. We’re interested in next settings:
overlays — we copy the existing TOML file into etc/ directory of the release. Also, we can ref link it instead of copying by using :link atom.
config_providers — we use to make Distillery read configuration from etc/ directory of the release. Transforms options here can be used to transform values from TOML to Erlang’s terms.
To build a release from the root folder of the project execute statement below. Distillery will do all things for us including TOML transformation into erlang’s sys.config. Here Mix environment is set to prod and release configuration is tutorial_prod.
After this, you should see completion information and a description of some of the tasks available inside the release.
Run app with the attached console by executing
sh _build/prod/rel/tutorial_toml/bin/tutorial_toml console
Now execute the function from the root module. In the result, we get a value from TOML config file. You can also find TOML config file in the release directory in etc/config_prod.toml.
Let’s change the value to something new.
To update value you don’t need to shut down the running app. Execute in the second console from the root directory of the project and wait while change will be applied.
sh _build/prod/rel/tutorial_toml/bin/tutorial_toml reload_config
Now let’s check again configuration value.
You can find an example project on Github
Thanks for reading!