tompave

tompave

Mix project: how to control whether the application is started based on config

TL;DR:

I want to customize whether a Mix project will start its application automatically based on user-provided configuration.

More details

I maintain an Elixir package that is meant to be used as a dependency in host applications. My package is configured to start an OTP application that encapsulates its own supervision tree.

I’m working on providing more control over the start behaviour of the package’s OTP application.

At the moment it’s a “batteries included” package that can work with zero config. Just add it as a dependency to your Mix project, and it works.

I would like to change that to provide this API:

First, tell the package to not start its OTP application:

# An application's config file.
use Config
config :fun_with_flags, start_application: false

Then, manually put the package’s supervisor module in a supervision tree of your choice, for example:

  def start(_type, _args) do
    children = [
      HelloWorld.Repo,
      {Phoenix.PubSub, name: HelloWorld.PubSub},
      HelloWorldWeb.Endpoint,
+     FunWithFlags.Supervisor
    ]

    opts = [strategy: :one_for_one, name: HelloWorld.Supervisor]
    Supervisor.start_link(children, opts)
  end

I’m trying to achieve this by doing something like this:

defmodule FunWithFlags.Application do
  use Application

  def start(_type, _args) do
+   if application_should_start? do
      FunWithFlags.Supervisor.start_link(nil)
+   end
  end

+ def application_should_start? do
+   Application.get_env(:fun_with_flags, :start_application, true)
+ end
end

That however fails with this error:

** (Mix) Could not start application fun_with_flags: FunWithFlags.Application.start(:normal, []) returned a bad value: nil

I’ve looked at the docs for the Application.start/2 callback and I can’t see any return value that would communicate “nope, nothing to do and it’s ok”. So I suppose that that’s not the right way to do it.

I’ve thought that maybe I can provide the config API described above, and then tell users of my package to tell their host applications to not start the dependency automatically, but that also doesn’t seem possible. At least, I’ve looked at the docs for the application/0 function in the Mixfile, but I can’t see anything useful there.

I suppose that a workaround could be this:

  def start(_type, _args) do
+   if application_should_start? do
      FunWithFlags.Supervisor.start_link(nil)
+   else
+     # start a bogus process that does nothing
+   end
  end

But I wonder if there is a simpler and more idiomatic way to accomplish what I need that I’m just missing.

Optional extra context

The reason I’m trying to do this is to address an occasional issue. More specifically, sometimes there is a race condition when the package’s OTP application starts much faster than some of the host application’s processes. (issue on GitHub)

This is unfortunate, but my package depends (with some configurations) on some injected processes (e.g. a Phoenix.PubSub process), and it’s a bit difficult to change that. Or, rather, I find it a less elegant solution so I would prefer to implement the API described above, if possible.

Marked As Solved

kip

kip

ex_cldr Core Team

I have a similar approach in my ex_money library where the exchange rate service can run it its own supervisor tree (batteries included) or be configured into the consuming app’s supervision tree, or not run at all.

The application code might give you some ideas.

You can also have your consumers configure your library as runtime: false in deps. I document this here - it prevents the host application from starting your library’s application at its application start.

Also Liked

LostKobrakai

LostKobrakai

If that’s the case I’m wondering why you don’t switch to userland supervision completely? As a user I prefer one way of using a library – even if more involved for some use-cases – over two ways of using it, maybe even with similar but slightly different means of setting both ways up.

tompave

tompave

I considered it, and I think that’s something I’ll do for v2.0.

For now, I’m forcing myself to not introduce breaking changes. I would like this to be a simple update that provides this extra capability without forcing users of the package to change their setup.

That’s also why I’m trying to research the most idiomatic way to accomplish this, rather than throwing it over the wall with a clunky solution.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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

Other popular topics 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
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 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
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

We're in Beta

About us Mission Statement