From 6edbb95f538cdda9beeb8d97eb30c6756ee5e053 Mon Sep 17 00:00:00 2001 From: Alexion Ramos Date: Wed, 26 Jun 2019 13:19:22 -0700 Subject: [PATCH] Wait for StackdriverLogging service to stop before restarting it. --- cluster/gce/windows/k8s-node-setup.psm1 | 30 +++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/cluster/gce/windows/k8s-node-setup.psm1 b/cluster/gce/windows/k8s-node-setup.psm1 index f8c5ef9d553..7c0cd38be5e 100644 --- a/cluster/gce/windows/k8s-node-setup.psm1 +++ b/cluster/gce/windows/k8s-node-setup.psm1 @@ -1113,13 +1113,39 @@ $STACKDRIVER_ROOT = 'C:\Program Files (x86)\Stackdriver' # sometimes is unstoppable, so we work around it by killing the processes. function Restart-StackdriverLoggingAgent { Stop-Service -NoWait -ErrorAction Ignore StackdriverLogging - # TODO: check periodically to lower the wait time - Start-Sleep 10 + + # Wait (if necessary) for service to stop. + $timeout = 10 + $stopped = (Get-service StackdriverLogging).Status -eq 'Stopped' + for ($i = 0; $i -lt $timeout -and !($stopped); $i++) { + Start-Sleep 1 + $stopped = (Get-service StackdriverLogging).Status -eq 'Stopped' + } + if ((Get-service StackdriverLogging).Status -ne 'Stopped') { # Force kill the processes. Stop-Process -Force -PassThru -Id (Get-WmiObject win32_process | Where CommandLine -Like '*Stackdriver/logging*').ProcessId + + # Wait until process has stopped. + $waited = 0 + $log_period = 10 + $timeout = 60 + while ((Get-service StackdriverLogging).Status -ne 'Stopped' -and $waited -lt $timeout) { + Start-Sleep 1 + $waited++ + + if ($waited % $log_period -eq 0) { + Log-Output "Waiting for StackdriverLogging service to stop" + } + } + + # Timeout occurred + if ($waited -ge $timeout) { + Throw ("Timeout while waiting for StackdriverLogging service to stop") + } } + Start-Service StackdriverLogging }