summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHentioe <me@bluerain.io>2020-12-03 17:10:34 +0800
committerHentioe <me@bluerain.io>2020-12-03 17:10:34 +0800
commitbb3769fecbe2b97dc9c405245816f29dd617e569 (patch)
treee27864d6534b797eedd888517ec8fce255c87515 /lib
parentFix error returns (diff)
Automatic retry of token application failure
Diffstat (limited to 'lib')
-rw-r--r--lib/azure_ex/token_hosting.ex18
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