On our build server, we have a running process that, among other things, are necessary for running integration tests, to gain access to a database.
This piece of software can be changed in our repository, and I have therefore created a CI build that builds the changes. What I would then like to do, is to restart the process with the new compiled version upon successful build, but that part I can not seem to get to work.
I have no issue killing the running process, and deploying the result to specific location on the build server, but seemingly no matter what I try, the process I spawn is killed as soon as the running build ends.
I have tried the following:
With PowerShell
Start-Process <path to file>
With CMD
with 'cmd' as tool and the following arguments:
<path to file>
start <path to file>
/c start <path to file>
cmd <path to file>
cmd start <path to file>
cmd /c start <path to file>
I have also tried simply supplying the .exe path as the tool name, and no arguments, no luck either.
How well did it work?
Well, for most of the above approaches, an extra step with the PS command Get-Process <exe name>*
I got the result that the process was running. The same step yielded no results after a step that stopped the process, so a new updated one could be started. So I am positive it works, vNext build simply kills it all after the build ends.
Other solutions
I have got 2 solutions left that I can think of should work, but both solutions are too complicated for my liking, in that I am introducing quite the complexity to the build process that then might go wrong, but here goes:
Again, I would rather not use any of the 2 solutions, if a better exists. :)
At the moment I ended up fixing it by installing AutoIt, and added this simple script to the "deploy" folder:
While 1 ; Opens up a WHILE loop, with 1 as a constant, so it is infinite
If Not ProcessExists("DelphiDebug.exe") Then Run("DelphiDebug.exe") ; if the process of DelphiDebug.exe doesn't exist, it starts it
Sleep (10) ; Puts the script to sleep for 10 milliseconds so it doesn't chew CPU power
WEnd ; Closes the loop, tells it to go back to the beginning
Credit goes to this forum entry where a notepad example was made.
I have then made a PowerShell script that renames the current version, copies the newly build version to "deploy" folder, stops the running instance, and deletes the renamed version:
# CONSTANTS AND CALCULATED VARIABLES
[string]$deploymentFolder = 'C:\Deployment-folder-on-local-machine'
[string]$deploymentPath = "$deploymentFolder\my-program.exe"
[string]$searchPathExistingVersion = $deploymentPath + '*' # The star is important so Get-Item does not throw an error
[string]$toDeleteExeName = 'please-delete.exe'
[string]$newCompiledVersionFullPath = "$env:BUILD_SOURCESDIRECTORY\sub-path-to-bin-folder\my-program.exe"
[string]$processName = 'my-program'
[string]$searchPathToDeleteVersion = "$deploymentFolder\$toDeleteExeName*" # The star is important so Get-Item does not throw an error
# EXECUTION
Write-Verbose "Search path: $searchPathExistingVersion"
$existingVersion = Get-Item $searchPathExistingVersion;
if ($existingVersion) {
Write-Debug "Match found: $existingVersion"
Rename-Item $existingVersion.FullName $toDeleteExeName
} else {
Write-Debug 'No existing file found'
}
Write-Verbose "Copy new version from path: $newCompiledVersionFullPath"
Copy-Item $newCompiledVersionFullPath $deploymentPath
Write-Verbose 'Stopping running processes'
Get-Process ($processName + '*') | Stop-Process # The new version is auto started with a running AutoIt script in the deployment folder.
Write-Verbose "Deleting old versions with search path: $searchPathToDeleteVersion"
$toDeleteVersion = Get-Item $searchPathToDeleteVersion
if ($toDeleteVersion) {
Write-Debug "Match found: $toDeleteVersion"
Remove-Item $toDeleteVersion -ErrorAction SilentlyContinue # Deletion is not critical. Next time the build runs, it will attempt another cleanup.
} else {
Write-Debug 'No file found to delete'
}
Again, this solution added another complexity to the server, so I am keeping the question open for a few days, to see if there are simpler solutions. :)