Qqwy

Qqwy

TypeCheck Core Team

When are Agent and Task.Supervisor useful?

I am working on a simple introduction talk to OTP. One of the things I myself am wondering about, however, is when Agent is useful and when Task.Supervisor is useful, as until now I’ve never felt the need to use them. So I am kind of searching for use-cases, so I can explain why they exist and when you should use them.

Agent

In the projects I have made in the past, I always wrapped a GenServer myself, instead of relying on Agent, because I knew that the message-passing and state-handling would become a little more complicated in the future. But I am not sure if this is the correct approach.

When is it better to use an Agent, and when is it better to just build a GenServer from scratch?

Task.Supervisor

I have used a lot of Tasks to enable small pieces of code that do not depend on each other to run concurrently. However, I’ve never had the need to supervise them, because they are either linked to the process that started them when using Task.async (so if a task fails, that process and all other tasks fail), or they were run with Task.start when I did not care about the return result, and therefore also did not care about if it succeeded or not.

In what cases is supervising tasks useful?

Most Liked

sasajuric

sasajuric

Author of Elixir In Action

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.

25
Post #3
gausby

gausby

As you may know, processes can do pure state (Agent), or pure computation (Task)—and everything in between! (GenServer, :gen_fsm).

When I poke around in a code base and I see a process that uses Agent instead of GenServer, I will instantly know that this is all about state. If I see a :gen_fsm I will know it is all about state, and when I see a GenServer I will have to take a closer look.

The tasks are for one off computations.

I am not an expert on the Task.Supervisor—because I don’t use tasks that often myself—but I guess you use it in situations where you would want your process to start the computation, and said computation should restart if it fails, but you don’t want your process to die along the computation if it should fail. Sometimes you might want to kill your process along with the task, so spawning and linking within the process would work in that scenario, but otherwise: stick it in a supervision tree.

Notice; Task, Agent, :gen_fsm are all implemented using GenServer; so yeah, you could go through life using only GenServer—but you probably shouldn’t :slight_smile:

entone

entone

I use Task.Supervisor to handle incoming Datagrams for my UDP server. As a binary message comes in, it spawns a Task to process the datagram, if the decryption fails, or json parsing fails, no big deal, UDP server is still running.

Handling incoming datagrams…
https://github.com/NationalAssociationOfRealtors/node_bucket/blob/master/lib/node_bucket/udp_server.ex#L33

And the supervisor…
https://github.com/NationalAssociationOfRealtors/node_bucket/blob/master/lib/node_bucket/supervisor.ex#L17

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement