Repository navigation
Page large device inventories for computer cleanup #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b68dc6
Page large device inventories for computer cleanup
PrzemyslawKlys ad293f4
Honor Graph owner projection and throttle responses
PrzemyslawKlys e769c30
Keep large device inventories complete across Graph failures
PrzemyslawKlys 6a68235
fix(devices): 🐛 Honor retry delays and complete owner inventory
PrzemyslawKlys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| function Get-GraphEssentialsPagedInventory { | ||
| <# | ||
| .SYNOPSIS | ||
| Reads a Graph collection one page at a time with bounded page retries. | ||
|
|
||
| .DESCRIPTION | ||
| Keeps only the current raw page in memory and retries its URL after a transient | ||
| failure. Callers must buffer their final inventory until this function finishes, | ||
| because an incomplete collection must never be used for cleanup decisions. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string] $Uri, | ||
|
|
||
| [ValidateRange(1, 10)] | ||
| [int] $MaxPageAttempts = 3 | ||
| ) | ||
|
|
||
| $pageUri = $Uri | ||
| $pageNumber = 0 | ||
| $seenPageUris = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::Ordinal) | ||
| while ($pageUri) { | ||
| if (-not $seenPageUris.Add($pageUri)) { | ||
| throw "Graph inventory returned a repeated page URL after page $pageNumber." | ||
| } | ||
| $pageNumber++ | ||
| $attempt = 0 | ||
| while ($true) { | ||
| $attempt++ | ||
| try { | ||
| $response = Invoke-MgGraphRequest -Method GET -Uri $pageUri -OutputType PSObject -ErrorAction Stop | ||
| break | ||
| } catch { | ||
| $errorRecord = $_ | ||
| $statusCode = $null | ||
| if ($errorRecord.Exception.Response -and $errorRecord.Exception.Response.StatusCode) { | ||
| $statusCode = [int] $errorRecord.Exception.Response.StatusCode | ||
| } | ||
| $message = [string] $errorRecord.Exception.Message | ||
| $transient = $statusCode -in @(408, 429, 500, 502, 503, 504) -or | ||
| $message -match '(?i)timed?\s*out|timeout|cancell?ed.*300 seconds|connection.*(closed|reset)|transport stream' | ||
|
|
||
| if (-not $transient -or $attempt -ge $MaxPageAttempts) { | ||
| throw "Graph inventory page $pageNumber failed after $attempt attempt(s): $message" | ||
| } | ||
|
|
||
| $delaySeconds = [Math]::Min(30, [int] [Math]::Pow(2, $attempt - 1)) | ||
| if ($statusCode -eq 429 -and $errorRecord.Exception.Response.Headers) { | ||
| $headers = $errorRecord.Exception.Response.Headers | ||
| $retryAfter = $null | ||
| if ($headers.GetType().FullName -eq 'System.Net.Http.Headers.HttpResponseHeaders') { | ||
| if ($headers.RetryAfter) { | ||
| $retryAfter = [string] $headers.RetryAfter | ||
| } | ||
| } elseif ($headers -is [System.Collections.IDictionary]) { | ||
| $retryAfter = @($headers['Retry-After'])[0] | ||
| } else { | ||
| $retryAfter = $headers.'Retry-After' | ||
| } | ||
| $retryAfterSeconds = 0 | ||
| if ($retryAfter -and [int]::TryParse([string] $retryAfter, [ref] $retryAfterSeconds)) { | ||
| $delaySeconds = [Math]::Max(1, $retryAfterSeconds) | ||
| } elseif ($retryAfter) { | ||
| $retryAt = [DateTimeOffset]::MinValue | ||
| if ([DateTimeOffset]::TryParse([string] $retryAfter, [ref] $retryAt)) { | ||
| $delaySeconds = [Math]::Max(1, [int] [Math]::Ceiling(($retryAt - [DateTimeOffset]::UtcNow).TotalSeconds)) | ||
| } | ||
| } | ||
| if ($delaySeconds -gt 3600) { | ||
| throw "Graph inventory page $pageNumber was throttled for $delaySeconds seconds; this run cannot complete within the retry limit." | ||
| } | ||
| } | ||
| Write-Verbose "Graph inventory page $pageNumber failed ($message). Retrying in $delaySeconds seconds." | ||
| Start-Sleep -Seconds $delaySeconds | ||
| } | ||
| } | ||
|
|
||
| if ($null -eq $response -or $null -eq $response.value) { | ||
| throw "Graph inventory page $pageNumber did not contain a value collection." | ||
| } | ||
| foreach ($item in $response.value) { | ||
| if ($null -ne $item) { | ||
| $item | ||
| } | ||
| } | ||
| $pageUri = $response.'@odata.nextLink' | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.