Class: Daemons::ApplicationGroup

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, options = {}) ⇒ ApplicationGroup

Returns a new instance of ApplicationGroup.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/daemons/application_group.rb', line 26

def initialize(app_name, options = {})
  @app_name = app_name
  @options = options
  
  if options[:script]
    @script = File.expand_path(options[:script])
  end
  
  #@controller = controller
  @monitor = nil
  
  #options = controller.options
  
  @multiple = options[:multiple] || false
  
  @dir_mode = options[:dir_mode] || :script
  @dir = options[:dir] || ''
  
  @keep_pid_files = options[:keep_pid_files] || false
  @no_pidfiles = options[:no_pidfiles] || false
  
  #@applications = find_applications(pidfile_dir())
  @applications = []
end

Instance Attribute Details

#app_argvObject

Returns the value of attribute app_argv.



17
18
19
# File 'lib/daemons/application_group.rb', line 17

def app_argv
  @app_argv
end

#app_nameObject (readonly)

Returns the value of attribute app_name.



5
6
7
# File 'lib/daemons/application_group.rb', line 5

def app_name
  @app_name
end

#applicationsObject (readonly)

Returns the value of attribute applications.



14
15
16
# File 'lib/daemons/application_group.rb', line 14

def applications
  @applications
end

#controller_argvObject

Returns the value of attribute controller_argv.



16
17
18
# File 'lib/daemons/application_group.rb', line 16

def controller_argv
  @controller_argv
end

#dirObject

Returns the value of attribute dir.



20
21
22
# File 'lib/daemons/application_group.rb', line 20

def dir
  @dir
end

#dir_modeObject

Returns the value of attribute dir_mode.



19
20
21
# File 'lib/daemons/application_group.rb', line 19

def dir_mode
  @dir_mode
end

#monitorObject (readonly)

Returns the value of attribute monitor.



8
9
10
# File 'lib/daemons/application_group.rb', line 8

def monitor
  @monitor
end

#multipleObject (readonly)

true if the application is supposed to run in multiple instances



23
24
25
# File 'lib/daemons/application_group.rb', line 23

def multiple
  @multiple
end

#optionsObject (readonly)

attr_reader :controller



12
13
14
# File 'lib/daemons/application_group.rb', line 12

def options
  @options
end

#scriptObject (readonly)

Returns the value of attribute script.



6
7
8
# File 'lib/daemons/application_group.rb', line 6

def script
  @script
end

Instance Method Details

#create_monitor(an_app) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/daemons/application_group.rb', line 143

def create_monitor(an_app)
  return if @monitor
  
  if options[:monitor]
    @monitor = Monitor.new(an_app)

    @monitor.start(@applications)
  end
end

#find_applications(dir) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/daemons/application_group.rb', line 63

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/daemons/application_group.rb', line 72

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
  
  return pids.map {|f|
    app = Application.new(self, {}, PidMem.existing(f))
    setup_app(app)
    app
  }
end

#find_applications_by_pidfiles(dir) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/daemons/application_group.rb', line 98

def find_applications_by_pidfiles(dir)
  pid_files = PidFile.find_files(dir, app_name, ! @keep_pid_files)
  
  #pp pid_files
  
  @monitor = Monitor.find(dir, app_name + '_monitor')
  
  pid_files.reject! {|f| f =~ /_monitor.pid$/}
  
  return pid_files.map {|f|
    app = Application.new(self, {}, PidFile.existing(f))
    setup_app(app)
    app
  }
end

#new_application(add_options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/daemons/application_group.rb', line 114

def new_application(add_options = {})
  if @applications.size > 0 and not @multiple
    if options[:force]
      @applications.delete_if {|a|
        unless a.running?
          a.zap
          true
        end
      }
    end
    
    raise RuntimeException.new('there is already one or more instance(s) of the program running') unless @applications.empty?
  end
  
  app = Application.new(self, add_options)
  
  setup_app(app)
  
  @applications << app
  
  return app
end

#pidfile_dirObject



59
60
61
# File 'lib/daemons/application_group.rb', line 59

def pidfile_dir
  PidFile.dir(@dir_mode, @dir, script)
end

#reload_allObject



178
179
180
# File 'lib/daemons/application_group.rb', line 178

def reload_all
  @applications.each {|a| a.reload}
end

#setupObject

Setup the application group. Currently this functions calls find_applications which finds all running instances of the application and populates the application array.



55
56
57
# File 'lib/daemons/application_group.rb', line 55

def setup
  @applications = find_applications(pidfile_dir())
end

#show_statusObject



188
189
190
# File 'lib/daemons/application_group.rb', line 188

def show_status
  @applications.each {|a| a.show_status}
end

#start_allObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/daemons/application_group.rb', line 153

def start_all
  @monitor.stop if @monitor
  @monitor = nil
  
  @applications.each {|a| 
    fork { 
      a.start 
    } 
  }
end

#stop_all(no_wait = false) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/daemons/application_group.rb', line 164

def stop_all(no_wait = false)
  @monitor.stop if @monitor
  
  threads = []
  
  @applications.each {|a| 
    threads << Thread.new do
      a.stop(no_wait)
    end
  }
  
  threads.each {|t| t.join}
end

#zap_allObject



182
183
184
185
186
# File 'lib/daemons/application_group.rb', line 182

def zap_all
  @monitor.stop if @monitor
  
  @applications.each {|a| a.zap}
end