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.