Module: Fume::Daemon::InstanceMethods
- Defined in:
- lib/fume/daemon.rb
Instance Attribute Summary collapse
-
#pid_file_path ⇒ Object
Returns the value of attribute pid_file_path.
Instance Method Summary collapse
- #already_running? ⇒ Boolean
- #daemon_name ⇒ Object
- #initialize ⇒ Object
- #register_trap ⇒ Object
- #remove_pid_file ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #write_pid_file(pid) ⇒ Object
Instance Attribute Details
#pid_file_path ⇒ Object
Returns the value of attribute pid_file_path.
19 20 21 |
# File 'lib/fume/daemon.rb', line 19 def pid_file_path @pid_file_path end |
Instance Method Details
#already_running? ⇒ Boolean
51 52 53 54 55 56 57 58 59 |
# File 'lib/fume/daemon.rb', line 51 def already_running? if self.pid_file_path && File.exists?(self.pid_file_path) pid = File.read(self.pid_file_path) return Process.kill(0, pid.to_i) == 1 if pid # test running end return false rescue Errno::ESRCH # NOT running or zombied return false end |
#daemon_name ⇒ Object
25 26 27 |
# File 'lib/fume/daemon.rb', line 25 def daemon_name self.class.name end |
#initialize ⇒ Object
21 22 23 |
# File 'lib/fume/daemon.rb', line 21 def initialize self.pid_file_path = Rails.root.join("tmp", "pids", "#{daemon_name}.pid") end |
#register_trap ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/fume/daemon.rb', line 29 def register_trap %w{TERM INT HUP}.each do |signal_name| Signal.trap(signal_name) do self.stop end end end |
#remove_pid_file ⇒ Object
47 48 49 |
# File 'lib/fume/daemon.rb', line 47 def remove_pid_file FileUtils.rm(pid_file_path, :force => true) if self.pid_file_path && File.exists?(self.pid_file_path) end |
#start ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fume/daemon.rb', line 61 def start return if already_running? begin register_trap write_pid_file(Process.pid) logger.info { "Daemon: #{daemon_name} starting ... PID: #{Process.pid}" } @run = true while @run self.execute end logger.info { "Daemon: #{daemon_name} stoped ..." } rescue Exception => e logger.error(e) ensure remove_pid_file end end |
#stop ⇒ Object
81 82 83 |
# File 'lib/fume/daemon.rb', line 81 def stop @run = false end |
#write_pid_file(pid) ⇒ Object
41 42 43 44 45 |
# File 'lib/fume/daemon.rb', line 41 def write_pid_file(pid) if self.pid_file_path File.open(self.pid_file_path, "w") { |f| f << pid } end end |