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 Switch mode

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.

Last Post!

tompave

tompave

I appreciate the suggestion, but that sounds even more complicated and I think it would result in a very unfriendly update experience. It might be fine for a brand new package, but I don’t think it’s the right solution for me.

Where Next?

Trending in Questions Top

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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement