Umbrella project only works when started via iex

Hello!

I’ve got an umbrella project with 2 apps in it: an app with business logic and an app for db access (ecto).

In the business logic app, I specified a dependency on the db app like so: {:db, in_umbrella: true}.
The business app uses the db app to insert some data on startup.

The problem is that the database isn’t modified…

… unless I start the umbrella project via iex -S mix.

I don’t get any error messages and my code in the db app is called properly (confirmed via log messages).

So I suspect my (trivial) code is working fine and it’s some startup timing problem where the db app isn’t started before the business app.

Do I need to declare anything else to make the dependency on the :db app working properly?

P.S. I even stopped my database server (postgres) and ran the project to see if I even get some error messages – which I do not get unless I launch everything via iex.

Thanks!

How do you start the app when it does not work?

From the root of the umbrella project via mix run.

This is not a web app, is it? I mean, how do you know or ask the app to modify the database if it’s got no web interface?

mix run does usually exit quickly (after the called function has returned), try to use --no-halt option as described in mix run documentation.

This though will start your application in the background as far as I remember and kept running until you shut it down by other means.

If it is a one-shot thing, you should probably consider writing a custom mix task or an escript.

4 Likes

@hubertlepicki:

No, there isn’t a web frontend or anything like that. I connect via pgcli (postgres client) to my database and query the server.

@NobbZ:

Thanks, --no-halt does the trick! So even though there’s still a process running, the application exits because the first called function returns?

EDIT: After some searching, I found the answer to my question. mix run starts the application in its own beam process and after the start callback of the app is finished, the process gets terminated – and everything else that got started.
Here are some references for anyone, stumbling on this: https://stackoverflow.com/questions/30687781/how-to-run-elixir-application#answer-30688873
Basic: Missing a fundamental concept with applications, appreciate help - `mix run` not waiting

Thank you for you help!

2 Likes