tompave

tompave

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.

Showing Posts 1 to 6

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.

tompave

tompave OP

Ah, runtime: false might do the trick! Thank you. I could document that my package needs to be added as a dependency with that option if users want to manage the supervision tree directly.

I’ve also just noticed this in those docs:

:included_applications - specifies a list of applications that will be included in the application. It is the responsibility of the primary application to start the supervision tree of all included applications, as only the primary application will be started. A process in an included application considers itself belonging to the primary application.

Maybe this will work too. I’ll try it and report back.

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 OP

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.

derek-zhou

derek-zhou

I’ll second what @LostKobrakai has said. Provide a supervisor tree-less pure library that require the user to put your process under their supervision tree, then make another hex package, a thinly wrapped application that depend on the first package, to be compatible with your earlier release.

Now your users can simply pick one out of two ways to use your library by picking which hex package in deps.

tompave

tompave OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews