3

I have an Elixir app with two applications inside the lib/ folder:

myproject/lib/app1 myproject/lib/app2

They both have files which use Application:

myproject/lib/app1.exs myproject/lib/app2.exs

They each implement start and spawn a supervision tree.

In myproject/mix.exs I tried:

  def application do
    [
      mod: {app1, []},
           {app2, []},
      applications: [:foo, :bar]
    ]
  end

But all I get are syntax errors on the line with {app2, []}.

Is such a thing even possible? If not, what is the right way to launch separate applications with supervision trees in Elixir?

sheldonkreger
  • 858
  • 1
  • 9
  • 25

2 Answers2

2

You should add app2 as dependency of app1 and call it in applications, like:

mix.exs for app2:

  #...
  def application do
    [
      mod: {My.App2, []},
      applications: [:logger]
    ]
  end

  defp deps do
    [
      ...
    ]
  end
  #...
end

mix.exs for app1:

  def application do
    [
      mod: {My.App1, []},
      applications: [:logger, :my_app2]
    ]
  end

  defp deps do
    [
      {:my_app2, in_umbrella: true}
    ]
  end
  # ...
end

This is the case if both apps are in the same umbrella. If not, just add my_app2 as you would (from hex, path or git).

For more information on dependencies and umbrella projects, take a look @ http://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-apps.html

BurmajaM
  • 724
  • 5
  • 10
0

If it makes sense for app1 to be responsible for starting app2 then another option is to use Application.start(app2) inside app1 where appropriate.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 24 '22 at 10:51