Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach an iex session to running elixir/OTP process

I have an elixir/OTP application running in production, that was started with mix phoenix.server. It has several processes that hold state. One of these is a stash, implemented as Agent, that currently have a state that I would like to manually change, without stopping the whole application. Once I'm in a iex session inside the application it will be trivial, but I don't know if such option is even possible in elixir?

like image 979
Nagasaki45 Avatar asked Jul 20 '16 22:07

Nagasaki45


People also ask

How does elixir run on multiple CPUs?

Instead, Elixir uses process support in Erlang. These processes will run across all your CPUs (just like native processes), but they have very little overhead.” The BEAM accomplishes this by running in a single OS process. From there, it creates a “scheduler” for each available CPU core – each running in its own thread.

How to display information about an elixir term in iex shell?

In case of an Elixir script (.exs), it will compile and execute the file immediately. So you will see output as below — The i/1 command can be used to display information about an Elixir term. If I used this command in IEx shell for the previously loaded Issues module, it displays information as below —

How do I send a message to a process in Elixir?

Sending messages to processes in Elixir is easy. All we need is the process ID (PID) and a message to send. We can try this out in IEx: What we’ve done here is send the message, :hi, to our current IEx session. The send/2 function returns the message we sent, but nothing else appears to have happened. Where did the message go?

How to compile and load a module from file in IEX?

While in IEx session, you can do the below to compile and load a module from file — In case of an Elixir script (.exs), it will compile and execute the file immediately. So you will see output as below — The i/1 command can be used to display information about an Elixir term.


1 Answers

That depends on how did you start your OTP application. To connect to the node it needs to be started with either --name or --sname flag. You can check the name of currently running session with node()

$ iex
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.3.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> node()
:nonode@nohost

The node name is an atom where the first part is actual node name and second part is host. The host is used for routing, so it is hard to connect to a node that is deployed on nohost.

If you start iex with short name (--sname), it will automatically detect your hostname.

$ iex --sname foo --cookie ciastko
(...)
iex(foo@MacBook-Pro-Tomasz)1> node
:"foo@MacBook-Pro-Tomasz"

On some other console run iex with different name and the same cookie and try Node.connect(:"foo@MacBook-Pro-Tomasz"). They should connect.

You probably didn't start your phoenix application with that in mind and you can't connect now. To start Phoenix with this possibility next time you need to run:

elixir --sname some_name --cookie ciastko -S mix phoenix.server
like image 140
tkowal Avatar answered Oct 01 '22 03:10

tkowal