Powershell script never finishes

Hello, I am deploying a Node.js application to a single Windows machine using Octopus 3.2.19 and am attempting to start it using forever. Here is my deployment script:

npm install forever
node node_modules/forever/bin/forever stopall
node node_modules/forever/bin/forever start ./main.js
Write-Host "Done."

Here is the output from the last three commands:

10:38:23 info:    No forever processes running

10:38:24 info:    Forever processing file: ./main.js

10:38:24 Done.

The script is successful and my application starts running. However, the build task never finishes. It will remain on this task until the deployment is cancelled, and after cancelling, subsequent deployments indicate that the tentacle is busy, and a service restart/machine reboot is required before another deployment can start.

I have run my script on the machine via RDP using Calamari run-script, and the script processes successfully and exits as expected.

While debugging, I ran the following deployment script:

npm install forever
node node_modules/forever/bin/forever stopall
node node_modules/forever/bin/forever start ./main.js
node node_modules/forever/bin/forever stopall
Write-Host "Done."

Notice that forever is now instructed to stop all scripts after starting my script. All commands execute and return as expected, however, the task now does finish. This leads me to believe that Octopus is detecting the process that forever is creating, and is expecting it to end before it allows the build task to finish.

I am looking for guidance on solving this issue, I can’t seem to make any progress with it. Any suggestions are welcome. Thanks!

Hi,

Thanks for getting in touch. I agree, it appears that forever is keeping the powershell session/process active so it never exits.

Can you try the following as it seem to work for me.

npm install forever
Write-Host "Stopping all running forever processes..."
node .\node_modules\forever\bin\forever stopall

Write-Host "Starting main.js as new forever process..."
$node = "node.exe"
$args = ".\node_modules\forever\bin\forever start ./main.js
Start-Process $node $args

Let me know how you go.

Rob

Hi Rob,

Thank you for your response, it solved my problem!

For posterity, here is my final build step:

Pre-deployment script

npm install forever -g

forever stopall

$dir = $OctopusParameters['Octopus.Action[xyz].Package.CustomInstallationDirectory']
Cmd /C "rmdir /S /Q $dir"

Deployment script

npm install

Start-Process "forever" "start ./main.js"

Octopus’ purge directory option fails on Windows because of npm’s node_modules nesting, so rmdir is used in pre-deployment.
Using .\node_modules\forever was a workaround that shouldn’t be used because this folder is cleaned on subsequent deployments. I was able to fix the npm PATH issue so global npm packages like forever can be used.

Thanks again for your help!

Hi,

Thank you for following-up with your experience as it helps others with similar problems. :slight_smile:

Happy deploying!

Rob