fireproofsocks

fireproofsocks

Restarting a task that can be awaited on

I’m starting to get deeper into the subtleties of supervisors and I could use some help understanding how and when to restart failed tasks.

Given I have a risky_function() that may raise errors, I can ensure that it eventually completes by supervising a task that calls it by doing something like this (assume MyTaskSupervisor is started):

Task.Supervisor.start_child(MyTaskSupervisor, fn -> 
  risky_work() end, 
  restart: :transient)

Even if the risky_work function hits errors, it restarts and it eventually completes successfully.

This is (I think) an example of a non-awaited task. How would this need to be structured if I wanted that same risky_work to be part of an awaited task?

The following attempt doesn’t ensure that the task completes:

task = Task.Supervisor.async_nolink(MyTaskSupervisor, fn ->
  risky_work()
end)
Task.await(task) # or Task.yield(task)

The risky_work function is called immediately and any errors it raises are immediately visible. The docs show how Task.Supervisor.async_nolink/3 might get used inside of a GenServer… I can get that example working, but it still doesn’t ensure that the risky_work ever completes. I can see where I can restart the task – there’s even a comment in the example # Log and possibly restart the task..., but if I am restarting the task, I feel like I’m doing something wrong. Isn’t that what the supervisor is supposed to do?

Thanks for any clarifications! I feel like maybe I’m thinking about this the wrong way. :thinking:

Most Liked

al2o3cr

al2o3cr

The way that an await-able Task gets its arguments isn’t compatible with being restarted by a standard Supervisor - the Task process is started up, and then the MFA and alias to use are sent to it:

https://github.com/elixir-lang/elixir/blob/v1.17.2/lib/elixir/lib/task/supervisor.ex#L599-L603

https://github.com/elixir-lang/elixir/blob/47abe2d107e654ccede845356773bcf6e11ef7cb/lib/elixir/lib/task.ex#L512-L516

So “restarting” the task process will leave it waiting for that initial tuple message forever.

This is obliquely referenced in the documentation for Task.Supervisor.async_nolink/3:

Note this function requires the task supervisor to have :temporary as the :restart option (the default), as async_nolink/3 keeps a direct reference to the task which is lost if the task is restarted.

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement