-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_api_smoke.ps1
More file actions
90 lines (90 loc) · 2.87 KB
/
Copy pathrun_api_smoke.ps1
File metadata and controls
90 lines (90 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
$ErrorActionPreference='Continue'
$artifact='dist/server.js'
if (-not (Test-Path $artifact)) {
Write-Output "ARTIFACT_MISSING:$artifact"
exit 1
}
$stdout=Join-Path $PWD 'api_smoke_stdout.log'
$stderr=Join-Path $PWD 'api_smoke_stderr.log'
Remove-Item $stdout,$stderr -ErrorAction SilentlyContinue
$proc=Start-Process -FilePath node -ArgumentList $artifact -PassThru -RedirectStandardOutput $stdout -RedirectStandardError $stderr -WorkingDirectory $PWD
Write-Output "STARTED_PID:$($proc.Id)"
$started=$false
for ($i=0; $i -lt 20; $i++) {
Start-Sleep -Milliseconds 500
$proc.Refresh()
if ($proc.HasExited) { break }
try {
$null=Invoke-WebRequest -Uri 'http://127.0.0.1:3000/health' -UseBasicParsing -TimeoutSec 2
$started=$true
break
} catch { }
}
$proc.Refresh()
if ($proc.HasExited) {
Write-Output "STARTUP_FAILED:Process exited with code $($proc.ExitCode)"
Write-Output 'STDERR_BEGIN'
if (Test-Path $stderr) { Get-Content $stderr -Tail 200 }
Write-Output 'STDERR_END'
Write-Output 'STDOUT_BEGIN'
if (Test-Path $stdout) { Get-Content $stdout -Tail 200 }
Write-Output 'STDOUT_END'
Write-Output 'PROBES_SKIPPED:true'
exit 0
}
if ($started) { Write-Output 'STARTUP_CONFIRMED:true' } else { Write-Output 'STARTUP_UNCONFIRMED:true' }
$urls=@(
'http://127.0.0.1:3000/health',
'http://127.0.0.1:3000/ready',
'http://127.0.0.1:3000/metrics',
'http://127.0.0.1:3000/admin/system-health'
)
foreach ($u in $urls) {
try {
$resp=Invoke-WebRequest -Uri $u -UseBasicParsing -TimeoutSec 5 -MaximumRedirection 0
$body=$resp.Content
if ($null -eq $body) { $body='' }
$snippet=($body.Substring(0, [Math]::Min(200, $body.Length)) -replace "`r|`n", ' ')
Write-Output "PROBE:$u STATUS:$([int]$resp.StatusCode) OK:true BODY:$snippet"
} catch {
$ex=$_.Exception
$status=''
$body=''
if ($ex.Response) {
try { $status=[int]$ex.Response.StatusCode } catch { $status='' }
try {
$stream=$ex.Response.GetResponseStream()
if ($stream) {
$reader=New-Object System.IO.StreamReader($stream)
$body=$reader.ReadToEnd()
$reader.Close()
}
} catch { }
}
$msg=($ex.Message -replace "`r|`n", ' ')
$snippet=''
if ($body) { $snippet=($body.Substring(0, [Math]::Min(200, $body.Length)) -replace "`r|`n", ' ') }
Write-Output "PROBE:$u STATUS:$status OK:false ERROR:$msg BODY:$snippet"
}
}
if (-not $proc.HasExited) {
try {
Stop-Process -Id $proc.Id
Start-Sleep -Milliseconds 500
} catch {
Write-Output "STOP_ERROR:$($_.Exception.Message)"
}
}
$proc.Refresh()
Write-Output "PROCESS_EXITED:$($proc.HasExited)"
if (-not $proc.HasExited) {
try {
$proc.Kill()
Write-Output 'KILLED:true'
} catch {
Write-Output "KILL_ERROR:$($_.Exception.Message)"
}
}
Write-Output 'STDERR_TAIL_BEGIN'
if (Test-Path $stderr) { Get-Content $stderr -Tail 80 }
Write-Output 'STDERR_TAIL_END'