Module: Daemon::Controller
- Defined in:
- lib/inari/daemon.rb
Class Method Summary collapse
- .daemonize(daemon) ⇒ Object
- .remove_pid_file(daemon) ⇒ Object
-
.start(daemon) ⇒ Object
Create a process for this task.
-
.stop(daemon) ⇒ Object
Stop all tasks.
Class Method Details
.daemonize(daemon) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/inari/daemon.rb', line 32 def self.daemonize(daemon) case !ARGV.empty? && ARGV[0] when 'start' start(daemon) when 'stop' puts 'Cleaning up all processes.' stop(daemon) puts 'Stopped.' exit when 'restart' stop(daemon) start(daemon) else puts 'Invalid command. Please specify start, stop or restart.' exit end end |
.remove_pid_file(daemon) ⇒ Object
85 86 87 |
# File 'lib/inari/daemon.rb', line 85 def self.remove_pid_file(daemon) FileUtils.rm(daemon.pid_file_name) end |
.start(daemon) ⇒ Object
Create a process for this task.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/inari/daemon.rb', line 51 def self.start(daemon) fork do # Become session leader Process.setsid # Zap session leader exit if fork # Store the pid PidFile.store(daemon, Process.pid) # Change directory Dir.chdir Dir.tmpdir redirect_logs if ARGV.empty? and ARGV[1] != 'debug' trap('TERM') {daemon.stop; exit} daemon.start end end |
.stop(daemon) ⇒ Object
Stop all tasks.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/inari/daemon.rb', line 70 def self.stop(daemon) puts 'Stopping all tasks...' if !File.file?(daemon.pid_file_name) puts "Pid file not found (#{daemon.pid_file_name}). Is the daemon running?" exit end (PidFile.recall(daemon) or []).each do |pid| pid && Process.kill('TERM', pid) rescue Errno::ESRCH end remove_pid_file(daemon) puts 'Tasks stopped.' end |