Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add lacquer:varnishd:reload task for reloading the VCL config file

without restarting varnishd

This commit introduces a bin_path attribute on Lacquer::Varnishd which
the varnishadm_cmd method uses to construct the path to varnishadm. The
reload command is issued through the varnishadm management interface. If
varnishd is not running, reloading simply starts varnishd.
  • Loading branch information...
commit 16f765ea95fca05e3bd8b87357afeacb26bf2435 1 parent 6742317
@piinecone piinecone authored
View
6 README.rdoc
@@ -59,19 +59,22 @@ config/varnishd.yml
development:
listen: localhost:3001
telnet: localhost:6082
- sbin_path: /usr/local/sbin
+ sbin_path: /usr/local/sbin # path to varnishd
+ bin_path: /usr/local/bin # path to varnishadm
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
test:
listen: localhost:3002
telnet: localhost:6083
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
production:
listen: :80
telnet: localhost:6082
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
params:
overflow_max: 2000 # for Varnish 2.x ... use "queue_max: 2000" for Varnish 3.x
@@ -188,6 +191,7 @@ Control varnishd with the following rake tasks
rake lacquer:varnishd:start
rake lacquer:varnishd:stop
rake lacquer:varnishd:restart
+ rake lacquer:varnishd:reload
rake lacquer:varnishd:status
rake lacquer:varnishd:global_purge
View
3  lib/generators/lacquer/templates/varnishd.yml
@@ -3,6 +3,7 @@ development:
telnet: 127.0.0.1:6082
backend: 127.0.0.1:3000
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
test:
@@ -10,6 +11,7 @@ test:
telnet: 127.0.0.1:6083
backend: 127.0.0.1:3000
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
production:
@@ -18,6 +20,7 @@ production:
backend: backend_server:8080
use_sudo: false
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
pid_path: <%= Rails.root.join('log') %>
storage: "file,#{Rails.root}/log/varnishd.#{Rails.env}.cache,100MB"
params:
View
6 lib/lacquer/tasks.rb
@@ -29,6 +29,12 @@
varnishd.start
end
+ desc "Reload VCL configuration through varnishadm with Lacquer's settings"
+ task :reload => :environment do
+ varnishd = Lacquer::Varnishd.new
+ varnishd.reload
+ end
+
desc "Purge ALL urls from Varnish"
task :global_purge => :environment do
Lacquer::Varnish.new.purge('.*')
View
22 lib/lacquer/varnishd.rb
@@ -1,6 +1,6 @@
module Lacquer
class Varnishd
- attr_accessor :listen, :telnet, :sbin_path, :storage, :working_dir, :user, :backend, :params, :use_sudo, :pid_path
+ attr_accessor :listen, :telnet, :sbin_path, :bin_path, :storage, :working_dir, :user, :backend, :params, :use_sudo, :pid_path
cattr_accessor :started_check_delay, :vcl_script_filename
self.started_check_delay = 1
@@ -23,8 +23,8 @@ def self.config
end
def initialize(settings = self.class.config)
- self.listen, self.telnet, self.backend, self.sbin_path, self.storage, self.working_dir, self.user, self.params, self.use_sudo, self.pid_path =
- settings.values_at("listen", "telnet", "backend", "sbin_path", "storage", "working_dir", "user", "params", "use_sudo", "pid_path")
+ self.listen, self.telnet, self.backend, self.sbin_path, self.bin_path, self.storage, self.working_dir, self.user, self.params, self.use_sudo, self.pid_path =
+ settings.values_at("listen", "telnet", "backend", "sbin_path", "bin_path", "storage", "working_dir", "user", "params", "use_sudo", "pid_path")
end
def render_vcl
@@ -62,6 +62,18 @@ def stop
end
end
+ def reload
+ if running?
+ generate_vcl
+ reload_id = "reload#{Time.now.usec}"
+ load_cmd = "#{varnishadm_cmd} vcl.load #{reload_id} #{options['-f']}"
+ use_cmd = "#{varnishadm_cmd} vcl.use #{reload_id}"
+ execute "#{load_cmd} && #{use_cmd}"
+ else
+ start
+ end
+ end
+
def running?
!!pid && !!execute("ps p #{pid}").include?(pid.to_s) # works with sudo
end
@@ -96,6 +108,10 @@ def varnishd_cmd
"#{'sudo ' if use_sudo}#{Pathname.new(sbin_path).join('varnishd')}"
end
+ def varnishadm_cmd
+ "#{'sudo ' if use_sudo}#{Pathname.new(bin_path).join('varnishadm')} -T #{options['-T']}"
+ end
+
def pid_file
pid_computed_path.join("varnishd.#{self.class.env}.pid")
end
View
1  spec/config/varnishd.yml
@@ -2,6 +2,7 @@ test:
listen: :80
telnet: localhost:6082
sbin_path: /usr/local/sbin
+ bin_path: /usr/local/bin
backend: 0.0.0.0:3000
storage: "file,#{Rails.root}/log/varnish,1GB"
working_dir: ""
View
42 spec/lacquer/varnishd_spec.rb
@@ -26,6 +26,7 @@ def executes_with(regexp)
Lacquer::Varnishd.config.should have_key("listen")
Lacquer::Varnishd.config.should have_key("telnet")
Lacquer::Varnishd.config.should have_key("sbin_path")
+ Lacquer::Varnishd.config.should have_key("bin_path")
Lacquer::Varnishd.config.should have_key("storage")
Lacquer::Varnishd.config.should have_key("use_sudo")
Lacquer::Varnishd.config["params"].should have_key('overflow_max')
@@ -77,4 +78,45 @@ def executes_with(regexp)
result.should include('.host = "0.0.0.0"')
result.should include('.port = "3000"')
end
+
+ describe '#reload' do
+ def expect_reload_cmd(attributes)
+ Time.stub(:now).and_return Time.parse('October 6th, 1984')
+ varnishadm_cmd = "#{attributes['bin_path']}/varnishadm -T #{attributes['telnet']}"
+ reload_id = "reload#{Time.now.usec}"
+ load_cmd = "#{varnishadm_cmd} vcl.load #{reload_id} config/generate.vcl"
+ use_cmd = "#{varnishadm_cmd} vcl.use #{reload_id}"
+
+ executes_with "#{load_cmd} && #{use_cmd}"
+ end
+
+ context 'given varnishd is running' do
+ before do
+ attributes = { "sbin_path" => "/opt/varnishd/sbin", "bin_path" => "/opt/bin", "telnet" => "localhost:6082" }
+ expect_reload_cmd attributes
+ @varnishd = Lacquer::Varnishd.new attributes
+ @varnishd.stub(:vcl_script_filename).and_return("config/generate.vcl")
+ @varnishd.stub(:running?).and_return true
+ end
+
+ it 'executes the varnishadm reload commands' do
+ @varnishd.should_receive :generate_vcl
+ end
+
+ after do
+ @varnishd.reload
+ end
+ end
+
+ context 'given varnishd is not running' do
+ before do
+ executes_with(%r[/opt/varnishd/sbin/varnishd])
+ @varnishd = Lacquer::Varnishd.new("sbin_path" => "/opt/varnishd/sbin")
+ end
+
+ it 'executes the varnishadm reload commands' do
+ @varnishd.reload
+ end
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.