1
votes

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

1
How is your config file loading the port option? Can you check if your prod.exs has something like http: [port: {:system, "PORT"}],?S.B
There's a hardcoding of port 4000 for htttp / 443 for https. Is the suggestion then to run the two environments on different ports that I pass with a PORT= flag when run? Or is there syntax to pass a fallback in the config in case of nothing getting passed?Laser
You can try port: System.get_env("PORT") || 4000. Are your using --remsh to connect to the running server?S.B
Another developer's deploying so can't check right now, but I think that should work, thanks. Not using --remsh, just ssh'ed directly in so running locally from the server.Laser
Yep, also had to switch out the port for HTTPS, but now it works! Thanks :)Laser

1 Answers

2
votes

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.