My windows service take about 5 minutes to stop and octopus fails

During production deployment the services take about 5 minutes to stop. This makes the copy/purging steps to fail since it says the service is being used. how to resolve this issue? can we use any script to wait the activity? I tried to even do that but then the script steps in too late

Hi,

Thanks for getting in touch!

We definitely would recommend using a script to wait for the service to stop. One potential way to complete this task would be to use a Script step which will both stop the service and wait to complete until the service has stopped, which should resolve the problem you have with your copy/purge steps. Below is an example script that will stop a service and then wait for it to report as stopped before completing:

$ServiceName = 'OctopusDeploy Tentacle'
$arrService = Get-Service -Name $ServiceName

while ($arrService.Status -eq 'Running')
{

    Stop-Service $ServiceName
    write-host $arrService.status
    write-host 'Service stopping'
    Start-Sleep -seconds 60
    $arrService.Refresh()
    if ($arrService.Status -eq 'Stopped')
    {
        Write-Host 'Service is now stopped'
    }

}

I hope that helps, please let me know if there is anything else that I can assist with,

Regards,

Alex

Hi Alex,
Thanks for posting the reply on the issue. Actually on further investigating my issue on the service taking time, we found that this particular service has too many internal connections which take a lot of time to stop.
For this purpose, we have implemented the following in the pre deployment step
function StopWaitGo($servicename)
{
Stop-Service -name $servicename
Write-host "Service $servicename has been stopped. Sleeping for 5 minutes to make sure all connections are stopped or killed"
Start-Sleep -m 100000
Write-host "1.6 Minutes over"
Start-Sleep -m 100000
Write-host "3.2 Minutes over"
Start-Sleep -m 100000
}
This has resolved the issue.

Hi,

Thanks for the update, glad to hear that you have resolved the issue!

Happy deploying,

Regards,

Alex