Fl4m3Ph03n1x

Fl4m3Ph03n1x

Start Application dependencies before main Applciation is started

Background

I am trying to ensure an application’s dependency is started before the main application is, whilst also having said dependency as part of the Supervision Tree (as a child).

Problem

Currently I have this in application.ex:

    children = [
      Telemetry,
      {Phoenix.PubSub, name: PubSub},
      Endpoint,
      MyDependency
    ]

    {:ok, things} = MyDependency.list_things()

    IO.inpsect(things, label: "THINGS")
    opts = [strategy: :one_for_one, name: WebInterface.Supervisor]
    Supervisor.start_link(children, opts)
  end

However, MyDependency.list_things() crashes with:

[notice] Application web_interface exited: exited in: WebInterface.Application.start(:normal, [])
    ** (EXIT) exited in: GenServer.call(MyDependency.Runtime.Worker, :list_things, 5000)
        ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

I understand this happens because the MyDependency dependency is not started, and thus I cannot call it.

Research

I tried adding this dependency to extra_applications, but to no avail:

def application do
    [
      mod: {WebInterface.Application, []},
      extra_applications: [:logger, :runtime_tools, :my_dependency]
    ]
  end

I also read the docs for the Application start function, hoping to find some way to defer the start of my main app to a later stage, but I couldn’t find it:

Questions

How can I fix this?

Marked As Solved

LostKobrakai

LostKobrakai

with {:ok, pid} <- Supervisor.start_link(children, opts) do
  {:ok, things} = MyDependency.list_things()
  {:ok, pid}
end

You need to start the supervisor (and therefore its children) before you can expect the children to run.

Also Liked

derek-zhou

derek-zhou

Shouldn’t it be:

Supervisor.start_link(children, opts)   
{:ok, things} = MyDependency.list_things()

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement