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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement