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:
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: