Class: Daemons::ApplicationGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/daemon_kit/patches/force_kill_wait.rb

Instance Method Summary collapse

Instance Method Details

#find_applications(dir) ⇒ Object

We want to redefine find_applications to not rely on pidfiles (e.g. find application if pidfile is gone) We recreate the pid files if they’re not there.



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
# File 'lib/daemon_kit/patches/force_kill_wait.rb', line 14

def find_applications(dir)
  # Find pid_files, like original implementation
  pid_files = PidFile.find_files(dir, app_name)
  @monitor = Monitor.find(dir, app_name + '_monitor')
  pid_files.reject! {|f| f =~ /_monitor.pid$/}
  
  # Find the missing pids based on the UNIX pids
  pidfile_pids = pid_files.map {|pf| PidFile.existing(pf).pid}
  missing_pids = unix_pids - pidfile_pids

  # Create pidfiles that are gone
  if missing_pids.size > 0
    puts "[daemons_ext]: #{missing_pids.size} missing pidfiles: " + 
         "#{missing_pids.inspect}... creating pid file(s)."
    missing_pids.each do |pid|
      pidfile = PidFile.new(dir, app_name, multiple)
      pidfile.pid = pid # Doesn't seem to matter if it's a string or Fixnum
    end
  end

  # Now get all the pid file again
  pid_files = PidFile.find_files(dir, app_name)

  return pid_files.map {|f|
    app = Application.new(self, {}, PidFile.existing(f))
    setup_app(app)
    app
  }
end

#stop_all(force = false) ⇒ Object

Specify :force_kill_wait => (seconds to wait) and this method will block until the process is dead. It first sends a TERM signal, then a KILL signal (-9) if the process hasn’t died after the wait time.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/daemon_kit/patches/force_kill_wait.rb', line 47

def stop_all(force = false)
  @monitor.stop if @monitor
  
  wait = options[:force_kill_wait].to_i
  if wait > 0
    puts "[daemons_ext]: Killing #{app_name} with force after #{wait} secs."

    # Send term first, don't delete PID files.
    @applications.each {|a| a.send_sig('TERM')}

    begin
      started_at = Time.now
      Timeout::timeout(wait) do
        num_pids = unix_pids.size
        while num_pids > 0
          time_left = wait - (Time.now - started_at)
          puts "[daemons_ext]: Waiting #{time_left.round} secs on " +
               "#{num_pids} #{app_name}(s)..."
          sleep 1
          num_pids = unix_pids.size
        end 
      end
    rescue Timeout::Error
      @applications.each {|a| a.send_sig('KILL')}
    ensure
      # Delete Pidfiles
      @applications.each {|a| a.zap!}
    end

    puts "[daemons_ext]: All #{app_name}(s) dead."
  else
    @applications.each {|a| 
      if force
        begin; a.stop; rescue ::Exception; end
      else
        a.stop
      end
    }
  end
end