So im looking to do something like below where I get a list of posts
from the db and then run an api call on each post's
comments
and i want it to all happen as concurrently as possible. Is nesting task bad practice?
defmodule MyApp.ObanWorker do
def perform do
posts = MyApp.Repo.all(Posts)
MyTaskSupervisor
|> Task.Supervisor.async_stream(posts, &MyApp.ApiClient.do_it/1)
|> List.flatten()
|> Enum.each(fn {_, message} -> Logger.info(message) end)
:ok
end
end
defmodule MyApp.ApiClient do
def do_it(%{comments: comments} = post) do
MyTaskSupervisor
|> Task.Supervisor.async_stream(comments, &make_call/1)
|> Enum.filter(&(elem(&1, 0) == :error))
end
def make_call(comment) do
result = # ...make api request
case result do
{:ok, %{status: 200}} -> {:ok, "good job."}
_ -> {:error, "not good for #{comment}."}
end
end
end