owaisqayum
Starting a Genserver from executable
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
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?
Would
mix ecto.rollback -v 200809061...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Other popular topics
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I would like to know what is the best IDE for elixir development?
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New








