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!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
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
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









