這是最貧民窟的黑客。我基本上在做的是查看執行Jenkins CLI jar文件的java命令的錯誤輸出,如果錯誤輸出包含「Failed to connect」,我認爲Jenkins已被關閉。
我很想看看有沒有人有更優美的解決方案,造成男人,這只是毛病。
while(!$JenkinsIsDown)
{
Write-Host "Getting the session ID from Jenkins to check if it's down..."
#This is a terrible hack so we can process the output of the command
$JenkinsProcess = New-Object System.Diagnostics.ProcessStartInfo
$JenkinsProcess.FileName = "java"
$JenkinsProcess.RedirectStandardError = $true
$JenkinsProcess.RedirectStandardOutput = $true
$JenkinsProcess.UseShellExecute = $false
$JenkinsProcess.Arguments = "-jar " + $JenkinsCLILocation + " -s http://" + $JenkinsURL + " session-id"
$JenkinsProcess.WaitForExit()
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $JenkinsProcess
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
if($stderr.Contains("Failed to connect"))
{
Write-Host "The Jenkins is down."
$JenkinsIsDown = $true
}
}