# Configurando quantidade de tentativas no Oban

Mesmo com o mecanismo de *retry* do *Tesla,* nossa requisição pode falhar e o Tesla não conseguir se recuperar. Para termos mais chance de sucesso, podemos configurar a quantidade de tentativas que o Oban poderá fazer antes de desistir. Por padrão ele tenta uma vez, mas podemos aumentar a quantidade. Nesse exemplo colocarei duas tentativas.&#x20;

Para configurar, vamos adicionar opções a nossa `Oban.Worker`:

<pre class="language-elixir" data-title="lib/coffee_shop/integrations/coffee/hot_coffees_worker.ex" data-line-numbers><code class="lang-elixir">defmodule CoffeeShop.Integrations.Coffee.HotCoffeesWorker do
<strong>  use Oban.Worker, max_attempts: 2
</strong>
  alias CoffeeShop.Integrations.Coffee.Response
  alias CoffeeShop.Integrations.Coffee.Client

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"opts" => opts}}) do
    opts = to_list(opts)
    
    case Client.all_hot_coffees(opts) do
      {:ok, %Response{status: 200, body: _body}} -> :ok
      {:ok, %Response{status: status, body: _body}} -> {:error, "status: #{status}"}
      error -> error
    end
  end

  defp to_list(map) do
    Enum.map(map, fn {key, value} -> {String.to_existing_atom(key), value} end)
  end
end
</code></pre>

Adicionamos a opção `max_attempts` para `2` . Agora precisamos testar se a configuração está correta.

<pre class="language-elixir" data-title="test/coffee_shop/integrations/coffee/client_test.exs" data-line-numbers><code class="lang-elixir">defmodule CoffeeShop.Integrations.Coffee.ClientTest do

  # ...
  
  describe "all_hot_coffees_async/0" do
    test "schedule and execute a job", %{bypass: bypass} do
      Bypass.expect_once(bypass, "GET", "/coffee/hot", fn conn ->
        Plug.Conn.resp(conn, 200, "")
      end)

      opts = [
        base_url: "http://localhost:3000",
        retry_delay: 1
      ]

      in_a_day = DateTime.add(DateTime.utc_now(), 3600 * 24, :second)

<strong>      assert {:ok, job} = Client.all_hot_coffees_async(opts)
</strong><strong>      assert job.max_attempts == 2
</strong>
      assert_enqueued(
        worker: HotCoffeesWorker,
        args: %{"base_url" => "http://localhost:3000", "retry_delay" => 1},
        scheduled_at: in_a_day
      )

      args = %{opts: Map.new(opts, fn option -> option end)}

      assert :ok = perform_job(HotCoffeesWorker, args)
    end
  end
end

</code></pre>

Na linha 19 fazemos um *check* se o máximo de tentativas reflete o configurado. Deixamos o resto com o Oban.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://consumindo-apis-com-elixir.cafecomelixir.com.br/problemas-de-api-externa/rate-limit-de-longa-duracao/configurando-quantidade-de-tentativas-no-oban.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
