| Class | Daemons::ApplicationGroup |
| In: |
lib/daemons/application_group.rb
|
| Parent: | Object |
| app_argv | [RW] | |
| app_name | [R] | |
| applications | [R] | |
| controller_argv | [RW] | |
| dir | [RW] | |
| dir_mode | [RW] | |
| monitor | [R] | |
| multiple | [R] | true if the application is supposed to run in multiple instances |
| options | [R] | attr_reader :controller |
| script | [R] |
# File lib/daemons/application_group.rb, line 26
26: def initialize(app_name, options = {})
27: @app_name = app_name
28: @options = options
29:
30: if options[:script]
31: @script = File.expand_path(options[:script])
32: end
33:
34: #@controller = controller
35: @monitor = nil
36:
37: #options = controller.options
38:
39: @multiple = options[:multiple] || false
40:
41: @dir_mode = options[:dir_mode] || :script
42: @dir = options[:dir] || ''
43:
44: @keep_pid_files = options[:keep_pid_files] || false
45: @no_pidfiles = options[:no_pidfiles] || false
46:
47: #@applications = find_applications(pidfile_dir())
48: @applications = []
49: end
# File lib/daemons/application_group.rb, line 143
143: def create_monitor(an_app)
144: return if @monitor
145:
146: if options[:monitor]
147: @monitor = Monitor.new(an_app)
148:
149: @monitor.start(@applications)
150: end
151: end
# File lib/daemons/application_group.rb, line 63
63: def find_applications(dir)
64: if @no_pidfiles
65: return find_applications_by_app_name(app_name)
66: else
67: return find_applications_by_pidfiles(dir)
68: end
69: end
TODO: identifiy the monitor process
# File lib/daemons/application_group.rb, line 72
72: def find_applications_by_app_name(app_name)
73: pids = []
74:
75: begin
76: x = `ps auxw | grep -v grep | awk '{print $2, $11, $12}' | grep #{app_name}`
77: if x && x.chomp!
78: processes = x.split(/\n/).compact
79: processes = processes.delete_if do |p|
80: pid, name, add = p.split(/\s/)
81: # We want to make sure that the first part of the process name matches
82: # so that app_name matches app_name_22
83:
84: app_name != name[0..(app_name.length - 1)] and not add.include?(app_name)
85: end
86: pids = processes.map {|p| p.split(/\s/)[0].to_i}
87: end
88: rescue ::Exception
89: end
90:
91: return pids.map {|f|
92: app = Application.new(self, {}, PidMem.existing(f))
93: setup_app(app)
94: app
95: }
96: end
# File lib/daemons/application_group.rb, line 98
98: def find_applications_by_pidfiles(dir)
99: pid_files = PidFile.find_files(dir, app_name, ! @keep_pid_files)
100:
101: #pp pid_files
102:
103: @monitor = Monitor.find(dir, app_name + '_monitor')
104:
105: pid_files.reject! {|f| f =~ /_monitor.pid$/}
106:
107: return pid_files.map {|f|
108: app = Application.new(self, {}, PidFile.existing(f))
109: setup_app(app)
110: app
111: }
112: end
# File lib/daemons/application_group.rb, line 114
114: def new_application(add_options = {})
115: if @applications.size > 0 and not @multiple
116: if options[:force]
117: @applications.delete_if {|a|
118: unless a.running?
119: a.zap
120: true
121: end
122: }
123: end
124:
125: raise RuntimeException.new('there is already one or more instance(s) of the program running') unless @applications.empty?
126: end
127:
128: app = Application.new(self, add_options)
129:
130: setup_app(app)
131:
132: @applications << app
133:
134: return app
135: end
# File lib/daemons/application_group.rb, line 59
59: def pidfile_dir
60: PidFile.dir(@dir_mode, @dir, script)
61: end
# File lib/daemons/application_group.rb, line 178
178: def reload_all
179: @applications.each {|a| a.reload}
180: end
Setup the application group. Currently this functions calls find_applications which finds all running instances of the application and populates the application array.
# File lib/daemons/application_group.rb, line 55
55: def setup
56: @applications = find_applications(pidfile_dir())
57: end
# File lib/daemons/application_group.rb, line 188
188: def show_status
189: @applications.each {|a| a.show_status}
190: end
# File lib/daemons/application_group.rb, line 153
153: def start_all
154: @monitor.stop if @monitor
155: @monitor = nil
156:
157: @applications.each {|a|
158: fork {
159: a.start
160: }
161: }
162: end