server/webstats.rb in bloopletech-webstats-0.9.0 vs server/webstats.rb in bloopletech-webstats-0.10.0

- old
+ new

@@ -1,5 +1,6 @@ +#NO GEM DEPENDENCIES FTW require 'webrick' require 'yaml' if $DEBUG Thread.abort_on_exception @@ -7,12 +8,10 @@ if (pid = fork) Signal.trap('HUP', 'IGNORE') Process.detach(pid) exit end - $stdout = File.new('/dev/null', 'w') - $stderr = File.new('/dev/null', 'w') end Thread.new do while(true) sleep(300) @@ -71,11 +70,11 @@ def stringify_keys! each_with_index { |v, i| self[i] = self[i].dup.stringify_keys! if v.is_a? Hash } end def to_json - "[#{map { |e| e.to_json }.join(',')}]" + "[#{map { |e| e.to_json }.join(', ')}]" end end class Hash def formatted! @@ -96,19 +95,19 @@ def stringify_keys! keys.each { |key| self[key.to_s] = delete(key) } each_pair { |k, v| self[k] = self[k].dup.stringify_keys! if v.is_a? Hash } end - alias_method :undecorated_get, :[] + alias_method :undecorated_get, :[] unless method_defined?(:undecorated_get) def [](key) undecorated_get(key) or undecorated_get(key.is_a?(String) ? key.to_sym : key.to_s) end def to_json arr = [] - each_pair { |k, v| arr << "#{k.to_json}:#{v.to_json}" } - "{#{arr.join(',')}}" + each_pair { |k, v| arr << "#{k.to_json}: #{v.to_json}" } + "{#{arr.join(', ')}}" end end class Symbol def to_json @@ -138,11 +137,11 @@ $settings = {} if File.exists?(WEBSTATS_PATH) $settings = YAML.load(IO.read(WEBSTATS_PATH)).symbolize_keys! else - $settings['webstats'] = { 'password' => nil } + $settings['webstats'] = { 'password' => nil, 'clients' => [] } DataProviders::DATA_SOURCES_CLASSES.each_pair { |k, v| $settings[k.to_s] = v.default_settings.stringify_keys! } File.open(WEBSTATS_PATH, "w") { |f| YAML.dump($settings, f) } end DataProviders.setup($settings) @@ -196,11 +195,10 @@ { if(http.readyState == 4) { var results = eval("(" + http.responseText + ")"); if(!results) return; - console.log('procing'); EOF DataProviders::DATA_SOURCES.each_pair do |k, v| body << %{var data_source = results['#{k}']; var sc = document.getElementById('source_contents_#{k}'); sc.className = "source_contents " + (data_source['status'] ? data_source['status'] : ''); #{v.renderer[:contents]}\n} end @@ -246,16 +244,26 @@ res.body = body res['Content-Type'] = "text/html" end end -s = WEBrick::HTTPServer.new(:Port => 9970) +threads = [] +s = WEBrick::HTTPServer.new(:Port => 9970, :Logger => WEBrick::Log.new(nil, 0), :AccessLog => WEBrick::Log.new(nil, 0)) + death = proc do s.shutdown DataProviders::DATA_SOURCES.each_pair { |k, v| v.kill } + threads.each { |t| t.kill } end trap("INT", death) trap("TERM", death) s.mount("/", Webstats) -s.start +threads << Thread.new { s.start } + +if $settings['webstats'].key?(:clients) and !$settings['webstats'][:clients].nil? + $DAEMONIZE = false + $settings['webstats'][:clients].each { |name| threads << Thread.new { require "#{File.dirname(__FILE__)}/../clients/#{name}/#{name}.rb" } } +end + +threads.first.join \ No newline at end of file