When are Agent and Task.Supervisor useful?

A frequently overlooked but important role of supervisors is proper cleanup of processes. When a supervisor terminates, all of it’s descendants will be taken down as well. That allows you to terminate needless processes if some part of the system crashes, and also to cleanly take down the entire application (for example during rolling upgrade).

Therefore every process in your system should reside in the supervision tree, even if you don’t want to restart them. In such cases you can use the temporary restart strategy which is in fact the default for Task.Supervisor.

Except for some temporary experiments I wouldn’t advise using Task.start (or GenServer.start) in production as that may lead to dangling processes which may in turn cause some strange behaviour of the system.

23 Likes