The process - deep dive into Elixir processes

Elixir’s Task will take care of child_spec/1 and spawn_link. Often you want to implement orderly shutdown, like saving some states, I think you still need to do the following:

  • trap :EXIT in your process
  • handle :EXIT message to do your orderly shutdown
  • exit(:shutdown) to terminate the process and notify the supervisor you are done

The supervisor will send you a :EXIT message in shutdown, and wait up to 5 seconds (configurable). It expects you to shutdown yourself and notify the supervisor. If you do not trap, you will crash. If you trap, but do nothing, the supervisor will brutal_kill you after the timeout. If you shutdown but did not call exit(:shutdown) the supervisor will believe you crashed and print a warning.