Don't start worker process if running console

In our phoenix app, we have in app.ex several child processes:

def start(_type, _args) do
  Supervisor.start_link([
    worker(MyApp.Worker.Worker1, []),
    worker(MyApp.Worker.Worker2, []),
    worker(MyApp.Worker.Worker3, []),
  ], [strategy: :one_for_one, name: MyApp.Supervisor])

But some of those processes are required only if running the web server along, not when we do iex -S mix to get a console. Can I filter out those processes if the main server process is not started?

Thanks

https://hexdocs.pm/phoenix/1.4.11/Phoenix.Endpoint.html#server?/2

2 Likes

Perhaps a bit of a tangent, but this is one of the reasons I Always use Releases. Running a remote console gets you a console running inside the existing running node, so you don’t have to worry about it starting up extra stuff.

It’s still a problem in dev, which is exactly the reason, why phoenix has a config if the server shall be started or not.

It’s your worker, just add a config to control whether to work or not, in dev.exs make the value false and have your worker check for true/false then don’t do any work if false.