Elixir Unix Signal Helper
After you've added the dependency:
def deps do
[{:signalex, "~> 0.0.1"}]
endYou can create your signal handler:
defmodule MyProject.SignalHandler do
use Signalex
def handle_event(:sigterm, state) do
# do some cleanup or whatever
{:ok, state}
end
end
If you want to handle a signal other than sigusr1, sigquit and sigterm, you'll need to list it explicitly:
defmodule MyProject.SignalHandler do
use Signalex, handle: [:sigusr2, :sigalrm]
...
endStart the handler:
:ok = MyProject.SignalHandler.start()Since the module is only executed once, on termination, there's little point in supervision.
As far as I can tell, there's no way to prevent the application from shutting down (which is part of the reason why there's no test for this). But you can execute code to do cleanup or whatever and delay termination until your handler exits.