vegabook

vegabook

How to specifiy location of DETS table files for a library

I’m writing a Bloomberg terminal API library for Elixir, and this library will need to store some mutable startup configuration in a DETS table. Specifically, ticker streams that are subscribed to during a session can (optionally) be stored, reloaded the next time a session is started, and resubscribed-to.

So now I need a file location for the DETS table. I could force one on users for example ~/.lib_name/application_name_subs.dets, but it seems right to be able to allow users to override such a location.

Problem is the library guidelines says one should avoid application-level configuration.

But in the case I mention, I’m going to guess application level configuration for such an override option is okay? What are the alternatives for the above?

Marked As Solved

al2o3cr

al2o3cr

Where in the code is a session “started”? That’s where I’d start looking for a place to supply config.

For instance, here’s how Oban does it (from the installation guide):

# config/config.exs
config :my_app, Oban,
  repo: MyApp.Repo,
  plugins: [Oban.Plugins.Pruner],
  queues: [default: 10]

# lib/my_app/application.ex
def start(_type, _args) do
  children = [
    MyApp.Repo,
    {Oban, Application.fetch_env!(:my_app, Oban)}
  ]

  Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
end

Also Liked

al2o3cr

al2o3cr

The difference is small but important:

# avoid this
config :somebody_elses_library, foo: "bar"

# and in the supervisor
SomebodyElsesLibrary

(or not mentioned in the application supervisor at all and started via `extra_applications` etc)

--------------------------

# do this
config :my_app, SomebodyElsesLibrary, foo: "bar"

# and in the supervisor
{SomebodyElsesLibrary, Application.fetch_env!(:my_app, SomebodyElsesLibrary)}

A good way to check if you’re on the right path is to ask “can there be two of them?”

  • in the “don’t” case, there’s nowhere for that second config to go. Back when umbrella apps had separate config directories per-app, people would try to configure the same app differently in different child apps only to discover that all the app configs were always loaded

  • in the “do” case, all it takes is more of the same:

config :my_app, SomebodyElsesLibrary, foo: "bar"
config :my_app, SomebodyElsesLibraryWithBellsOn, foo: "bar with bells on", name: WithBellsOn

# and in the supervisor
{SomebodyElsesLibrary, Application.fetch_env!(:my_app, SomebodyElsesLibrary)},
{SomebodyElsesLibrary, Application.fetch_env!(:my_app, SomebodyElsesLibraryWithBellsOn)},
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Nope, see how it’s scoped under your app? And even that is a convention, you can just supply config directly to the child and skip the app env entirely.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement