Starting the app on another OS process within mix task


I have an Elixir application that listens for tcp/http messages on the local machine (the “server”)
Then, I have mix tasks that do not start the Elixir application but only send tcp messages to it (“the client”).

When I run one of those mix tasks, I would like to start the server if it is not already started, and I would like the server to stay up until the machine is turned off.

What I am planning to do is ping the server at the start of the mix start, and if it does not respond, run a system command to launch the release of the app.

Do you think it could work ? Is there a better way to do that ?

Thank you

This is a usecase for a systemd socket unit.

The server could be “loaded” in a way that it is unable to answer the clients ping message in a reasonable time, then your client will try to start another instance of the server, which would fail because the port is already in use…

Given that there are systems you could configure to accomplish your goal, and those services would do that transparently (your client would never know if your server was started already or just recently) your client should just spit out an error message if it is unable to connect and shut down gracefully.

Hello, thank you for your answer.

This is interesting but it seems a bit hard for me.
Is it possible for instance to git clone the project, and make which creates a configuration for systemd part of the release ?

It seems that this solution the service should be either started manually or automatically started when the system starts. I’d rather start the service on demand.

I think it’s ok ping for N seconds before retrying to start.

Another way would be to ping, and without a response start the application and the detach the command line. I have no idea if it is possible.

I understant that the Elixir/Erlang use case is more about building a system and let it run forever.

If you’re letting it run indefinitely after being started on demand I’m wondering what speaks agains starting it on startup and have it be read when you want to use it.

Well this project is only used when I work for my company on my computer, and I use this computer on my free time too. Having this process up all the time is a waste of resources.
Of course this app has a very small footprint so it would be ok to have it start automatically on boot given my coworkers which whom I will share the project do not have to configure systemd stuff.

Also many of us including me have laptop with MacOs and it seems that systemd is not available here.

Under this circumstances, I’d use systemds ability to have services on a per-user basis. Paired with the fact, that one should always have an account for work and one for personal use on such a workstation, that should give you the necessary amount of control.

Judging by how often I shut down my laptop those two statements are quite contradicting. Personally I’d opt for simply starting the service explicitly if it’s not meant to be running all the time. I’m not sure how much you’d gain from starting it implicitly when doing the first command. I can’t imagine it being worth much effort.

Yes you are right, I should focus on other features.
I was asking if there were an idiomatic way for this use case, and I have my answer :slight_smile: