diff options
author | Hentioe <me@bluerain.io> | 2020-12-03 17:10:34 +0800 |
---|---|---|
committer | Hentioe <me@bluerain.io> | 2020-12-03 17:10:34 +0800 |
commit | bb3769fecbe2b97dc9c405245816f29dd617e569 (patch) | |
tree | e27864d6534b797eedd888517ec8fce255c87515 | |
parent | Fix error returns (diff) |
Automatic retry of token application failure
-rw-r--r-- | lib/azure_ex/token_hosting.ex | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/azure_ex/token_hosting.ex b/lib/azure_ex/token_hosting.ex index cb1646c..e659fd5 100644 --- a/lib/azure_ex/token_hosting.ex +++ b/lib/azure_ex/token_hosting.ex @@ -5,6 +5,8 @@ defmodule AzureEx.TokenHosting do use GenServer + require Logger + defmodule Token do @moduledoc false @@ -51,8 +53,8 @@ defmodule AzureEx.TokenHosting do @grant_type "client_credentials" @headers [{"Content-Type", "application/x-www-form-urlencoded"}] - @spec apply_token(Params.t()) :: {:ok, Token.t()} | {:error, binary} - defp apply_token(%{tenant: tt, client_id: ci, client_secret: cs}) do + @spec apply_token(Params.t()) :: {:ok, Token.t()} | {:error, any} + defp apply_token(%{tenant: tt, client_id: ci, client_secret: cs} = params) do endpoint = "https://login.microsoftonline.com/#{tt}/oauth2/v2.0/token" form = [ @@ -68,11 +70,17 @@ defmodule AzureEx.TokenHosting do {:ok, token} + {:error, %HTTPoison.Error{reason: :timeout}} -> + Logger.warn("the token request timed out, retrying...") + + apply_token(params) + {:error, e} -> - # TODO: 超时等网络问题自动重试 # TODO: 抽象出错误模型 - # TODO: 记录日志 - {:error, inspect(e)} + Logger.error("Token application failed, details: #{inspect(e)}") + :timer.sleep(500) + + apply_token(params) end end |