forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke_cargo_with_timeout.ps1
More file actions
54 lines (43 loc) · 1.29 KB
/
Copy pathinvoke_cargo_with_timeout.ps1
File metadata and controls
54 lines (43 loc) · 1.29 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
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$CargoArgs,
[ValidateRange(1, 86400)]
[int]$TimeoutSeconds = 300
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Stop-ProcessTree {
param(
[Parameter(Mandatory=$true)]
[System.Diagnostics.Process]$Process
)
if ($Process.HasExited) {
return
}
$taskkill = Get-Command taskkill.exe -ErrorAction SilentlyContinue
if ($taskkill) {
& $taskkill.Source /PID $Process.Id /T /F | ForEach-Object { Write-Host $_ }
return
}
Stop-Process -Id $Process.Id -Force -ErrorAction SilentlyContinue
}
$timeoutMilliseconds = $TimeoutSeconds * 1000
Write-Host "::group::$Name"
try {
Write-Host "cargo $($CargoArgs -join ' ')"
$process = Start-Process -FilePath 'cargo' -ArgumentList $CargoArgs -NoNewWindow -PassThru
if (-not $process.WaitForExit($timeoutMilliseconds)) {
Stop-ProcessTree -Process $process
throw "$Name timed out after $TimeoutSeconds seconds"
}
$exitCode = $process.ExitCode
if ($exitCode -ne 0) {
throw "$Name failed with exit code $exitCode"
}
} finally {
Write-Host '::endgroup::'
}