I am writing an app that will call an installed library at host server, wait for its execution and broadcast result output to client via channel.
At happy scenario, the execution time will finish in milli seconds, with little CPU consumption, however, considering that the called library might get stuck or consume high CPU power for threshold cycles, what’s the recommended way to kill its OS process and return some message to the user?
I am running phoenix as a service using systemd at my development laptop, I just restart the service as sudo systemctl restart phoenix and I can see that the library process will be killed, however, I can’t do so at production, as lots of users are connected and using the same library.
So you “library” is not a “library” but a regular application you run.
If you were using Port instead of System.cmd/3 (which is basically just a wrapper around the former), you could use Port.info/1,2 to get the OS pid and read from /proc/${os_pid} the necessary information to track CPU usage/Memory usage or whatever metric you need to kill it.
Thanks, so, do you recommend using an elixir worker to monitor the application os_pid? and decide to kill it? or using an external solution like: inspeqtor ? I think Elixir based solution would be better, as I can directly send a feedback to client about the monitored process…
This will kill the started mylib after it has consumed 60 seconds of CPU time. cpulimit does only seem to limit current usage, but is unable to kill on exhaustive use…
Though I maybe misunderstand what you are actually trying to achive.
NIF is basically a way to interact with native code. A good example is an Erlang sqlite library that maps Erlang function calls to C function calls that do the real work.
So in your scenario it would probably be an overkill to make a NIF module that delegates work to native code (C/C++ code, or in a shared library like a .dll or .so file). Because it seems you simply have to interact with a full-blown external program.
I think your scenario can be best served by a Port since you can monitor a “connection” to executing a program and just kill it whenever you like – but this lacks the ability to monitor CPU usage.
cpulimit can help you but you’ll have to monitor the exit code of the external program.