Sep 03
I’m am still captivated by the perfect, simple CMS solution in Ruby/Rails (I’ll take another framework too). There are some fantastic projects out there, browsercms.org for example. But posts like THIS , really drive home the point. If Wordpress is so well suited for a simple site and/or blog, why not just use it? Igvita demonstrates easy Rails integration with Apache rewrite rules.
There are others out there stating the same thing: while Rails can be used to create a CMS, a developer’s time is better spent solving business rather than reinventing the wheel in Ruby to keep the whole project/code under one umbrella. And at my current gig, we are starting to retool for this paradigm. Rails and Sinatra apps are purely business focuses and super light weight. These service applications can them be glued together via ESI, rewrites, or Wordpress for consumption. In effect, your complex web app is assembled at request time from a set of simple web services. Here is some mock code to demonstrate:
wordpress_template.php
//some more wonder wordpress stuff
<?= include_rails_service('http://service.app/user/profile',ESI_INCLUDE) ?>
//some more wonder wordpress stuff
<?= include_rails_service('http://service.app2/events/weeks/2',SERVICE) ?>
//end of wordpress.php
...
//function included elsewhere
function include_rails_service(service_url,type){
//could return any of following for the resource url
switch (type) {
case ESI_INCLUDE:
return "<esi include ='" + service_url + "'>";
break;
case SERVICE:
include service_url;
break;
case AJAX:
//javascript include with service js, doing doc.write
}
}
The point is Wordpress doesn’t know about Rails and Rails doesn’t know about Wordpress. Keep them seperate, keep them simple and make life better.
Tagged with: ajax • CMS • esi • integration • Rails • sinatra • wordpress
Aug 10
When working with Sinatra + ActiveRecord + MySQL running on the venerable Thin server, you are probably going to see the error after 8 hours(MySQL default connection timeout): “Mysql::Error: MySQL server has gone away” message. The resolution is pretty simple:
class NewApp < Sinatra::Default
before do
ActiveRecord::Base.connection.verify!
end
...
end #end of Sinatra class
I created a Lighthouse ticket before I discovered the resolution. There are also more technical details here: https://thin.lighthouseapp.com/projects/7212-thin/tickets/101-activerecord-connection-does-not-reconnect-when-using-thinsinatra-but-works-fine-with-just-sinatra#ticket-101-4
Tagged with: activerecord • error • mysql • sinatra • thin
Jul 15
This example uses Ruby to telnet into Varnish and issue the “url.purge .*” command. The only gotcha is that the varnish telnet server does not issue a command prompt which causes Ruby telnet to timeout and get cranky. Well a little exception handling hacks past this. Enjoy.
require 'rubygems'
namespace "varnish" do
desc "Purge ALL urls from Varnish"
task :global_purge => :environment do
#It WILL timeout, just accept it. Varnish does not have a command prompt.
require 'net/telnet'
@result = ""
begin
localhost = Net::Telnet::new("Host" => "localhost",
"Port" => 6082,
"Timeout" => 5)
localhost.cmd("url.purge .*") { |c| @result = c}
rescue Exception
if @result.include? ("200 0")
puts "varnish purged OK."
else
raise "Varnish not purged."
end
end
end
end
Tagged with: purge • rake • task • varnish
Jul 14
What does this mean? Well for starters, those nice little PCRE shorthands for character classes just don’t work! So putting this in your Varnish VCL will silently do nothing:
if (req.url ~ "/\d+(/$|/\?|\?|$)") ...
whereas this match:
if (req.url ~ "/[0-9]+(/$|/\?|\?|$)") ...
Enjoy.
Tagged with: caching • posix • regex • varnish
Jun 05
When using a BigIP to load balance a Sinatra app via Thin you will need to provide more than just the GET request string in F5’s “send string” field OR you might get “Request Invalid!” error message. Heads up.
Using this will cause issue:
GET /ops/heartbeat
This works:
GET /ops/heartbeat HTTP/1.1\r\nHost: \r\nConnection: Close\r\n\r\n
Tagged with: BigIP • F5 • healthcheck • Rails
Jun 05
Simple but effective regex to monitor when there are issues. In my example below, this will show all response times > 1 second:
tail -f production.log | egrep "Completed in [0-9]{3,10}"
Tagged with: logging • logs • Rails • regex
May 29
Introduction
At my current gig with Primedia, we have some pretty high volume Rails sites. Since having highly available sites are essential for our core business, we have to be very attentive to production issues. Below is a list that I’ve compiled recently in my head and by working with some very bright folks. This doc is to help Ops/Dev remember some basics and tricks when debugging issues. So remember to try this stuff.
Non-Technical (READ FIRST)
1. If the issue is not a major impact on production, make sure all your troubleshooting is passive! You might introduce an outage if you try serious troubleshooting.
2. Confirm the issue with Ops/Infrastructure in your presence, this gets buy-in from with the folks who have official root access.
3. As a developer, you should not work alone on production systems except to gather information (if you get temporary access). All changes (in seeking issue mitigation) should be paired with Ops/Infra. This will avoid finger-pointing and headaches later.
Technical
Elimination + log files is my troubleshooting method of choice.
1. Skip the load balancer if possible and hit direct IP/Port of a single app server to eliminate load balancer in mix.
2. Enable DEBUG log level and restart app server(s). Preferably, take an app server out of load balancer pool and work on in isolation. Regardless, DEBUG can slow response signficantly so be aware. However, there are situations where DEBUG is the only way to get an indication from Rails what is going on.
3. Tail log file of single app IP/Port to squelch noise from other servers and hit just that app server.
4. Make sure that the PIDs are not hung,old versions. Look at time/date of PIDS. If the deploy is screwed up, you can be deploying new code and yet the processes running are code from a month ago.
5. To be thorough, look at the kernel logs too. In Linux, /var/log/messages etc. You could have something else that is going on.
6. The logs should give status regarding database(s), but especially look for the words “timeout” if you have any custom network libraries that fetch data (we do) or “ActiveRecord”. Perhaps even, grep for it.
7. Use a non-browser client, like Curl or wget to eliminate possible browser issues. Case in point, we had an issue related to ActiveRecord sessions that were migrated to another datacenter. When accessed via Curl there was no issue (Curl does not save cookies by default).
Other
1. If you cannot duplicate issue in development, remember to change your configs to look like production mode and run your development workstation in production mode. Rails behaves different in its modes.
Tagged with: applications • debugging • production • Rails • ruby