class FclRailsDaemon::Daemon

Attributes

log_file[RW]
process_name[RW]

Public Class Methods

help() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 23
def self.help
  raise " ༼ つ ◕_◕ ༽つ OOOPS... It was not implemented 'self.help' method in command    "
end
new(command: nil, log: nil, process_name: nil) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 10
def initialize(command: nil, log: nil, process_name: nil)
  raise " ༼ つ ◕_◕ ༽つ OOOPS... It was not set the 'command' name in the command initialize.    " unless command
  @daemon ||= nil
  @command = command
  @log_file = (log) ?  File.join(DAEMON_ROOT, log) : File.join(DAEMON_ROOT, DAEMON_CONFIG['default_log'])
  @process_name = (process_name) ? process_name : command
end
pids() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 18
def self.pids
  f = File.open(@@pids_file, 'rb')
  f.read
end

Public Instance Methods

restart() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 74
def restart
  stop
  start
end
run(loop: false, sleep: nil, &block ) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 27
def run(loop: false, sleep: nil, &block )
  raise " ༼ つ ◕_◕ ༽つ OOOPS... Block is mandatory to run the command.    " unless block_given?

  print @process_name
  @daemon = Daemons.call(multiple: true, app_name: '' ) do
    #Load environment file (rails project)

    #set process_name
    Process.setproctitle(@process_name)

    # Force the output to the defined log
    $stdout.reopen(@log_file, 'a')
    $stderr.reopen(@log_file, 'a')
    $stdout.sync = true

    if loop.equal? true
      loop do
        runner &block
        sleep(sleep) if sleep.is_a? Numeric
      end
    else
      runner &block
    end

  end
end
start() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 54
def start
  pid = get_pid @command
  if running?(pid)
    puts "#{@process_name}: process with pid #{pid} is already running."
    return
  end
  run
  set_pid(@command, @daemon.pid.pid) if @daemon
end
status() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 79
def status
  pid = get_pid @command
  if running? pid
    puts "#{@process_name}: process with pid #{pid} is running."
  else
    puts "#{@process_name}: process is not running."
  end
end
stop() click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 64
def stop
  pid = get_pid @command

  unless running? pid
    puts "#{@process_name}: process is not running."
    return
  end
  kill pid
end

Private Instance Methods

get_pid(command) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 89
def get_pid(command)
  pids = YAML.load_file(@@pids_file)
  (pids.has_key?(command)) ? pids[command] : nil
end
kill(pid) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 115
def kill(pid)
  status = false
  if pid
    begin
      Process.kill("KILL", pid)
      set_pid(@command, nil)
      status = true
    rescue Errno::ESRCH
      puts "#{@process_name}: there is no process with pid #{pid}."
      status = false
    ensure
      if status == false
        puts "#{@process_name}: process is not running."
      else
        puts "#{@process_name}: process was stopped."
      end
    end
  else
    puts "#{@process_name}: there is no process with pid #{pid}."
  end
end
runner(&block) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 137
def runner(&block)
  begin
    block.call
  rescue Exception => e
    puts e.message
    e.backtrace.each {|l| puts l}
  end
end
running?(pid) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 102
def running?(pid)
  status = false
  if pid
    begin
      Process.getpgid(pid)
      status = true
    rescue Errno::ESRCH
      status = false
    end
  end
  status
end
set_pid(command, pid) click to toggle source
# File lib/fcl_rails_daemon/core/daemon.rb, line 94
def set_pid(command, pid)
  pids = YAML.load_file(@@pids_file)
  pids[command] = pid
  File.open(@@pids_file, 'wb') do |f|
    f <<  pids.to_yaml
  end
end