Class: Lacquer::Varnishd
- Inherits:
-
Object
- Object
- Lacquer::Varnishd
- Defined in:
- lib/lacquer/varnishd.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#bin_path ⇒ Object
Returns the value of attribute bin_path.
-
#listen ⇒ Object
Returns the value of attribute listen.
-
#params ⇒ Object
Returns the value of attribute params.
-
#pid_path ⇒ Object
Returns the value of attribute pid_path.
-
#sbin_path ⇒ Object
Returns the value of attribute sbin_path.
-
#storage ⇒ Object
Returns the value of attribute storage.
-
#telnet ⇒ Object
Returns the value of attribute telnet.
-
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
-
#user ⇒ Object
Returns the value of attribute user.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #args ⇒ Object
- #generate_vcl ⇒ Object
-
#initialize(settings = self.class.config) ⇒ Varnishd
constructor
A new instance of Varnishd.
- #options ⇒ Object
- #params_args ⇒ Object
- #reload ⇒ Object
- #render_vcl ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(settings = self.class.config) ⇒ Varnishd
Returns a new instance of Varnishd.
25 26 27 28 |
# File 'lib/lacquer/varnishd.rb', line 25 def initialize(settings = self.class.config) 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 |
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def backend @backend end |
#bin_path ⇒ Object
Returns the value of attribute bin_path.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def bin_path @bin_path end |
#listen ⇒ Object
Returns the value of attribute listen.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def listen @listen end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def params @params end |
#pid_path ⇒ Object
Returns the value of attribute pid_path.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def pid_path @pid_path end |
#sbin_path ⇒ Object
Returns the value of attribute sbin_path.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def sbin_path @sbin_path end |
#storage ⇒ Object
Returns the value of attribute storage.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def storage @storage end |
#telnet ⇒ Object
Returns the value of attribute telnet.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def telnet @telnet end |
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def use_sudo @use_sudo end |
#user ⇒ Object
Returns the value of attribute user.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def user @user end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
3 4 5 |
# File 'lib/lacquer/varnishd.rb', line 3 def working_dir @working_dir end |
Class Method Details
.config ⇒ Object
21 22 23 |
# File 'lib/lacquer/varnishd.rb', line 21 def self.config YAML.load(File.read(config_file))[env].stringify_keys end |
.config_file ⇒ Object
17 18 19 |
# File 'lib/lacquer/varnishd.rb', line 17 def self.config_file root_path.join('config/varnishd.yml') end |
.env ⇒ Object
13 14 15 |
# File 'lib/lacquer/varnishd.rb', line 13 def self.env Rails.env end |
.root_path ⇒ Object
9 10 11 |
# File 'lib/lacquer/varnishd.rb', line 9 def self.root_path Rails.root end |
Instance Method Details
#args ⇒ Object
81 82 83 |
# File 'lib/lacquer/varnishd.rb', line 81 def args .map { |k, v| "#{k} #{v}" }.join(" ") end |
#generate_vcl ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/lacquer/varnishd.rb', line 36 def generate_vcl if erb_vcl_script_filename.exist? log "#{erb_vcl_script_filename} found rendering to #{vcl_script_filename}" File.open(vcl_script_filename, "w") do |vcl| vcl.write(render_vcl) end end end |
#options ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lacquer/varnishd.rb', line 89 def opt = {} opt["-P"] = pid_file opt["-a"] = listen opt["-T"] = telnet if telnet.present? opt["-n"] = working_dir if working_dir.present? opt["-u"] = user if user.present? opt["-s"] = eval(%Q("#{storage}")) opt["-f"] = vcl_script_filename opt end |
#params_args ⇒ Object
85 86 87 |
# File 'lib/lacquer/varnishd.rb', line 85 def params_args params.map { |k, v| "-p #{k}=#{v}" }.join(" ") end |
#reload ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lacquer/varnishd.rb', line 65 def reload if running? generate_vcl reload_id = "reload#{Time.now.usec}" load_cmd = "#{varnishadm_cmd} vcl.load #{reload_id} #{['-f']}" use_cmd = "#{varnishadm_cmd} vcl.use #{reload_id}" execute "#{load_cmd} && #{use_cmd}" else start end end |
#render_vcl ⇒ Object
30 31 32 33 34 |
# File 'lib/lacquer/varnishd.rb', line 30 def render_vcl require 'erubis' eruby = Erubis::Eruby.new(erb_vcl_script_filename.read) eruby.result(binding) end |
#running? ⇒ Boolean
77 78 79 |
# File 'lib/lacquer/varnishd.rb', line 77 def running? !!pid && !!execute("ps p #{pid}").include?(pid.to_s) # works with sudo end |
#start ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lacquer/varnishd.rb', line 45 def start if running? log("Already running") return end generate_vcl execute("#{varnishd_cmd} #{args} #{params_args}") sleep(self.class.started_check_delay) log("Failed to start varnishd daemon") unless running? end |
#stop ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/lacquer/varnishd.rb', line 56 def stop if running? execute("#{'sudo ' if use_sudo}kill #{pid}") pid_file.delete else log("pid file not found or varnishd not running") end end |