How to restart a controlled task that fails

{:ok, tas} = Task.Supervisor.start_link(restart: :transient, max_restarts: 4)
a = 1
Task.Supervisor.async_nolink(tas, fn -> IO.puts "#{a}..." end)
Task.Supervisor.async_nolink(tas, fn ->
  IO.puts "Not Restarting :( "
  1 = 2
end)
a =  a + 1
Task.Supervisor.async_nolink(tas, fn -> IO.puts "#{a}.." end)
a =  a + 1
Task.Supervisor.async_nolink(tas, fn -> IO.puts "#{a}.." end)

The option restart: :transientdoes not seem to have any effect.

I have few tasks Task.async(fn(x) -> fetch_info(x) endthat make an HTTP request to get multiple resources and have a timeout error. It would be nice to repeat the failed task instead of using try, rescue.

I think that async_nolinkis the closest I got without a process crash. If there is no way to use Task, we have a simpler approach, using Supervisor, which starts several processes that exist after they are executed and restart them if they do not work?

+4
source share
2 answers

Task.Supervisor.start_child Task.Supervisor.async_nolink :

{:ok, tas} = Task.Supervisor.start_link(restart: :transient, max_restarts: 4)

Task.Supervisor.start_child(tas, fn -> 1 = 2 end) 
+6

, - , . Task Docs , :

, . : simple_one_for_one , (.. ).

: restart - , : ( ),: : . Supervisor.Spec . , ;

GenServer , , GenServer Intro. Little Elixir OTP Guide , , OTP, .

EDIT: , .

+4

Source: https://habr.com/ru/post/1628936/


All Articles