Do something after Task death

Because

  1. Elixir’s MOD is “let if fail”, instead of coding paranoid catching of all possible exception like in Java
  2. A task can be killed by other means and one still needs to handle it gracefully (perform cleanups, pubsub events etc)
    So I followed @mpope’s recommendation and built a GenServer that monitors task execution and handles its death in handle_info({:DOWN, ... and normal return in handle_info({_, result}... . The GenServer is started in application’s supervisor tree and then when the task is started I register it using MyMonitor.monitor(task) which delegates to Process.monitor(task.pid) - to make sure the calling process - in this case, my GenServer/Monitor - gets its handle_info called.
    I wish there was an easier way out of the box but c’est la vie.