Search code examples
elixirerlang-otpelixir-mix

Why is mix not using much of OTP?


I am trying to learn OTP by looking at good examples of projects using OTP. I studied the code for mix. But looks like mix is not using much of OTP.

To me, mix has lot of possible usecases for OTP. Examples like, compiling multiple files concurrently, downloading deps concurrently etc.

I was wondering why mix is not using OTP? Or is OTP overkill for a CLI application? Does it only make sense for long running applications?

I have also noticed that mix doesn't run things concurrently. Is there a reason for this too?


Solution

  • I think you've answered your own question. Core OTP concepts may not suite short lived commands.

    For example supervisors are great if you have many independent tasks (like server connections) and when one of them fails you still need to keep the app running. For mix task, if the task fails it doesn't make much sense to try it again, just print the error message and exit.

    GenServers are also great for keeping state in long running apps, but in CLI apps all the state is read ad hoc and then used so it may be better to just pass it to function call.

    However, it is not true that mix doesn't run things concurrently. Try running htop and then compiling your project and it will utilize all of your cores. mix compile uses parallel compiler to speed things up and it works great. The state of compilation is kept in ETS tables instead of GenServers so all processes can access it concurrently.

    OTP is for keeping your app up and running even in case of failure and this is just not the case with mix. You wouldn't like mix to hang and retry n times before reporting the error.

    If you want to see nice usage of OTP, ranch is great.