3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/capistrano-daemonize/daemonize.rb', line 3
def daemonize(command, options)
raise "Must pass a hash containing 'as'" if not options.is_a?(Hash) or
not options.has_key?(:as)
name = options.delete(:as)
opts = Hash[[:pidfile, :logfile, :chdir, :user].map do
|option| [option, options.delete(option)]
end]
callbacks = options.delete(:callbacks)
sudo_command = ''
before(%w(start stop restart).map { |action| "#{name}:#{action}" }) do
_cset(:rails_env) { fetch(:rails_env) || 'production' }
_cset(:daemonize_pidfile) do
opts[:pidfile] || "#{fetch(:shared_path)}/pids/#{name}.pid"
end
_cset(:daemonize_logfile) do
opts[:logfile] || "#{fetch(:shared_path)}/log/#{name}.log"
end
_cset(:daemonize_chdir) { opts[:chdir] || fetch(:current_path) }
_cset(:daemonize_user) { opts[:user] || fetch(:user) }
_cset(:daemonize_sudo) do
opts[:user] == fetch(:user) && try_sudo || ''
end
end
namespace name do
task :start, { desc: "Start #{name}" }.merge(options) do
run "if [ -e \#{daemonize_pidfile} ]; then\n echo 'pidfile exists';\n exit 1;\nfi;\n\n\#{daemonize_sudo} /sbin/start-stop-daemon --pidfile \#{daemonize_pidfile} \\\n--start --make-pidfile --chdir \#{daemonize_chdir} --user \#{daemonize_user} \\\n--background --exec \#{command.split[0]} -- \#{command.split[1..-1].join(' ')} \\\n2>&1 >>\#{daemonize_logfile} RAILS_ENV=\#{rails_env};\nsleep 1\n SCRIPT\n end\n\n task :stop, { desc: \"Stop \#{name}\" }.merge(options) do\n run <<-SCRIPT\n\#{sudo_command} /sbin/start-stop-daemon --stop --pidfile \#{daemonize_pidfile};\nrm -f \#{daemonize_pidfile}\n SCRIPT\n end\n\n task :restart, { desc: \"Restart \#{name}\" }.merge(options) do\n stop\n start\n end\n end\n\n if callbacks\n after 'deploy:start', \"\#{name}:start\"\n after 'deploy:stop', \"\#{name}:stop\"\n after 'deploy:restart', \"\#{name}:restart\"\n end\nend\n"
|