Skip to content

Commit 4f57d46

Browse files
committed
timeout changes for appnode
1 parent cbca0ec commit 4f57d46

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

win/common-utils.ps1

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ function Invoke-External {
124124
[Parameter(Mandatory)][string]$Exe,
125125
[Parameter()][string[]]$Arguments = @(),
126126
[string]$LogFile,
127-
[string]$WorkingDirectory
127+
[string]$WorkingDirectory,
128+
[int]$TimeoutSeconds = 0 # 0 = no timeout (current behaviour)
128129
)
130+
129131
$psi = New-Object System.Diagnostics.ProcessStartInfo
130132
$exeToRun = $Exe
131133
$argLine = ($Arguments | ForEach-Object { if ($_ -match '\s') { '"{0}"' -f $_ } else { $_ } }) -join ' '
@@ -142,8 +144,9 @@ function Invoke-External {
142144

143145
$psi.RedirectStandardOutput = $true
144146
$psi.RedirectStandardError = $true
145-
$psi.UseShellExecute = $false
146-
$psi.CreateNoWindow = $true
147+
$psi.UseShellExecute = $false
148+
$psi.CreateNoWindow = $true
149+
147150
if ([string]::IsNullOrWhiteSpace($WorkingDirectory)) {
148151
$psi.WorkingDirectory = (Get-Location).Path
149152
} else {
@@ -155,7 +158,9 @@ function Invoke-External {
155158

156159
if ($LogFile) {
157160
$logDir = Split-Path -Parent $LogFile
158-
if ($logDir -and !(Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir | Out-Null }
161+
if ($logDir -and !(Test-Path $logDir)) {
162+
New-Item -ItemType Directory -Path $logDir | Out-Null
163+
}
159164

160165
$stdoutAction = {
161166
if (-not [string]::IsNullOrEmpty($EventArgs.Data)) {
@@ -169,27 +174,51 @@ function Invoke-External {
169174
}
170175

171176
$stdoutEvent = Register-ObjectEvent -InputObject $p -EventName OutputDataReceived -Action $stdoutAction -MessageData $LogFile
172-
$stderrEvent = Register-ObjectEvent -InputObject $p -EventName ErrorDataReceived -Action $stderrAction -MessageData $LogFile
177+
$stderrEvent = Register-ObjectEvent -InputObject $p -EventName ErrorDataReceived -Action $stderrAction -MessageData $LogFile
173178

174179
[void]$p.Start()
175180
$p.BeginOutputReadLine()
176181
$p.BeginErrorReadLine()
177-
$p.WaitForExit()
182+
183+
if ($TimeoutSeconds -gt 0) {
184+
$waitMs = $TimeoutSeconds * 1000
185+
if (-not $p.WaitForExit($waitMs)) {
186+
try {
187+
Log-Line "⚠️ Process '$Exe $argLine' exceeded timeout of $TimeoutSeconds seconds. Killing it..." $GLOBAL_LOG
188+
} catch {}
189+
try { $p.Kill() } catch {}
190+
$p.WaitForExit()
191+
}
192+
} else {
193+
$p.WaitForExit()
194+
}
178195

179196
Unregister-Event -SourceIdentifier $stdoutEvent.Name
180197
Unregister-Event -SourceIdentifier $stderrEvent.Name
181198
Remove-Job -Id $stdoutEvent.Id -Force
182199
Remove-Job -Id $stderrEvent.Id -Force
183200
} else {
184201
[void]$p.Start()
185-
$stdout = $p.StandardOutput.ReadToEnd()
186-
$stderr = $p.StandardError.ReadToEnd()
187-
$p.WaitForExit()
202+
203+
if ($TimeoutSeconds -gt 0) {
204+
$waitMs = $TimeoutSeconds * 1000
205+
if (-not $p.WaitForExit($waitMs)) {
206+
try { $p.Kill() } catch {}
207+
$p.WaitForExit()
208+
}
209+
$stdout = $p.StandardOutput.ReadToEnd()
210+
$stderr = $p.StandardError.ReadToEnd()
211+
} else {
212+
$stdout = $p.StandardOutput.ReadToEnd()
213+
$stderr = $p.StandardError.ReadToEnd()
214+
$p.WaitForExit()
215+
}
188216
}
189217

190218
return $p.ExitCode
191219
}
192220

221+
193222
function Get-MavenCommand {
194223
param([Parameter(Mandatory)][string]$RepoDir)
195224
$mvnCmd = Get-Command mvn -ErrorAction SilentlyContinue

win/env-setup-run.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,19 @@ function Setup-Mobile-NodeJS {
500500

501501
# ---- npm run test ----
502502
Print-TestsRunningSection -Command "npm run test"
503-
Log-Line "ℹ️ About to run tests: cmd.exe /c npm run test" $GLOBAL_LOG
504-
Log-Line "ℹ️ Working directory for tests: $testDir" $GLOBAL_LOG
503+
504+
$testTimeout = 300 # 15 minutes hard cap for CI
505+
Log-Line "ℹ️ Starting 'npm run test' with timeout of $testTimeout seconds..." $GLOBAL_LOG
506+
507+
$exit = Invoke-External `
508+
-Exe "cmd.exe" `
509+
-Arguments @("/c","npm","run","test") `
510+
-LogFile $LogFile `
511+
-WorkingDirectory $testDir `
512+
-TimeoutSeconds $testTimeout
513+
514+
Log-Line "ℹ️ 'npm run test' finished with exit code: $exit" $GLOBAL_LOG
515+
Log-Line "ℹ️ Run Test command completed." $GLOBAL_LOG
505516

506517
$testStart = Get-Date
507518
$testExit = Invoke-External -Exe "cmd.exe" -Arguments @("/c","npm","run","test") -LogFile $LogFile -WorkingDirectory $testDir

0 commit comments

Comments
 (0)