Skip to content
Browse files

Transform the script from a pipe to a multi-service daemon.

This is particularly important because the AWS initialisation takes
more time than the processing most of the time.

Also, by doing this we can also save the hostname the log comes from,
which allows for the results to be filtered.
  • Loading branch information...
1 parent f1bc654 commit 9ef25a371406e897a7e97c09bbc3dd54adee58a1 @Flameeyes Flameeyes committed Jun 15, 2012
Showing with 59 additions and 48 deletions.
  1. +59 −48 readscript.rb
View
107 readscript.rb
@@ -17,6 +17,7 @@
require 'inifile'
require 'aws'
+require 'socket'
require 'archive/tar/minitar'
require 'builder'
@@ -46,54 +47,64 @@
domain = sdb.domains[domain_name]
sdb.domains.create(domain_name) unless domain.exists?
-Archive::Tar::Minitar::Reader.open($stdin) do |input|
- input.each do |log|
- matches = 0
-
- next unless log.file?
-
- log_name = File.basename(log.name, ".log")
-
- xml_builder = Builder::XmlMarkup.new(:indent => 2)
- xml_builder.instruct!
-
- html_output = xml_builder.html {
- xml_builder.head {
- xml_builder.link(:href => "htmlgrep.css",
- :rel => "stylesheet",
- :type => "text/css")
- }
-
- xml_builder.body {
- xml_builder.ol {
- log.read.split("\n").each do |line|
- if line =~ match
- xml_builder.li line, :class => "match"
- matches += 1
- else
- xml_builder.li line
- end
- end
+Socket.tcp_server_loop("::", 28011) do |sock, client_host|
+ begin
+ hostname = client_host.getnameinfo()[0]
+ puts "Received connection from #{hostname}"
+
+ Archive::Tar::Minitar::Reader.open(sock) do |input|
+ input.each do |log|
+ matches = 0
+
+ next unless log.file?
+
+ log_name = File.basename(log.name, ".log")
+
+ xml_builder = Builder::XmlMarkup.new(:indent => 2)
+ xml_builder.instruct!
+
+ html_output = xml_builder.html {
+ xml_builder.head {
+ xml_builder.link(:href => "../htmlgrep.css",
+ :rel => "stylesheet",
+ :type => "text/css")
+ }
+
+ xml_builder.body {
+ xml_builder.ol {
+ log.read.split("\n").each do |line|
+ if line =~ match
+ xml_builder.li line, :class => "match"
+ matches += 1
+ else
+ xml_builder.li line
+ end
+ end
+ }
+ }
}
- }
- }
-
- html_log = bucket.objects.create(log_name + ".html",
- :data => html_output,
- :acl => :public_read,
- # if we don't use text/html Chromium is
- # unable to render it with the CSS.
- :content_type => 'text/html',
- :storage_class => :reduced_redundancy)
-
- log_parts = log_name.split(':')
- pkg_name = log_parts[0..1].join("/")
- date_time = log_parts[2]
-
- domain.items.create(log_name,
- :matches => matches,
- :pkg => pkg_name,
- :date => date_time,
- :public_url => html_log.public_url);
+
+ html_log = bucket.objects.create("#{hostname}/#{log_name}.html",
+ :data => html_output,
+ :acl => :public_read,
+ # if we don't use text/html Chromium is
+ # unable to render it with the CSS.
+ :content_type => 'text/html',
+ :storage_class => :reduced_redundancy)
+
+ log_parts = log_name.split(':')
+ pkg_name = log_parts[0..1].join("/")
+ date_time = log_parts[2]
+
+ domain.items.create("#{hostname}/#{log_name}",
+ :host => hostname,
+ :pkg => pkg_name,
+ :date => date_time,
+ :matches => matches,
+ :public_url => html_log.public_url);
+ end
+ end
+ ensure
+ sock.close
end
end

0 comments on commit 9ef25a3

Please sign in to comment.
Something went wrong with that request. Please try again.