Deploy NodeJS app with pm2 and Capistrano - node.js

I develop project based on NodeJs, pm2, Capistrano 3.
Faced with problem of downtime while deploying Node app with Capistrano.
deploy.rb:
set :linked_dirs, ['node_modules', 'logs']
set :linked_files, ['ecosystem.json']
set :npm_flags, '--silent --no-spin'
before 'deploy:updated', 'assets:upload'
after 'deploy:updated', 'assets:webpack'
after 'deploy:publishing', 'pm2:restart'
assets:upload - builds js and css files and uploads to CDN. Build performs with Webpack so it's create webpack-assets.json.
assets:webpack - uploads webpack-assets.json to prod servers. webpack-assets.json is using by node to get exact asset name because it contains hash:
task :webpack do
run_locally do
roles(:web).each do |host|
execute :rsync, '-rvzu', "themes-assets.json", "#{host.user}##{host.hostname}:#{fetch(:release_path)}"
execute :rsync, '-rvzu', "webpack-assets.json", "#{host.user}##{host.hostname}:#{fetch(:release_path)}"
end
end
end
pm2:restart - should perform zero time reload. But in fact I'm getting 1second down time. If I perform this task independently there is no downtime.
def restart_app
within current_path do
execute :pm2, :startOrRestart, fetch(:deploy_to) + '/shared/ecosystem.json'
end
end
pm2 logs show the following error
Process with pid 123169 still not killed, retrying...

Instead of
pm2 startOrRestart <app|conf>
You have to use
pm2 startOrReload <app|conf>
If you still see a downtime while using "startOrReload", have a look at: http://pm2.keymetrics.io/docs/usage/signals-clean-restart/#graceful-start

Related

Google Cloud Run Second Flask Application - requirements.txt issue

I have a google cloud run flask application named "HelloWorld1" already up and running however i need to create a second flask application. I followed the below steps as per documentation:
1- On "Cloud Shell Editor" clicked "<>Cloud Code" --> "New Application" --> "Cloud Run Application Basic Cloud Run Application .."-->"Python (Flask): Cloud Run", provide and new folder and application is created.
2- When i try to run it using "Run on Cloud Run Emulator" i get the following error:
Starting to run the app using configuration 'Cloud Run: Run/Debug Locally' from .vscode/launch.json...
To view more detailed logs, go to Output channel : "Cloud Run: Run/Debug Locally - Detailed"
Dependency check started
Dependency check succeeded
Starting minikube, this may take a while...................................
minikube successfully started
The minikube profile 'cloud-run-dev-internal' has been scheduled to stop automatically after exiting Cloud Code. To disable this on future deployments, set autoStop to false in your launch configuration /home/mian/newapp/.vscode/launch.json
Update initiated
Update failed with error code DEVINIT_REGISTER_BUILD_DEPS
listing files: file pattern [requirements.txt] must match at least one file
Skaffold exited with code 1.
Cleaning up...
Finished clean up.
I tried following:
1- tried to create different type of application e.g django instead of flask however always getting the same error
2- tried to give full path of [requirements.txt] in docker settings, no luck.
Please if someone help me understanding why i am not able to run a second cloud run Flask app due to this error?
It's likely that your Dockerfile references the 'requirements.txt' file, but that file is not in your local directory. So, it gives the error that it's missing:
listing files: file pattern [requirements.txt] must match at least one file

how to detect what triggered PM2 watch restart

Is there some kind of debug option to find out which file exactly was changed and caused PM2 to restart when it's used with --watch key?
At least in my version 4.5.5 it is quite simple - just show the logs. But the trick is that you should not show the log from the process itself. Therefore just execute pm2 logs without specifying the process.
What you will get is something like this:
PM2 | Change detected on path fallback-backend/logger/logfiles/info.log for app SmartProduction-Fallback - restarting

Correct way to restar/reload application for a different release

I have following folder structure:
current
releases
2192091029019/
1029012901920/
Latest release gets pushed to current folder, and I afterwards start it wiht pm2 start, however If I upload new release with different folder name and do pm2 reload from new folder it still trys to reference original release from where application was started. Is there a way to restart application respecting new code?
I have same problem with this release structure but with supervisord+Rails instead pm2 + node.
In my case i need to completely restart supervisord every deploy to fix that.
So in your case it may work like this:
pm2 stop
kill -SIGTERM {pm2_pid}
pm2 startup
It's hackish but working solution.

Custom remote task is not executed with capistrano 3

I run into a weird issue with capistrano 3 and brunch.
I want to execute brunch on remote server but nothing happen. My custom remote task looks like this:
namespace :brunch do
desc "Building assets with brunch.io"
task :build do
on roles(:web) do
within "#{release_path}" do
execute "node #{release_path}/node_modules/brunch/bin/brunch build --env=#{fetch(:stage)} #{release_path}"
end
end
end
end
When I run "cap staging deploy", I can see command is executed:
INFO [a246858c] Running node /releases/20160303145521/node_modules/brunch/bin/brunch build --env=staging /releases/20160303145521 as web
INFO [a246858c] Finished in 0.159 seconds with exit status 0 (successful).
But my assets are not built, nothing is done.
And if I connect on my server run command, everything works fine.
I don't understand this behaviour, is any one aware of that?
Thanks a lot for your help
I'm using Capistrano Version: 3.4.0 (Rake Version: 10.5.0)

Jenkins nodejs pulgin long running running pm2 deamons

At the end of my Jenkins build I use the nodejs plugin to kick off pm2 to spawn a few deamons. When the build finishes I get the message:
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
Looking at this, and the corrosponding https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller it suggests that I can run something like:
BUILD_ID=dontKillMe pm2 start processes.json
But the deamons are not running after the build completes. Any ideas?
Thanks, Chris

Resources