msgraph-sdk-powershell icon indicating copy to clipboard operation
msgraph-sdk-powershell copied to clipboard

Powershell Microsoft.Graph- How to set MaxRetry

Open MarkDordoy opened this issue 3 years ago • 4 comments

Is there a way to set the max retry value for the Microsoft.Graph powershell modules.

I'm hitting an issue from time to time, where a particular cmdlet retries 3 times then fails. Ideally i'd like a way to increase this somehow. Is this supported?

This supports the theory that it is set to 3 and backs up what im seeing.

Thanks

MarkDordoy avatar Feb 22 '22 15:02 MarkDordoy

@MarkDordoy, the PowerShell SDK does currently not support this. The MaxRetry is currently locked at 3, and the implementation is based on https://microsoftgraph.github.io/msgraph-sdk-design/middleware/RetryHandler.html.

Which command are you using? Ideally, the service should respond with a retry-after header that SDK will honor.

We will consider exposing a way for customers to set their preferred MaxRetry. This will likely be through a global variable.

peombwa avatar Feb 23 '22 22:02 peombwa

I came across this issue when using this call: Get-MgAuditLogDirectoryAudit -Filter "targetResources/any(t: t/id eq '$($user.Id)') and activityDisplayName eq 'Enable account' and activityDateTime gt $gracePeriodStr" -Top 1

Ifs fine for a while, but i was running this within a foreach-Object -Parallel script block so i guess i was hitting 429s and 3 retries was not enough (I think).

MarkDordoy avatar Feb 25 '22 13:02 MarkDordoy

Error that is caught:

Get-MgAuditLogDirectoryAudit_List:
Line |
  25 |  … toryAudit -Filter "targetResources/any(t: t/id eq '$($user.Id)') and  …
     |                                                         ~~~~~~~~
     | Code: tooManyRetries
Message: More than 3 retries encountered while sending the request.

It throws an exception type of Microsoft.Graph.ServiceException

$_.Exception.Error

Code            : tooManyRetries
Message         : More than 3 retries encountered while sending the request.
Target          :
Details         :
InnerError      :
ThrowSite       :
ClientRequestId :
AdditionalData  :

Stack Trace:

$_.Exception.StackTrace
   at Microsoft.Graph.RetryHandler.SendRetryAsync(HttpResponseMessage response, CancellationToken cancellationToken)
   at Microsoft.Graph.RetryHandler.SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken)
   at Microsoft.Graph.CompressionHandler.SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken)
   at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Microsoft.Graph.PowerShell.Reports.AuditLogsListDirectoryAudits_Call(HttpRequestMessage request, Func`3 onOk, Func`3 onDefault, IEventListener eventListener, ISendAsync sender)
   at Microsoft.Graph.PowerShell.Reports.AuditLogsListDirectoryAudits_Call(HttpRequestMessage request, Func`3 onOk, Func`3 onDefault, IEventListener eventListener, ISendAsync sender)
   at Microsoft.Graph.PowerShell.Reports.AuditLogsListDirectoryAudits(Nullable`1 Top, Nullable`1 Skip, String Search, String Filter, Nullable`1 Count, String[] Orderby, String[] Select, String[] Expand, Func`3 onOk, Func`3 onDefault, IEventListener eventListener, ISendAsync sender)
   at Microsoft.Graph.PowerShell.Cmdlets.GetMgAuditLogDirectoryAudit_List.ProcessRecordAsync()```

MarkDordoy avatar Feb 25 '22 13:02 MarkDordoy

Got it! We will need to allow customers to set RetriesTimeLimit global variable that they can use to control the maximum number of retries allowed. The current implementation currently locks this to 3.

peombwa avatar Apr 08 '22 17:04 peombwa

@peombwa I have the impression the max retries and retry after was already covered by another PR of the v2 work you did. Just checking.

maisarissi avatar Oct 14 '22 15:10 maisarissi

@maisarissi, this is still an open feature request. It hasn't been implemented in v2.

I'll expedite its implementation in v1 since it is blocking customers.

peombwa avatar Oct 20 '22 16:10 peombwa

Hi @peombwa. When will this be available for beta v2 commands? Or does Set-MgRequestContext work with beta v2 commands now?

ArmaanMcleod avatar Aug 31 '23 06:08 ArmaanMcleod