lib/capistrano/tasks/nginx-unit.rake in capistrano-nginx-unit-0.3 vs lib/capistrano/tasks/nginx-unit.rake in capistrano-nginx-unit-0.4
- old
+ new
@@ -11,11 +11,10 @@
set :nginx_unit_group, -> { nil }
set :nginx_unit_script, -> { "config.ru" }
end
end
-# TODO: Stop NGINX Unit process
namespace :nginx_unit do
desc "Start NGINX Unit process"
task :start do
on release_roles(fetch(:nginx_unit_roles)) do
pid_file = fetch(:nginx_unit_pid)
@@ -28,10 +27,23 @@
fetch(:nginx_unit_options)
end
end
end
+ # NOTE: Should we detach listener and application before killing?
+ desc "Stop NGINX Unit process"
+ task :stop do
+ on release_roles(fetch(:nginx_unit_roles)) do
+ pid_file = fetch(:nginx_unit_pid)
+ if test("[ -e #{pid_file} ] && kill -0 `cat #{pid_file}`")
+ execute :sudo, :kill, "-s QUIT `cat #{pid_file}`"
+ else
+ info "NGINX Unit is already stopped"
+ end
+ end
+ end
+
# If you want to apply new code when deployed,
# please invoke this task after deploy:published
desc "Attach listener and application configuration to NGINX Unit"
task :attach do
invoke "nginx_unit:start"
@@ -72,18 +84,30 @@
end
desc "Detach listener configuration from NGINX Unit"
task :detach_listener do
on release_roles(fetch(:nginx_unit_roles)) do
- control_nginx_unit(:delete, path: "/listeners/#{fetch(:nginx_unit_listen)}")
+ listen = fetch(:nginx_unit_listen)
+
+ if nginx_unit_conf["listeners"][listen]
+ control_nginx_unit(:delete, path: "/listeners/#{listen}")
+ else
+ info "Listener \"#{listen}\" is already detached"
+ end
end
end
desc "Detach application configuration from NGINX Unit"
task :detach_app do
on release_roles(fetch(:nginx_unit_roles)) do
- control_nginx_unit(:delete, path: "/applications/#{fetch(:nginx_unit_app_name)}")
+ app_name = fetch(:nginx_unit_app_name)
+
+ if nginx_unit_conf["applications"][app_name]
+ control_nginx_unit(:delete, path: "/applications/#{app_name}")
+ else
+ info "Application \"#{app_name}\" is already detached"
+ end
end
end
# Send request to NGINX Unit control socket
def control_nginx_unit(method, path: "", json: nil)
@@ -95,7 +119,16 @@
]
args << "-d '#{json}'" if json
execute :curl, *args
+ end
+
+ # Get current configuration
+ def nginx_unit_conf
+ JSON.parse(capture(
+ :curl,
+ "--unix-socket #{fetch(:nginx_unit_control_sock)}",
+ "http://localhost/"
+ ))
end
end