Created
April 11, 2012 09:24
-
-
Save wyldrodney/2358216 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Periodic | |
class HostProbeJob | |
def initialize(host) | |
@host = host | |
end | |
def perform | |
stime = Time.current.to_f | |
@host.host_statuses.create(is_up: StatusChecks::HostProbe.new(@host).is_up?, timestamp: Time.current) | |
dtime = Time.current.to_f - stime | |
next_run = if (dtime + 8) < @host.period | |
@host.period - dtime | |
else | |
@host.period | |
end | |
Delayed::Job.enqueue Periodic::HostProbeJob.new(@host), { run_at: next_run.seconds.from_now } | |
end | |
end | |
class ResourceProbeJob | |
def initialize(resource) | |
@resource = resource | |
@host = @resource.host | |
end | |
def perform | |
stime = Time.current.to_f | |
resource_analyze = StatusChecks::ResourceProbe.new(@resource) | |
@resource.resource_statuses.create(code: resource_analyze.code, timeout: resource_analyze.timeout, | |
timestamp: Time.current) | |
dtime = Time.current.to_f - stime | |
next_run = if (dtime + 4) < @host.period | |
@host.period - dtime | |
else | |
@host.period | |
end | |
Delayed::Job.enqueue Periodic::ResourceProbeJob.new(@resource), { run_at: next_run.seconds.from_now } | |
end | |
end | |
end | |
module Proxy | |
require 'net/http' | |
require 'net/https' | |
require 'rchardet19' | |
require 'nokogiri' | |
class LinkConvert | |
def initialize(address) | |
@address = address | |
walk_around | |
end | |
def to_html | |
if @doc | |
add_styles | |
@doc.to_html | |
else | |
"Error: Proxy can't load or convert page." | |
end | |
end | |
private | |
def walk_around | |
load | |
{link: 'href', img: 'src', a: 'href', script: 'src'}.each do |key, value| | |
begin | |
@doc.search(key).each do |key| | |
key.attributes[value].value = extend_with_data(key.attributes[value].value) | |
end | |
rescue | |
puts "Get error while call 'extend_with_data' on #{@address} with {#{key}: '#{value}'}." | |
end | |
end | |
end | |
def load | |
@uri = URI(@address) | |
if @uri | |
http = Net::HTTP.new(@uri.host, @uri.port) | |
http.use_ssl = true if @uri.scheme == 'https' | |
begin | |
body = http.get(@uri.path, {"User-Agent" => "Mozilla/5.0 (compatible; SiteSaver/0.1; +http://sitesaver.ru/)"}).body | |
@doc = Nokogiri::HTML(convert_to_utf8(body)) | |
rescue | |
@doc = nil | |
puts "Get error while call 'load' on #{@address}" | |
end | |
else | |
@doc = nil | |
end | |
end | |
def convert_to_utf8(body) | |
begin | |
charset = CharDet.detect(body, silent: false).encoding | |
body = body.encode('utf-8', charset) unless charset == 'utf-8' | |
rescue | |
puts "Get error while call 'convert_to_utf8' on #{@address}" | |
end | |
body | |
end | |
def extend_with_data(link) | |
if has_domain?(link) | |
link | |
else | |
"#{@uri.scheme}://#{@uri.host}:#{@uri.port}/#{link}" | |
end | |
end | |
def has_domain?(link) | |
link =~ /(^http|\.+\w+\/)/ | |
end | |
def add_styles | |
Nokogiri::HTML::Builder.with(@doc.at('body')) do |html| | |
html.style { | |
html.text ".wizard-hover { background-color: #E6F1F6; border: 1px solid #CCCCCC; color: #4E575B; border-radius: 3px; }" | |
html.text ".wizard-selected { background-color: #F8F8F8; border: 1px solid #AAAAAA; color: #333333; border-radius: 3px; }" | |
} | |
end | |
end | |
end | |
end | |
module StatusChecks | |
require 'socket' | |
require 'timeout' | |
require 'net/http' | |
require 'net/https' | |
require 'rchardet19' | |
require 'nokogiri' | |
HOST_PROBE_TIMEOUT = 12 | |
RESOURCE_PROBE_TIMEOUT = 24 | |
RESOURCE_CONTENT_PROBE_TIMEOUT = 120 | |
UA = "Mozilla/5.0 (compatible; SiteSaver/0.1; +http://sitesaver.ru/)" | |
class HostProbe | |
def initialize(host) | |
@host = host | |
end | |
def is_up? | |
Timeout::timeout(HOST_PROBE_TIMEOUT) do | |
begin | |
TCPSocket.new(@host.address, @host.port).close | |
true | |
rescue | |
false | |
end | |
end | |
rescue Timeout::Error | |
false | |
end | |
end | |
class ResourceProbe | |
def initialize(resource) | |
@resource = resource | |
@host = resource.host | |
probe | |
end | |
attr_reader :code, :timeout | |
private | |
def probe | |
http = Net::HTTP.new(@host.address, @host.port) | |
http.use_ssl = true if @host.scheme == 'https' | |
Timeout::timeout(RESOURCE_PROBE_TIMEOUT) do | |
begin | |
start_time = Time.now.to_f | |
@code = http.get(@resource.path, { "User-Agent" => UA }).code | |
@timeout = (Time.now.to_f - start_time).round(3) | |
true | |
rescue | |
false | |
end | |
end | |
rescue Timeout::Error | |
false | |
end | |
end | |
class ResourceContentProbe | |
def initialize(resource) | |
@resource = resource | |
@host = resource.host | |
probe | |
end | |
attr_reader :content | |
private | |
def probe | |
http = Net::HTTP.new(@host.address, @host.port) | |
http.use_ssl = true if @host.scheme == 'https' | |
Timeout::timeout(RESOURCE_CONTENT_PROBE_TIMEOUT) do | |
begin | |
body = http.get(@resource.path, { "User-Agent" => UA }).body | |
doc = Nokogiri::HTML(convert_to_utf8(body)) | |
@content = cut_content(doc) | |
true | |
rescue | |
false | |
end | |
end | |
rescue Timeout::Error | |
false | |
end | |
def convert_to_utf8(body) | |
begin | |
charset = CharDet.detect(body, silent: false).encoding | |
body = body.encode('utf-8', charset) unless charset == 'utf-8' | |
rescue | |
end | |
body | |
end | |
def cut_content(doc) | |
content = [] | |
@resource.xpaths.split(",").each do |xpath| | |
content << doc.search(xpath).to_html | |
end | |
content.join("\n") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment