How do I start an application process only once?

I’ve created an Elixir application which starts a *.Application module as a mod in mix.exs and I’ve created a mix task which starts said app. Everything is fun here but I’ve realized that I have a bit of an issue and I can’t seem to find a solution.

When I start my application with iex -S mix while my task is active the application process starts again so I have a double app running. This is an issue for me as I’ve coded a discord bot in Elixir so I have double responds to every event currently consumed.

As it is an OSS project, rather than giving you small snippets of code, here’s the link for it.

How do I do this as how Phoenix did it that when I start iex -S mix the web process isn’t duplicated as well?

You have started application twice, so naturally it will be running twice. What is the question? You want to connect to the existing application with your shell?

Kind of. More as starting the shell while not starting the application module itself. To act as similar as if I were to write iex -S mix while mod isn’t defined under mix.exs.

iex -S mix run --no-start is what you are looking for

1 Like

Hmm, I managed to get similar results to what I wanted but I can’t access the data from the GenServers 3rd party libs use. So the functionality isn’t the same as how Phoenix did it (I did try to look at the SC of how Phoenix did it but can’t seem to find a way how they did it). I’ll keep the question open for now in case someone responds.

They did in in simple way - they run the Endpoint only if there is proper option in the configuration set.

2 Likes

Yep, looks like that was the solution. I added an application variable in the mix task and then added statements to what not to start when it’s not using that variable. Works like a charm.