Gravatar Problems

August 25th, 2007

When I switched the blog to Mephisto I had installed the Gravatar caching plugin. It seemed to be working fine prior to deployment, but once in production it was not working as expected. Today I finally tried to track down what was wrong. hopefully this will fix any problems with Gravatar images, creating comments, etc. I won’t be so bold as to say this will work for everyone, but it works for me :)

My first problem was that Gravatar images were not displaying at all, even when the email address had an associated Gravatar and the plugin had cached the file. I could navigate to the image directly and see it was correct, but the plugin was not finding it. After some debugging, I noticed the file exists check was using a relative path. By changing this to an absolute path I was able to see images. Woohoo!

lib/mephisto_gravatar_cache.rb (line 14)

- if File.exists?("#{GravatarAPI.cache_dir}#{md5_email}.gif")
+ if File.exists?(File.expand_path("../#{GravatarAPI.cache_dir}#{md5_email}.gif"))

Next up was an annoying error message that was appearing whenever someone posted a comment. It looked something like this:

Cache Gravatar for => somebody@gmail.com
=> gravatar NOT FOUND at www.gravatar.com
Content-Type: text/html; charset=utf-8
Status: 302 Found
Location: http://millarian.com/2007/8/22/striving-for-100-percent/comments/121#comment-121
X-Runtime: 8.55324
Cache-Control: no-cache
Content-Length: 146

<html><body>You are being <a href="http://millarian.com/2007/8/22/striving-for-100-percent/comments/121#comment-121">redirected</a>.</body></html>

Same thing happened even when a Gravatar image was found… Nobody was safe from this problem. I did a search in the source for where “gravatar NOT FOUND” was being output. This message was being logged and put to the console, so I simply wrapped it in a conditional to output the results only in development mode, like so:

lib/gravatar_api.rb

def self.explain(msg)
  # create a new logger if it doesnt already exist
+ if ENV['RAILS_ENV'] == 'development'
    log.info(msg)
    puts(msg)
+ end
end

This seemed to solve that problem.

While I was exploring the Gravatar cache plugin I also rewrote the cache_gravatars rake task to be a little more efficient and, since I exclude the explain for production, to output a listing of what’s happening.

tasks/mephisto_gravatar_cache_tasks.rake

desc "WGET all gravatars for all email addresses in contents table (comments). Call with RAILS_ENV=production (else defaults to development env)" 
task :cache_gravatars => :environment do |t|

  # get all the comments that have an email address
  ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
- comments = (Comment.find :all).collect {|c| c if c.author_email?}.compact
+ comments = Comment.find(:all, :select => "author_email", :conditions => "author_email IS NOT NULL AND author_email != ''", :group => "author_email")

+ puts "Caching Gravatars for:" 

  # try caching a gravatar for each comment
  comments.each do |comment|
+   puts " - #{comment.author_email}" 
    GravatarAPI.cache_gravatar comment.author_email

    # be nice to gravatar.com, wait a second before next request
    sleep(1)
  end
end

Please let me know of any other oddities using the information in about. Thanks!

Server Failure

June 27th, 2007

Yep, my home server’s hard drive crashed about a week ago. As you can see, the site is back up and in tip-top condition. In fact, it may be even better than it was previously. Well, that’s not really hard to imagine is it?

I have moved to a hosted solution through Hosting Rails. So far, they have been awesome (and cheap). If you’re interested in trying them out, they gave me an affiliates link for referrals.

I have also switched over from Wordpress to Mephisto. I had wanted to try it out and this seemed like the perfect opportunity to jump right in. So far I am enjoying playing with Mephisto. Because it’s a Ruby on Rails application, I feel pretty comfortable digging into the source code. I think I have everything back the way I want it, but I welcome any suggestions for improvement.

Let me tell you, when that hard drive crashed (click, click, click… grrrrinnd) I was not all that concerned because I do fairly regular backups. If you haven’t backed up recently, I suggest you take the time right now to do so. I only lost a few posts and I was able to get those back from Google cache (Thanks Google!). The few pieces of data I was unable to retrieve were a couple of recent comments, so if you left a comment recently, then feel free to enter it again! If you haven’t left a comment recently, then now is the time to start.