class FakeFlorence::Daemon

Public Class Methods

new() click to toggle source
# File lib/fake_florence/daemon.rb, line 3
def initialize
  @cache = FeatureCache.new(Config.home_dir)
  @announcers = Announcers.load(Config.announce)
end

Public Instance Methods

pid() click to toggle source
# File lib/fake_florence/daemon.rb, line 44
def pid
  Config.pidfile.read.to_i
rescue Errno::ENOENT
  nil
end
run() click to toggle source
# File lib/fake_florence/daemon.rb, line 8
def run
  start_listeners
  Server.set :feature_cache, @cache
  Config.log.info "Florence is listening at #{Config.base_url}"

  prepare_for_run
  Server.run!
end
running?() click to toggle source
# File lib/fake_florence/daemon.rb, line 35
def running?
  return false if pid.nil?
  Process.getpgid(pid)
  true
rescue Errno::ESRCH
  Config.pidfile.delete
  false
end
start(daemonize:) click to toggle source
# File lib/fake_florence/daemon.rb, line 17
def start(daemonize:)
  raise 'Already running' if running?

  if daemonize
    $stdout = Config.logfile.open('a')
    $stdout.sync = true
    Process.daemon(true)
  end

  run
end
stop() click to toggle source
# File lib/fake_florence/daemon.rb, line 29
def stop
  raise 'Not running' unless running?

  Process.kill('TERM', pid)
end

Private Instance Methods

prepare_for_run() click to toggle source
# File lib/fake_florence/daemon.rb, line 58
def prepare_for_run
  Config.pidfile.write(Process.pid)
  at_exit do
    Config.pidfile.delete if Config.pidfile.exist?
    Config.log.info 'Shutting down'
  end
end
start_listeners() click to toggle source
# File lib/fake_florence/daemon.rb, line 52
def start_listeners
  @announcers.each do |announcer|
    @cache.register_listener(&announcer.method(:announce))
  end
end