Class: Invoker::Daemon
- Inherits:
-
Object
- Object
- Invoker::Daemon
- Defined in:
- lib/invoker/daemon.rb
Overview
rip off from borg github.com/code-mancers/borg/blob/master/lib/borg/borg_daemon.rb
Instance Attribute Summary collapse
-
#process_name ⇒ Object
readonly
Returns the value of attribute process_name.
Instance Method Summary collapse
- #daemonize ⇒ Object
-
#dead? ⇒ Boolean
pidfile exists but process isn’t running.
-
#initialize ⇒ Daemon
constructor
A new instance of Daemon.
- #kill_process ⇒ Object
- #log_file ⇒ Object
- #pid ⇒ Object
- #pid_file ⇒ Object
- #pidfile_exists? ⇒ Boolean
- #process_running? ⇒ Boolean
- #running? ⇒ Boolean
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Daemon
Returns a new instance of Daemon.
7 8 9 |
# File 'lib/invoker/daemon.rb', line 7 def initialize @process_name = 'invoker' end |
Instance Attribute Details
#process_name ⇒ Object (readonly)
Returns the value of attribute process_name.
5 6 7 |
# File 'lib/invoker/daemon.rb', line 5 def process_name @process_name end |
Instance Method Details
#daemonize ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/invoker/daemon.rb', line 38 def daemonize if fork sleep(2) exit(0) else Process.setsid File.open(pid_file, "w") do |file| file.write(Process.pid.to_s) end Invoker::Logger.puts "Invoker daemon log is available at #{log_file}" redirect_io(log_file) $0 = process_name end end |
#dead? ⇒ Boolean
pidfile exists but process isn’t running
80 81 82 |
# File 'lib/invoker/daemon.rb', line 80 def dead? status == 1 end |
#kill_process ⇒ Object
53 54 55 56 57 58 |
# File 'lib/invoker/daemon.rb', line 53 def kill_process pgid = Process.getpgid(pid) Process.kill('-TERM', pgid) File.delete(pid_file) if File.exist?(pid_file) Invoker::Logger.puts "Stopped Invoker daemon" end |
#log_file ⇒ Object
34 35 36 |
# File 'lib/invoker/daemon.rb', line 34 def log_file File.join(Invoker.home, ".invoker", "#{process_name}.log") end |
#pid ⇒ Object
30 31 32 |
# File 'lib/invoker/daemon.rb', line 30 def pid File.read(pid_file).strip.to_i end |
#pid_file ⇒ Object
26 27 28 |
# File 'lib/invoker/daemon.rb', line 26 def pid_file File.join(Invoker.home, ".invoker", "#{process_name}.pid") end |
#pidfile_exists? ⇒ Boolean
71 72 73 |
# File 'lib/invoker/daemon.rb', line 71 def pidfile_exists? File.exist?(pid_file) end |
#process_running? ⇒ Boolean
60 61 62 63 64 65 |
# File 'lib/invoker/daemon.rb', line 60 def process_running? Process.kill(0, pid) true rescue Errno::ESRCH false end |
#running? ⇒ Boolean
75 76 77 |
# File 'lib/invoker/daemon.rb', line 75 def running? status == 0 end |
#start ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/invoker/daemon.rb', line 11 def start if running? Invoker::Logger.puts "Invoker daemon is already running" exit(0) elsif dead? File.delete(pid_file) if File.exist?(pid_file) end Invoker::Logger.puts "Running Invoker daemon" daemonize end |
#status ⇒ Object
67 68 69 |
# File 'lib/invoker/daemon.rb', line 67 def status @status ||= check_process_status end |
#stop ⇒ Object
22 23 24 |
# File 'lib/invoker/daemon.rb', line 22 def stop kill_process end |