I want to start a genserver from executable binary. I can start the application by starting a node
iex --name n1@127.0.0.1 -S mix
Is there any way that I can start the server on a node without actually starting the node itself ?
defmodule SimpleCluster.CLI do
def main(args \\ []) do
SimpleCluster.Application.start(args)
end
end
The application module looks like
defmodule SimpleCluster.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_args) do
children = [
{Cluster.Supervisor, [topologies(), [name: SimpleCluster.ClusterSupervisor]]},
SimpleCluster.Observer,
SimpleCluster.Ping
]
opts = [strategy: :one_for_one, name: LibclusterCluster.Supervisor]
Supervisor.start_link(children, opts)
end
defp topologies do
[
example: [
strategy: Cluster.Strategy.Epmd,
config: [
hosts: [
:"n1@127.0.0.1",
:"n2@127.0.0.1"
]
]
]
]
end
end
and am using escript.build
for making executable binary.
What I want is when I start the executable on one node, it starts the server and send me responses if it is alive or not. When I run the executable on a new node, I want it to start the server and then connect with the first node.
i have the code for that as well. The only issue is that I am not able to start the server from executable.
code for observer
defmodule SimpleCluster.Observer do
@moduledoc """
Simple process that monitors node changes in the
current cluster.
"""
use GenServer
require Logger
def start_link(_), do: GenServer.start_link(__MODULE__, Map.new())
@impl GenServer
def init(state) do
:net_kernel.monitor_nodes(true)
{:ok, state}
end
@impl GenServer
def handle_info({:nodedown, node}, state) do
# A node left the cluster
Logger.info("--- Node down: #{node}")
{:noreply, state}
end
def handle_info({:nodeup, node}, state) do
# A new node joined the cluster
Logger.info("--- Node up: #{node}")
{:noreply, state}
end
end