config-inc: @config[db,sid] => @config[db][sid]. Only opened dbs supported.

This commit is contained in:
Denis Knauf 2010-03-29 01:12:22 +02:00
parent 2eb887d9cc
commit 313be57c1d
2 changed files with 35 additions and 15 deletions

View file

@ -5,41 +5,61 @@ require 'robustserver'
module LogAn::Inc module LogAn::Inc
class Main < RobustServer class Main < RobustServer
def config db, type = nil # Open Config.
def config env, db, type = nil
type ||= 1+4 type ||= 1+4
ret = @etc[ 'inc.cnf', db, SBDB::RDONLY] ret = env[ 'inc.cnf', db, SBDB::RDONLY]
ret = AutoValueConvertHash.new ret if type&4 > 0 ret = AutoValueConvertHash.new ret if type&4 > 0
ret = Cache.new ret, type&3 if type&3 > 0 ret = Cache.new ret, type&3 if type&3 > 0
ret ret
end end
# Prepare Server.
#
# * conf:
# logs
# : Where to store log-databases? default: ./logs
# etc
# : Where to find config-databases? default: ./etc
# server
# : Server-Configuration. default { port: 1087 }
def initialize conf def initialize conf
super super
@conf = conf @conf = {}
@logs = LogAn::Loglines.new 'logs' # Copy config - changes possible
etc = @conf[:etc] || 'etc' conf.each &@conf.method(:[]=)
Dir.mkdir etc rescue Errno::EEXIST # Default directories
@etc = SBDB::Env.new( etc, %w[logs etc].each {|key| @conf[key.to_sym] = key }
# Open Loglines-databases
@logs = LogAn::Loglines.new @conf[:logs]
# Open config-databases
Dir.mkdir @conf[:etc] rescue Errno::EEXIST
@etc = SBDB::Env.new( @conf[:etc],
log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE, log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE,
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL) flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL)
@hosts = config 'hosts' # Set inc-config - stored in etc/inc.cnf
@files = config 'files' @conf[:inc] = {}
@fileparser = config 'fileparser' %w[hosts files fileparser].each {|key| @conf[:inc][key.to_sym] = config( @etc, key) }
@serv = LogAn::Inc.new :sock => TCPServer.new( *@conf[:server]) # Prepare Inc-server - create server
@sigs[:INT] = @sigs[:TERM] = method(:shutdown) @serv = LogAn::Inc.new :sock => TCPServer.new( *@conf[:server]), :config => @conf[:inc]
# Shutdown on signals
@sigs[:INT] = @sigs[:TERM] = method( :shutdown)
end end
# Will be called at exit. Will close all opened BDB::Env
def at_exit def at_exit
@logs and @logs.close @logs and @logs.close
@etc and @etc.close @etc and @etc.close
end end
def shutdown s = nil # Shutdown Server cleanly.
$stderr.puts [:signal, s, Signal[s]].inspect if s def shutdown signal = nil
$stderr.puts [:signal, signal, Signal[signal]].inspect if signal
@serv.close @serv.close
exit 0 exit 0
end end
# Runs server. Don't use it! Use #main.
def run def run
@serv.run @serv.run
end end

View file

@ -95,7 +95,7 @@ module Logan
def event_cmd cmd def event_cmd cmd
sid, line = cmd.unpack 'Na*' sid, line = cmd.unpack 'Na*'
begin begin
@config[ :fileparser, sid].event_line line, self @config[ :fileparser][sid].event_line line, self
rescue Didi::Config::NoSIDFound, Didi::Config::SIDLoadingError rescue Didi::Config::NoSIDFound, Didi::Config::SIDLoadingError
$stderr.puts( {:sid => sid, :exception => $!, :backtrace => $!.backtrace}.inspect) $stderr.puts( {:sid => sid, :exception => $!, :backtrace => $!.backtrace}.inspect)
end end