Qqwy
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
Agentinstead ofGenServer, I will instantly know that this is all about state. If I see a:gen_fsmI will know it is all about state, and when I see aGenServerI 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_fsmare all implemented usingGenServer; so yeah, you could go through life using onlyGenServer—but you probably shouldn’tdefoemark
AFAIK
Agentis a “type” ofGenServercreated specifically for storing “shared” state, since in Elixir everything is isolated, I guess one would use it for storing live, quickly updating information.Task.Supervisorcan be used as a worker and it was designed to superviseTasks, if you don’t want them to be linked to another process and you want to define different strategy. I’m personally using it for sending email messages or push notifications, basically the activity which is not critical for the business logic and can be run in background.sasajuric
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
temporaryrestart strategy which is in fact the default forTask.Supervisor.Except for some temporary experiments I wouldn’t advise using
Task.start(orGenServer.start) in production as that may lead to dangling processes which may in turn cause some strange behaviour of the system.JEG2
One thought I’ve had about this is that a supervised
Taskseems perfect for any processing that doesn’t involve explicitly receiving messages.Task.Supervisormakes it handy to kick off such jobs as needed and it allows me to do things like retry in the case of transient errors, but not if the process exits normally.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