Is it possible to create background job and Asynchronous with Task.Supervisor?

Hello, is it possible to create background job and Asynchronous with Task.Supervisor ? I need my code to be loaded in other thread like background job. I create these:

def register do

  Task.Supervisor.async_nolink(Web.AuthController, fn ->
    Process.sleep(10000)
    Query.add_identity(%{
      "user_id" => user_info.id
    })
  end)
  
  other(email)
  IO.puts "test"
  conn
  |> put_status(200)
  |> json(%{message: "test"})
end

defp other(email) do
  Task.Supervisor.async_nolink(Web.AuthController, fn ->
    Process.sleep(10000)
    send_email(email)
  end)
end

I don’t need to load these outputs; I think they are loaded Synchronous but I need to load Asynchronous or I’m wrong?

is it my way true ? or I have to use Job plugin like exq

Thanks

1 Like

If you want to do something completely asynchronous, that doesn’t crash your app if it fails, you can use Task.start. If you want to link, you can try other functions from the Task module: https://hexdocs.pm/elixir/Task.html

1 Like