Skip to content

Commit 90a2879

Browse files
committed
Fix generation of request examples for docs on linux/macOS; timeout instead of hang when webserver does not start
1 parent f38a812 commit 90a2879

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

Diff for: docs/generate-examples.ps1

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#Requires -Version 7.3
22

3-
# This script generates response documents for ./request-examples
3+
# This script generates HTTP response files (*.json) for .ps1 files in ./request-examples
44

55
function Get-WebServer-ProcessId {
6-
$processId = $null
7-
if ($IsMacOs || $IsLinux) {
8-
$processId = $(lsof -ti:14141)
6+
$webProcessId = $null
7+
if ($IsMacOS -Or $IsLinux) {
8+
$webProcessId = $(lsof -ti:14141)
99
}
1010
elseif ($IsWindows) {
11-
$processId = $(Get-NetTCPConnection -LocalPort 14141 -ErrorAction SilentlyContinue).OwningProcess?[0]
11+
$webProcessId = $(Get-NetTCPConnection -LocalPort 14141 -ErrorAction SilentlyContinue).OwningProcess?[0]
1212
}
1313
else {
14-
throw [System.Exception] "Unsupported operating system."
14+
throw "Unsupported operating system."
1515
}
1616

17-
return $processId
17+
return $webProcessId
1818
}
1919

20-
function Kill-WebServer {
21-
$processId = Get-WebServer-ProcessId
20+
function Stop-WebServer {
21+
$webProcessId = Get-WebServer-ProcessId
2222

23-
if ($processId -ne $null) {
23+
if ($webProcessId -ne $null) {
2424
Write-Output "Stopping web server"
25-
Get-Process -Id $processId | Stop-Process -ErrorVariable stopErrorMessage
25+
Get-Process -Id $webProcessId | Stop-Process -ErrorVariable stopErrorMessage
2626

2727
if ($stopErrorMessage) {
2828
throw "Failed to stop web server: $stopErrorMessage"
@@ -32,16 +32,28 @@ function Kill-WebServer {
3232

3333
function Start-WebServer {
3434
Write-Output "Starting web server"
35-
Start-Job -ScriptBlock { dotnet run --project ..\src\Examples\GettingStarted\GettingStarted.csproj } | Out-Null
35+
$startTimeUtc = Get-Date -AsUTC
36+
$job = Start-Job -ScriptBlock {
37+
dotnet run --project ..\src\Examples\GettingStarted\GettingStarted.csproj --configuration Debug --property:TreatWarningsAsErrors=True --urls=http://0.0.0.0:14141
38+
}
3639

3740
$webProcessId = $null
41+
$timeout = [timespan]::FromSeconds(30)
42+
3843
Do {
3944
Start-Sleep -Seconds 1
45+
$hasTimedOut = ($(Get-Date -AsUTC) - $startTimeUtc) -gt $timeout
4046
$webProcessId = Get-WebServer-ProcessId
41-
} While ($webProcessId -eq $null)
47+
} While ($webProcessId -eq $null -and -not $hasTimedOut)
48+
49+
if ($hasTimedOut) {
50+
Write-Host "Failed to start web server, dumping output."
51+
Receive-Job -Job $job
52+
throw "Failed to start web server."
53+
}
4254
}
4355

44-
Kill-WebServer
56+
Stop-WebServer
4557
Start-WebServer
4658

4759
try {
@@ -55,10 +67,10 @@ try {
5567
& $scriptFile.FullName > .\request-examples\$jsonFileName
5668

5769
if ($LastExitCode -ne 0) {
58-
throw [System.Exception] "Example request from '$($scriptFile.Name)' failed with exit code $LastExitCode."
70+
throw "Example request from '$($scriptFile.Name)' failed with exit code $LastExitCode."
5971
}
6072
}
6173
}
6274
finally {
63-
Kill-WebServer
75+
Stop-WebServer
6476
}

0 commit comments

Comments
 (0)