Class Daemon
In: lib/daemon.rb
Parent: Object

Methods

Included Modules

Singleton ServiceManager Observable ObservablePool

Classes and Modules

Module Daemon::Notification

Attributes

config  [R] 
logger  [R] 
workdir  [R] 

Public Class methods

[Source]

# File lib/daemon.rb, line 35
  def initialize
    @config = nil
    @workdir = Pathname.new('')
    @detached = false
    add_observable(self)
    @logger = nil
  end

Public Instance methods

[Source]

# File lib/daemon.rb, line 70
  def clean_workdir
    @workdir.each_entry do |p|
      next if p.to_s =~ /^\./
      (@workdir + p).rmtree
    end
    changed
    notify_observers(Notification::CLEAN_WORKDIR)
    nil
  end

[Source]

# File lib/daemon.rb, line 43
  def config=(config)
    unless config.nil? or config.respond_to?(:__load__)
      raise(NoMethodError, 'configuration class need to respond to `load\'')
    end
    @config = config
    changed
    notify_observers(Notification::CONFIG, @config)
    @config
  end

[Source]

# File lib/daemon.rb, line 80
  def detach
    return if @detached
    @detached = true
    if pid = fork # father
      exit
    else # son
      Dir.chdir(@workdir.to_s.empty? ? '/' : @workdir.to_s)
      Process.setsid
      STDIN.close
      STDOUT.close
      STDERR.close
    end
    changed
    notify_observers(Notification::DETACH)
    nil
  end

[Source]

# File lib/daemon.rb, line 97
  def detached?
    @detached
  end

[Source]

# File lib/daemon.rb, line 101
  def logger=(logger)
    delete_global_observer(@logger) unless @logger.nil?
    @logger = logger
    add_global_observer(@logger)
    changed
    notify_observers(Notification::START)
  end

[Source]

# File lib/daemon.rb, line 118
  def reload_config
    unless @config.nil?
      @config.__load__
      changed
      notify_observers(Notification::RELOAD_CONFIG)
      @config
    end
  end

[Source]

# File lib/daemon.rb, line 127
  def start_daemon(*args)
  end

[Source]

# File lib/daemon.rb, line 111
  def stop_daemon
    stop_services
    changed
    notify_observers(Notification::STOP)
    exit
  end

[Source]

# File lib/daemon.rb, line 55
  def workdir=(workdir)
    unless workdir.to_s.empty?
      unless File.directory?(workdir)
        raise(ArgumentError, "`#{workdir}' - not a directory")
      end
      Dir.chdir(workdir)
    end
    @workdir = Pathname.new(workdir)
    changed
    notify_observers(Notification::WORKDIR, @workdir)
    @workdir
  end

[Validate]