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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hello, how can I check the Phoenix version ?
Thanks !
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Other popular topics
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









