From 52672d252dfd270bc2a981510c3320e72bd4cf40 Mon Sep 17 00:00:00 2001 From: Ronald Chaplin Date: Mon, 8 Jul 2013 14:50:02 -0700 Subject: [PATCH 1/4] Implemented forever option Implemented forever option to replace upstart. Upstart is still available, however forever is a better/easier solution for some people/apps. There are 4 new config settings as defined starting at line 54. run_method set this to 'forever' to use forever spin_sleep_time miliseconds for spinup time min_up_time minimum uptime max_run max number of times forever will try to restart --- lib/capistrano/node-deploy.rb | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/capistrano/node-deploy.rb b/lib/capistrano/node-deploy.rb index b07d28a..e830e21 100644 --- a/lib/capistrano/node-deploy.rb +++ b/lib/capistrano/node-deploy.rb @@ -50,6 +50,12 @@ def remote_file_differs?(full_path, content) cd #{current_path} && exec sudo -u #{node_user} NODE_ENV=#{node_env} #{app_environment} #{node_binary} #{current_path}/#{app_command} 2>> #{shared_path}/#{node_env}.err.log 1>> #{shared_path}/#{node_env}.out.log end script EOD + + #Forever related settings + set :run_method, "upstart" unless defined? run_method + set :spin_sleep_time, "1000" unless defined? spin_sleep_time + set :min_up_time, "1000" unless defined? min_up_time + set :max_run, "5" unless defined? max_run } @@ -59,7 +65,7 @@ def remote_file_differs?(full_path, content) run "mkdir -p #{shared_path}/node_modules" run "cp #{release_path}/package.json #{shared_path}" run "cp #{release_path}/npm-shrinkwrap.json #{shared_path}" - run "cd #{shared_path} && npm install #{(node_env != 'production') ? '--dev' : ''} --loglevel warn" + run "cd #{shared_path} && npm install #{(node_env == 'production') ? '--production' : ''} --loglevel warn" run "ln -s #{shared_path}/node_modules #{release_path}/node_modules" end @@ -78,20 +84,37 @@ def remote_file_differs?(full_path, content) sudo "cp #{temp_config_file_path} #{upstart_file_path}" end + desc "Check the status of forever" + task :status do + run "forever list" + end + desc "Start the node application" task :start do - sudo "start #{upstart_job_name}" + if run_method == 'forever' + run "cd #{current_path} && forever -c '#{node_binary}' -m #{max_run} --minUptime #{min_up_time} --spinSleepTime #{spin_sleep_time} -o #{shared_path}/console.log -e #{shared_path}/error.log start #{current_path}/#{app_command}" + else + sudo "start #{upstart_job_name}" + end end desc "Stop the node application" task :stop do - sudo "stop #{upstart_job_name}" + if run_method == 'forever' + run "forever stop #{current_path}/#{app_command}" + else + sudo "stop #{upstart_job_name}" + end end desc "Restart the node application" task :restart do - sudo "stop #{upstart_job_name}; true" - sudo "start #{upstart_job_name}" + if run_method == 'forever' + run "forever restart #{current_path}/#{app_command}" + else + sudo "stop #{upstart_job_name}; true" + sudo "start #{upstart_job_name}" + end end end From da03480af281046d30fe48b84994d5f34a73d6a7 Mon Sep 17 00:00:00 2001 From: Ronald Chaplin Date: Mon, 8 Jul 2013 15:00:40 -0700 Subject: [PATCH 2/4] Adjusted to reflect node_env logic --- lib/capistrano/node-deploy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano/node-deploy.rb b/lib/capistrano/node-deploy.rb index e830e21..fca28f1 100644 --- a/lib/capistrano/node-deploy.rb +++ b/lib/capistrano/node-deploy.rb @@ -65,7 +65,7 @@ def remote_file_differs?(full_path, content) run "mkdir -p #{shared_path}/node_modules" run "cp #{release_path}/package.json #{shared_path}" run "cp #{release_path}/npm-shrinkwrap.json #{shared_path}" - run "cd #{shared_path} && npm install #{(node_env == 'production') ? '--production' : ''} --loglevel warn" + run "cd #{shared_path} && npm install #{(node_env == 'production') ? '--dev' : ''} --loglevel warn" run "ln -s #{shared_path}/node_modules #{release_path}/node_modules" end From 186c4ca4ccc423c9211af7ef5fd8af6c7f99c7fa Mon Sep 17 00:00:00 2001 From: Ronald Chaplin Date: Mon, 8 Jul 2013 15:02:06 -0700 Subject: [PATCH 3/4] Adjusted to reflect node_env logic --- lib/capistrano/node-deploy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano/node-deploy.rb b/lib/capistrano/node-deploy.rb index fca28f1..d6d8aed 100644 --- a/lib/capistrano/node-deploy.rb +++ b/lib/capistrano/node-deploy.rb @@ -65,7 +65,7 @@ def remote_file_differs?(full_path, content) run "mkdir -p #{shared_path}/node_modules" run "cp #{release_path}/package.json #{shared_path}" run "cp #{release_path}/npm-shrinkwrap.json #{shared_path}" - run "cd #{shared_path} && npm install #{(node_env == 'production') ? '--dev' : ''} --loglevel warn" + run "cd #{shared_path} && npm install #{(node_env != 'production') ? '--dev' : ''} --loglevel warn" run "ln -s #{shared_path}/node_modules #{release_path}/node_modules" end From 657f8ff65861c0292014e68b5b2b0ae1ba907f25 Mon Sep 17 00:00:00 2001 From: Ronald Chaplin Date: Mon, 8 Jul 2013 15:19:14 -0700 Subject: [PATCH 4/4] Update README.md --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2318911..b3bcb56 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Features - Installs node packages (`npm install`) during deploys, using a shared folder for speed - Automatically creates upstart scripts for your node apps - Provides tasks for starting (`cap node:start`) and stopping (`cap node:stop`) your node app - +- Forever functionality and remote monitor (`cap node:status`) Usage ----- @@ -64,6 +64,21 @@ set :node_user, "james" # Set the name of the upstart command (defaults to #{application}-#{node_env}) set :upstart_job_name, "myserver" + +#Forever related settings + +# set to forever to use forever, defaults to upstart +set :run_method, "forever" + +#Time to wait (millis) between launches of a spinning script. +set :spin_sleep_time, "1000" + +#Minimum uptime (millis) for a script to not be considered "spinning" +set :min_up_time, "1000" + +#Only run the specified script MAX times +set :max_run, "5" + ```