GenServers depends on starting another genserver (cross app)

Hello there,

I have a little follow up question for mi previous question referencing here. For small recap:

I was triing to make one library supervisor free for sake of reducing complexity, what worked quite well. The main problem was that it was used in ambrela where some applications were starting some server from lib and some were not and I need to awoid process already started while keep ability to take service and run it on its own. (server are quite memory heavy)

So today problem is that that memory heavy servers now need a start one server, server should by started of group of aplication in umbrella start at least one of the mentioned memory heavy servers. From mi point of view, the easiest solution would by to just make every memory heavy server supervisor which would start self and the dependent server, which would have name and would also use the already_started Process link trick:

def start_link(_ \\ nil) do
    __MODULE__
    |> GenServer.start_link(:ok, name: __MODULE__)
    |> start_link_response()
end
defp start_link_response({:error, {:already_started, pid}}) do
    true = Process.link(pid)
    {:ok, pid}
  end

defp start_link_response(response), do: response

Bat it seems somehow smelly, is there any more elixirish solution?

You want to link to this process so the calling process will be exited if the “heavy” server exits ?

I have use cases for both, this is one of reasons why I would like to keep gen_server api, so every application could decide what will happend with it if server crash, some need restart and some can ignore that…

I think you are complicating things for no reason.

Your heavy server should be started, and should be registered somewhere (with it’s name or in a registry).
Then, any dependent server should use a monitor to be told when the heavy server exits, so they can wait for it to be restarted.

So, instead of starting the heavy process multiple times, you start it in your supervision tree only once.

But I have multiple applications in multiple umbrellas, so I don’t have single supervisor tree. One think that I consider now is to split common lib so it is solved by mix…

All the applications that depend on your heavy server should be started after the application that starts the heavy server. This way, the heavy server is aliver when your other applications start.

There is a way with Elixir/Erlang to configure the applications start order, but I do not know it myself.