erlangdistributed-systemgen-serverrebar3

How to host a gen_server as a standalone node using rebar3 release?


I have a rebar3 project which has some gen_server implementation. I would like to write an application in such a way, that the release would host that gen_server as an Erlang node in daemon mode.


Example

There is some gen server:

%% file src/my_gen_server.erl
-module(my_gen_server).
-behaviour(gen_server).
...

I would like to compile the project

$ rebar3 release

Then run it as daemon:

_build/prod/rel/myapp/bin/myapp daemon --setcookie biscuit

In a separate terminal session launch Erlang and connect to it:

$ erl -sname test -setcookie biscuit
1> gen_server:call(my_gen_server, test).
response

How do I set this up? By that question I mean: how to make the application be that gen server, and how to make it accept incoming messages from other nodes, possibly at different machines?


Solution

  • I suppose that you mean that you have an "application" project and you want to have that application as the only application in a release.

    Add a relx section to your rebar.config as explained in the doc and make sure that the application code is reachable, such as having it in ./apps/<application>/src/. With this step you have an erlang release starting your application code.

    In your .app.src file you specify a Module implementing the application behaviour, usually this module starts a supervisor, but you can start your gen_server directly here. Just bear in mind that if the process dies, the whole node dies with it, thats why this is discouraged.

    Regarding your last question, once you have the distributed erlang set up, you can send messages to registered process in nodes with the {Name :: atom(), node()} ! Message :: term() syntax. You can use the methods in gen_server or erpc too