Attach an iex session to running elixir/OTP process
Asked Answered
W

1

11

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?

Worthen answered 20/7, 2016 at 22:3 Comment(0)
M
22

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
Multiplex answered 21/7, 2016 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.