class Daemons::ApplicationGroup

Attributes

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]
script[R]

Public Class Methods

new(app_name, options = {}) click to toggle source
# File lib/daemons/application_group.rb, line 22
def initialize(app_name, options = {})
  @app_name = app_name
  @options = options

  if options[:script]
    @script = File.expand_path(options[:script])
  end

  @monitor = nil

  @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 = []
end

Public Instance Methods

create_monitor(an_app) click to toggle source
# File lib/daemons/application_group.rb, line 133
def create_monitor(an_app)
  if @monitor && options[:monitor]
    @monitor.stop
    @monitor = nil
  end

  if options[:monitor]
    @monitor = Monitor.new(an_app)
    @monitor.start(self)
  end
end
find_applications(dir) click to toggle source
# File lib/daemons/application_group.rb, line 55
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) click to toggle source

TODO: identifiy the monitor process

# File lib/daemons/application_group.rb, line 64
def find_applications_by_app_name(app_name)
  pids = []

  begin
    x = %xps 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) click to toggle source
# File lib/daemons/application_group.rb, line 90
def find_applications_by_pidfiles(dir)
  @monitor = Monitor.find(dir, app_name + '_monitor')

  pid_files = PidFile.find_files(dir, app_name, ! @keep_pid_files)
  pid_files.map do |f|
    app = Application.new(self, {}, PidFile.existing(f))
    setup_app(app)
    app
  end
end
new_application(add_options = {}) click to toggle source
# File lib/daemons/application_group.rb, line 101
def new_application(add_options = {})
  if @applications.size > 0 && !@multiple
    if options[: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, add_options)

  setup_app(app)

  @applications << app

  app
end
pidfile_dir() click to toggle source
# File lib/daemons/application_group.rb, line 51
def pidfile_dir
  PidFile.dir(@dir_mode, @dir, script)
end
reload_all() click to toggle source
# File lib/daemons/application_group.rb, line 176
def reload_all
  @applications.each { |a| a.reload }
end
running?() click to toggle source

Check whether at least one of the applications in the group is running. If yes, return true.

# File lib/daemons/application_group.rb, line 191
def running?
  @applications.each { |a| return true if a.running? }
  return false
end
setup() click to toggle source

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 47
def setup
  @applications = find_applications(pidfile_dir)
end
show_status() click to toggle source
# File lib/daemons/application_group.rb, line 186
def show_status
  @applications.each { |a| a.show_status }
end
start_all() click to toggle source
# File lib/daemons/application_group.rb, line 145
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) click to toggle source
# File lib/daemons/application_group.rb, line 158
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() click to toggle source
# File lib/daemons/application_group.rb, line 180
def zap_all
  @monitor.stop if @monitor

  @applications.each { |a| a.zap }
end

Private Instance Methods

setup_app(app) click to toggle source
# File lib/daemons/application_group.rb, line 124
def setup_app(app)
  app.controller_argv = @controller_argv
  app.app_argv = @app_argv
  if @options[:show_status_callback]
    app.show_status_callback = @options[:show_status_callback]
  end
end