Class: Lacquer::Varnish
- Inherits:
-
Object
- Object
- Lacquer::Varnish
- Defined in:
- lib/lacquer/varnish.rb
Instance Method Summary collapse
-
#purge(path) ⇒ Object
Sends the command ‘url.purge path’.
-
#send_command(command) ⇒ Object
Sends commands over telnet to varnish servers listed in the config.
- #stats ⇒ Object
Instance Method Details
#purge(path) ⇒ Object
Sends the command ‘url.purge path’
15 16 17 18 19 |
# File 'lib/lacquer/varnish.rb', line 15 def purge(path) send_command('url.purge ' << path).all? do |result| result =~ /200/ end end |
#send_command(command) ⇒ Object
Sends commands over telnet to varnish servers listed in the config.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lacquer/varnish.rb', line 22 def send_command(command) Lacquer.configuration.varnish_servers.collect do |server| retries = 0 response = nil begin retries += 1 connection = Net::Telnet.new( 'Host' => server[:host], 'Port' => server[:port], 'Timeout' => server[:timeout] || 5) if(server[:secret]) connection.waitfor("Match" => /^107/) do |authentication_request| matchdata = /^107 \d{2}\s*(.{32}).*$/m.match(authentication_request) # Might be a bit ugly regex, but it works great! salt = matchdata[1] if(salt.empty?) raise VarnishError, "Bad authentication request" end digest = OpenSSL::Digest::Digest.new('sha256') digest << salt digest << "\n" digest << server[:secret] digest << "\n" digest << salt digest << "\n" connection.cmd("String" => "auth #{digest.to_s}", "Match" => /\d{3}/) do |auth_response| if(!(/^200/ =~ auth_response)) raise AuthenticationError, "Could not authenticate" end end end end connection.cmd('String' => command, 'Match' => /\n\n/) {|r| response = r.split("\n").first.strip} connection.close rescue Exception => e if retries < Lacquer.configuration.retries retry else if Lacquer.configuration.command_error_handler Lacquer.configuration.command_error_handler.call({ :error_class => "Varnish Error, retried #{Lacquer.configuration.retries} times", :error_message => "Error while trying to connect to #{server[:host]}:#{server[:port]}: #{e}", :parameters => server, :response => response }) else raise VarnishError.new("Error while trying to connect to #{server[:host]}:#{server[:port]} #{e}") end end end response end end |
#stats ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/lacquer/varnish.rb', line 3 def stats send_command('stats').collect do |stats| stats = stats.split("\n") stats.shift stats = stats.collect do |stat| stat = stat.strip.match(/(\d+)\s+(.+)$/) { :key => stat[2], :value => stat[1] } if stat end end end |