module Daemonism

Constants

DAEMONISM_DEFAULT_OPTS

Public Instance Methods

at(event,&blk)
Alias for: on
at_exit(&blk)
Alias for: on_exit
at_startup(&blk)
Alias for: on_startup
daemonism(opts={},&block) click to toggle source
# File lib/daemonite.rb, line 40
def daemonism(opts={},&block)
  @at_exit = nil
  @at_start = nil

  if File.exists?(opts[:basepath] + '/' + opts[:conffile])
    opts.merge!(Psych::load_file(opts[:basepath] + '/' + opts[:conffile]))
  end
  Dir.chdir(opts[:basepath])

  # set more default options and do other stuff
  opts[:block] = nil
  instance_exec(opts,&block) if block_given?

  ########################################################################################################################
  # parse arguments
  ########################################################################################################################
  if opts[:cmdl_parsing]
    opts[:cmdl_operation] = "start"
    ARGV.options { |opt|
      opt.summary_indent = ' ' * 4
      opt.banner = "Usage:\n#{opt.summary_indent}ruby #{$PROGRAM_NAME} [options] start|stop|restart|info" + (opts[:runtime_cmds].length > 0 ? '|' : '') + opts[:runtime_cmds].map{|ro| ro[0]}.join('|') + "\n"
      opts[:runtime_opts].each do |ro|
        opt.on(*ro)
      end
      opt.on("--verbose", "-v", "Do not daemonize. Write ouput to console.") { opts[:verbose] = true }
      opt.on("--config=FNAME", "-cFNAME", "Config file location.") { |f,a|
        if File.exists?(opts[:basepath] + '/' + f)
          opts.merge!(Psych::load_file(opts[:basepath] + '/' + f))
        end
      }
      opt.on("--help", "-h", "This text.") { puts opt; ::Kernel::exit }
      opt.separator(opt.summary_indent + "start|stop|restart|info".ljust(opt.summary_width+1) + "Do operation start, stop, restart or get information.")
      opts[:runtime_cmds].each do |ro|
        opt.separator(opt.summary_indent + ro[0].ljust(opt.summary_width+1) + ro[1])
      end
      opt.parse!
    }

    unless (%w{start stop restart info} + opts[:runtime_cmds].map{|ro| ro[0] }).include?(ARGV[0])
      puts ARGV.options
      ::Kernel::exit
    end
    opts[:cmdl_operation] = ARGV[0]
  end
  ########################################################################################################################
  opts[:runtime_proc].call(opts) unless opts[:runtime_proc].nil?

  ########################################################################################################################
  # status and info
  ########################################################################################################################
  pid = File.read(opts[:basepath] + '/' + opts[:pidfile]).to_i rescue pid = -1
  status = Proc.new do
    begin
      Process.getpgid pid
      true
    rescue Errno::ESRCH
      false
    end
  end
  unless @@daemonism_restart
    if opts[:cmdl_operation] == "info" && status.call == false
      puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}not running"
      ::Kernel::exit
    end
    if opts[:cmdl_operation] == "info" && status.call == true
      puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}running as #{pid}"
      begin
        stats = `ps -o "vsz,rss,lstart,time" -p #{pid}`.split("\n")[1].strip.split(/ +/)
        puts "Virtual:  #{"%0.2f" % (stats[0].to_f/1024)} MiB"
        puts "Resident: #{"%0.2f" % (stats[1].to_f/1024)} MiB"
        puts "Started:  #{stats[2..-2].join(' ')}"
        puts "CPU Time: #{stats.last}"
      rescue
      end
      ::Kernel::exit
    end
    if %w{start}.include?(opts[:cmdl_operation]) && status.call == true
      puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}already started"
      ::Kernel::exit
    end
  end

  ########################################################################################################################
  # stop/restart server
  ########################################################################################################################
  unless @@daemonism_restart
    if %w{stop restart}.include?(opts[:cmdl_operation])
      if status.call == false
        puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}maybe not started?"
      else
        puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}stopped"
        puts "Waiting while server goes down ..."
        count = 0
        while status.call
          if count > opts[:kill_amount]
            Process.kill "SIGKILL", pid
            File.unlink(opts[:basepath] + '/' + opts[:pidfile])
          else
            if opts[:cmdl_operation] == 'stop'
              Process.kill "SIGTERM", pid
            else
              Process.kill "SIGHUP", pid
            end
          end
          count += 1
          sleep 0.3
        end
      end
      ::Kernel::exit unless opts[:cmdl_operation] == "restart"
    end
  end

  ########################################################################################################################
  # go through user defined startup thingis
  ########################################################################################################################
  unless @@daemonism_restart
    opts[:runtime_cmds].each do |ro|
      ro[2].call(status.call) if opts[:cmdl_operation] == ro[0]
    end
    @@daemonism_restart = true

    retain = $stdout.dup
    Process.daemon unless opts[:verbose]
    retain.puts "Server #{opts[:cmdl_info].nil? ? '' : '(' + opts[:cmdl_info].to_s + ') '}started as PID:#{Process.pid}"
    File.write(opts[:basepath] + '/' + opts[:pidfile],Process.pid) # after daemon, so that we get the forked pid
    Dir.chdir(opts[:basepath])
    ::Kernel::at_exit do
      File.unlink(opts[:basepath] + '/' + opts[:pidfile])
      @at_exit.call(opts) if @at_exit
    end
  end
end
exit() click to toggle source
# File lib/daemonite.rb, line 181
def exit; :exit; end
on(event,&blk) click to toggle source
# File lib/daemonite.rb, line 173
def on(event,&blk)
  case event
    when :exit
      @at_exit = blk
    when :startup
      @at_startup = blk
  end
end
Also aliased as: at
on_exit(&blk) click to toggle source
# File lib/daemonite.rb, line 183
def on_exit(&blk)
  on :exit, &blk
end
Also aliased as: at_exit
on_startup(&blk) click to toggle source
# File lib/daemonite.rb, line 186
def on_startup(&blk)
  on :startup, &blk
end
Also aliased as: at_startup
startup() click to toggle source
# File lib/daemonite.rb, line 182
def startup; :startup; end
use(blk) click to toggle source
# File lib/daemonite.rb, line 189
def use(blk)
  instance_eval(&blk)
end