Verify database connections in long-running idle Rails processes
I've interfaced one of my xmpp4r bots with the Xeriom Networks control panel. I had intended to write a post about it to show how easy it is, but I've been beaten to it. I can, however, offer one piece of advice that will stop the bot dying after a few hours of idling: periodically verify your database connections.
RAILS_DEFAULT_LOGGER.debug "Launching database connection verifier"
Thread.new do
loop do
sleep 1800 # Half an hour
RAILS_DEFAULT_LOGGER.debug "Verifying database connections"
ActiveRecord::Base.verify_active_connections!
end
end
Adding the above code to a script will stop database connections getting dropped (or, at least, will reconnect them if it happens).
To see it in action, add support@xeriom.net to your XMPP roster and have a chat. It's not hugely intelligent, but it does support a few useful commands.
Love me!
If you've found this article useful I'd appreciate beer and recommendations at Working With Rails.
XMPP4R-Simple makes XMPP in Ruby uhh... simple...
I thought it might be cool to have a control interface that you could talk to using IM, something like the IM client for Twitter.
Initially I was looking at XMPP4R but a little reading pointed out that there's a gem called XMPP4R-Simple. Well, simple is always good so one gem install and 45 minutes later I had a Ruby script that could log in to an XMPP server, listen to (and log) what people said, and respond with a simple message.
#!/usr/bin/env ruby
require 'rubygems'
require 'xmpp4r-simple'
logfile = File.join('..', 'log', "#{File.basename(__FILE__)}.log")
logger = Hodel3000CompliantLogger.new(logfile)
jabber = Jabber::Simple.new "username@domain.com", "password"
sleep 1
jabber.status(:away, "No one here but us mice.")
sleep 1
jabber.deliver("craig@xeriom.net", "I woke up at #{Time.now}.")
loop do
begin
jabber.received_messages do |msg|
jid = msg.from.strip.to_s
logger.info "%s said: %s" % [ jid, msg.body ]
jabber.add(jid) if !jabber.subscribed_to?(jid)
jabber.deliver(jid, "Nom nom nom.")
end
jabber.presence_updates do |update|
jid, status, message = *update
logger.info "#{jid} is #{status} (#{message})"
end
jabber.new_subscriptions do |friend, presence|
logger.info "#{friend.jid} #{presence.type}"
jabber.add(friend.jid) if !jabber.subscribed_to?(friend.jid)
end
rescue Exception => e
logger.error e.to_s
end
sleep 1
end
Our own little pet XMPP client. How cute is that?
Quit yo jibba jabba, sucka!
If you've found this article useful I'd appreciate recommendations at Working With Rails.


