Laser
Laser

Reputation: 5439

How can you run an iex session and a phoenix server attached to the same database and mix environment in production?

When you run a server like MIX_ENV=prod mix phx.server and then on another screen attached to the same server try to run a mix-environment attached iex session like iex -S mix then you get an error and shutdown with a complaint like Failed to start Ranch listener VukWeb.Endpoint.HTTP in :ranch_tcp:listen([port: 4000]) for reason :eaddrinuse (address already in use)

Is there a way to run an iex session while a server is running in a separate screen attached to the same database? I'm confused as to why iex -S mix is even trying to connect to an external port, as that iex session should have no server running nor require an external port simply for loading the mix environment?

I'm aware you can run the server with an iex session like MIX_ENV=prod iex -S mix phx.server, but my understanding is that is neither ideal for performance nor is it nice to have your iex session interrupted by streams of user logs as requests are processed (which is what we're doing right now). I also tried switching up the port like MIX_ENV=prod PORT=4040 iex -S mix but the flag seems to go ignored, as the complaint comes back the same with a reference to port 4000. I'm wonder if maybe there's some hardcoding that's causing the environment variable to be ignored, and if just undoing this hardcoding and switching it to a different port like this is the right approach, even if the port goes unused by the non-server mix environment.

If anyone has a tip on how you can get both a iex session and serving running—or has a different suggestion for workflow that makes such a desire unnecessary—would love to hear!

Thanks

Upvotes: 3

Views: 3150

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Mix has a perfect very detailed documentation.

Mix.Tasks.Run is a default task. iex -S -mix is essentially equivalent to iex -S mix run.

The latter starts your application, which [wild reasonable guess] in turn starts cowboy as a dependency. Hence the error.

 iex -S mix run --no-start

is what you are looking for. This task accepts several arguments, other are listed on the help page I linked.

Upvotes: 5

Related Questions