Question

I'm struggling, as an OTP noobie, to understand how to structure my Erlang project. So far it has several applications under an app directory managed by rebar:

proj_root
   apps
      app1
      app2
      appN
   rebar.config 

I can start app1, say, in the shell with application:start(app1). No doubt I could repeat this through appN. But is there a preferred or better way? Can I, say, write a function that bundles all these starts? If so, where do I put it?

I have several other questions along this line, but will post separately.

Many thanks,

LRP

Was it helpful?

Solution

You can indeed start applications manually as you suggest. This can quickly become burdensome if you have many applications and there are dependencies between them.

Automating the process is quite easy to implement with a recursive function. If you try to start an application while one or more dependencies is not running, application:start/1 will fail and return {error, {not_started, App}}. This function can be in any of your applications or even in its own.

However, this manual (or automated) way to proceed is not the OTP way, even if it can prove useful (typically for tests…). If you follow OTP principles, you are supposed to create a release with a .rel file containing all your applications. OTP releases are composed of a set of applications (yours and system applications they depend on), an emulator and a boot script that will start all applications (and handle dependencies). Starting a node with your applications can be performed by using the -boot flag to erl pointing to the proper boot script.

This is quite complex and rebar can actually build releases. It will even produce shell scripts to start nodes with all your applications using the OTP boot mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top