I have GenServer where I am pulling multiple database records, doing some processing and then updating the records. This job runs every 10 minutes.
Those records are processed with Enum.map
and don’t depend on each other, if one crashes (bad data), I don’t want it to affect the others. However I want to block on these tasks being returned because if the GenServer job runs again it will mess up the data.
The code below is similar to what I am doing. However testing this in iex
returns instantly. How can I block until I get the results?
children = [
{Task.Supervisor, name: MyApp.TaskSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
[1,2,3,4,5,6]
|> Enum.map(fn sleep_time -> Task.Supervisor.async(MyApp.TaskSupervisor,
fn ->
:timer.sleep(sleep_time)
end)
end)
|> Enum.map(&Task.await(&1))