Class: Daemons::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/daemons/monitor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(an_app) ⇒ Monitor

Returns a new instance of Monitor.



32
33
34
35
36
37
38
39
40
41
# File 'lib/daemons/monitor.rb', line 32

def initialize(an_app)
  @app = an_app
  @app_name = an_app.group.app_name + '_monitor'
  
  if an_app.pidfile_dir
    @pid = PidFile.new(an_app.pidfile_dir, @app_name, false)
  else
    @pid = PidMem.new
  end
end

Class Method Details

.find(dir, app_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/daemons/monitor.rb', line 10

def self.find(dir, app_name)
  pid = PidFile.find_files(dir, app_name, false)[0]
  
  if pid
    pid = PidFile.existing(pid)
    
    unless PidFile.running?(pid.pid)
      begin; pid.cleanup; rescue ::Exception; end
      return
    end
    
    monitor = self.allocate
  
    monitor.instance_variable_set(:@pid, pid)
    
    return monitor
  end
  
  return nil
end

Instance Method Details

#start(application_group) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/daemons/monitor.rb', line 113

def start(application_group)
  return if application_group.applications.empty?
  
  if @pid.kind_of?(PidFile)
    start_with_pidfile(application_group)
  else
    start_without_pidfile(application_group)
  end
end

#stopObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/daemons/monitor.rb', line 124

def stop
  begin
    pid = @pid.pid
    Process.kill(Application::SIGNAL, pid)
  Timeout::timeout(5, TimeoutError) {      
      while Pid.running?(pid)
        sleep(0.1)
      end
    }
  rescue ::Exception => e
    puts "#{e} #{pid}"
    puts "deleting pid-file."
  end
  
  # We try to remove the pid-files by ourselves, in case the application
  # didn't clean it up.
  begin; @pid.cleanup; rescue ::Exception; end
end