Class: Daemons::ApplicationGroup
- Inherits:
-
Object
- Object
- Daemons::ApplicationGroup
- Defined in:
- lib/daemons/application_group.rb
Instance Attribute Summary collapse
-
#app_argv ⇒ Object
Returns the value of attribute app_argv.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#applications ⇒ Object
readonly
Returns the value of attribute applications.
-
#controller_argv ⇒ Object
Returns the value of attribute controller_argv.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#dir_mode ⇒ Object
Returns the value of attribute dir_mode.
-
#monitor ⇒ Object
readonly
Returns the value of attribute monitor.
-
#multiple ⇒ Object
readonly
true if the application is supposed to run in multiple instances.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Instance Method Summary collapse
- #create_monitor(an_app) ⇒ Object
- #find_applications(dir) ⇒ Object
-
#find_applications_by_app_name(app_name) ⇒ Object
TODO: identifiy the monitor process.
- #find_applications_by_pidfiles(dir) ⇒ Object
-
#initialize(app_name, options = {}) ⇒ ApplicationGroup
constructor
A new instance of ApplicationGroup.
- #new_application(add_options = {}) ⇒ Object
- #pidfile_dir ⇒ Object
- #reload_all ⇒ Object
-
#running? ⇒ Boolean
Check whether at least one of the applications in the group is running.
-
#setup ⇒ Object
Setup the application group.
- #show_status ⇒ Object
- #start_all ⇒ Object
- #stop_all(no_wait = false) ⇒ Object
- #zap_all ⇒ Object
Constructor Details
#initialize(app_name, options = {}) ⇒ ApplicationGroup
Returns a new instance of ApplicationGroup.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/daemons/application_group.rb', line 22 def initialize(app_name, = {}) @app_name = app_name @options = if @options[:script] @script = File.(@options[:script]) end @monitor = nil @multiple = @options[:multiple] || false @dir_mode = @options[:dir_mode] || :script ['dir'].each do |k| @options[k] = File.(@options[k]) if @options.key?(k) end @dir = @options[:dir] || '' @keep_pid_files = @options[:keep_pid_files] || false @no_pidfiles = @options[:no_pidfiles] || false @pid_delimiter = @options[:pid_delimiter] @applications = [] end |
Instance Attribute Details
#app_argv ⇒ Object
Returns the value of attribute app_argv.
14 15 16 |
# File 'lib/daemons/application_group.rb', line 14 def app_argv @app_argv end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
4 5 6 |
# File 'lib/daemons/application_group.rb', line 4 def app_name @app_name end |
#applications ⇒ Object (readonly)
Returns the value of attribute applications.
11 12 13 |
# File 'lib/daemons/application_group.rb', line 11 def applications @applications end |
#controller_argv ⇒ Object
Returns the value of attribute controller_argv.
13 14 15 |
# File 'lib/daemons/application_group.rb', line 13 def controller_argv @controller_argv end |
#dir ⇒ Object
Returns the value of attribute dir.
17 18 19 |
# File 'lib/daemons/application_group.rb', line 17 def dir @dir end |
#dir_mode ⇒ Object
Returns the value of attribute dir_mode.
16 17 18 |
# File 'lib/daemons/application_group.rb', line 16 def dir_mode @dir_mode end |
#monitor ⇒ Object (readonly)
Returns the value of attribute monitor.
7 8 9 |
# File 'lib/daemons/application_group.rb', line 7 def monitor @monitor end |
#multiple ⇒ Object (readonly)
true if the application is supposed to run in multiple instances
20 21 22 |
# File 'lib/daemons/application_group.rb', line 20 def multiple @multiple end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/daemons/application_group.rb', line 9 def @options end |
#script ⇒ Object (readonly)
Returns the value of attribute script.
5 6 7 |
# File 'lib/daemons/application_group.rb', line 5 def script @script end |
Instance Method Details
#create_monitor(an_app) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/daemons/application_group.rb', line 143 def create_monitor(an_app) if @monitor && [:monitor] @monitor.stop @monitor = nil end if [:monitor] opt = {} opt[:monitor_interval] = [:monitor_interval] if [:monitor_interval] @monitor = Monitor.new(an_app, opt) @monitor.start(self) end end |
#find_applications(dir) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/daemons/application_group.rb', line 61 def find_applications(dir) if @no_pidfiles return find_applications_by_app_name(app_name) else return find_applications_by_pidfiles(dir) end end |
#find_applications_by_app_name(app_name) ⇒ Object
TODO: identifiy the monitor process
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/daemons/application_group.rb', line 70 def find_applications_by_app_name(app_name) pids = [] begin x = `ps auxw | grep -v grep | awk '{print $2, $11, $12}' | grep #{app_name}` if x && x.chomp! processes = x.split(/\n/).compact processes = processes.delete_if do |p| _pid, name, add = p.split(/\s/) # We want to make sure that the first part of the process name matches # so that app_name matches app_name_22 app_name != name[0..(app_name.length - 1)] and not add.include?(app_name) end pids = processes.map { |p| p.split(/\s/)[0].to_i } end rescue ::Exception end pids.map do |f| app = Application.new(self, {}, PidMem.existing(f)) setup_app(app) app end end |
#find_applications_by_pidfiles(dir) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/daemons/application_group.rb', line 96 def find_applications_by_pidfiles(dir) @monitor = Monitor.find(dir, app_name + '_monitor') reporter = Reporter.new() pid_files = PidFile.find_files(dir, app_name, ! @keep_pid_files, @pid_delimiter) do |pid, file| reporter.deleted_found_pidfile(pid, file) end pid_files.map do |f| app = Application.new(self, {}, PidFile.existing(f)) setup_app(app) app end end |
#new_application(add_options = {}) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/daemons/application_group.rb', line 111 def new_application( = {}) if @applications.size > 0 && !@multiple if [:force] @applications.delete_if do |a| unless a.running? a.zap true end end end fail RuntimeException.new('there is already one or more instance(s) of the program running') unless @applications.empty? end app = Application.new(self, ) setup_app(app) @applications << app app end |
#pidfile_dir ⇒ Object
57 58 59 |
# File 'lib/daemons/application_group.rb', line 57 def pidfile_dir PidFile.dir(@dir_mode, @dir, script) end |
#reload_all ⇒ Object
188 189 190 |
# File 'lib/daemons/application_group.rb', line 188 def reload_all @applications.each { |a| a.reload } end |
#running? ⇒ Boolean
Check whether at least one of the applications in the group is running. If yes, return true.
203 204 205 206 |
# File 'lib/daemons/application_group.rb', line 203 def running? @applications.each { |a| return true if a.running? } return false end |
#setup ⇒ Object
Setup the application group. Currently this functions calls find_applications
which finds all running instances of the application and populates the application array.
53 54 55 |
# File 'lib/daemons/application_group.rb', line 53 def setup @applications = find_applications(pidfile_dir) end |
#show_status ⇒ Object
198 199 200 |
# File 'lib/daemons/application_group.rb', line 198 def show_status @applications.each { |a| a.show_status } end |
#start_all ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/daemons/application_group.rb', line 157 def start_all @monitor.stop if @monitor @monitor = nil pids = [] @applications.each do |a| pids << fork do a.start end end pids.each { |pid| Process.waitpid(pid) } end |
#stop_all(no_wait = false) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/daemons/application_group.rb', line 170 def stop_all(no_wait = false) if @monitor @monitor.stop @monitor = nil setup end threads = [] @applications.each do |a| threads << Thread.new do a.stop(no_wait) end end threads.each { |t| t.join } end |
#zap_all ⇒ Object
192 193 194 195 196 |
# File 'lib/daemons/application_group.rb', line 192 def zap_all @monitor.stop if @monitor @applications.each { |a| a.zap } end |