Connect with socket in threads

S

Shumkov

I'm write a daemon for check uptime of my servers. Use Daemons libiary
(http://daemons.rubyforge.org/) to run it.

ENV['RAILS_ENV'] ||= 'production'

require File.dirname(__FILE__) + "/../../config/environment"
require 'logger'

log = Logger.new("#{RAILS_ROOT}/log/uptime.log")
# Install gem mysql and so no "Lost connection" errors
ActiveRecord::Base.allow_concurrency = false
# Hack from http://dev.rubyonrails.org/ticket/2742. It's worked?
ActiveRecord::Base.connection

Signal.trap('TERM') do
log.info 'Stop checking'
exit
end

# Class of ping result
class Result
ATTRIBUTES = [:target, :check, :response, :request_at, :duration]

ATTRIBUTES.each do |attribute|
attr_accessor attribute
end

def initialize(target, check)
self.target, self.check, self.proxy = target, check, proxy
self.request_at = Time.now
self.duration = 0
end

def true!
self.duration = (Time.now - self.request_at) * 1000
self.response = true
end

def false!
self.response = false
end

def to_hash
hash = {}
ATTRIBUTES.each do |attribute|
hash[attribute] = self.send attribute
end
hash
end
end

log.info 'Start checking'
loop do
proxies = []
# Get all ping's targets and they ping types (cheks)
targets = Target.find :all,
:include => :checks
# Create threads and open proxy for each target
threads = targets.map do |target|
Thread.new(target) do |target|
log.info "Check #{target.host}:"
results = target.checks.map do |check|
result = Result.new(target, self)
begin
Timeout::timeout(60 * 2) do
begin
socket = TCPSocket.new(target.host, 80)
rescue => error
logger.debug "------ Connect error: #{error}"
result.false!
else
logger.debug "------ Ping ok!"
result.true!
end
end
rescue Timeout::Error => error
logger.debug "------ Connect error #{error}"
result.false!
end
log.info " Check #{result.check.title} is
#{result.response}"
result
end
log.info 'Cheking complite'
results
end
end
# Do threads and save result
threads.map { |thread| thread.value }.flatten.each do |result|
Log.create(result.to_hash)
end
sleep 15
end

May be, it worked ok, but i'm have two questions:
1. First target delay а cause delay at other targets. I think, what
first connection blocked other connections, and so а cause delay at
other targets.
2. I'm want to save ping results every 5 minutes precisely, but i
don't known how implement this schema.
How I'm see this: threads with ping work as now, but results append to
array which visible to all threads. Create another thread, which
always run and every 5 minutes average result and save to database.

I'm need you comments and suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top