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 #4
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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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 39297 209
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement