Class: Lacquer::Varnishd

Inherits:
Object
  • Object
show all
Defined in:
lib/lacquer/varnishd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#backendObject

Returns the value of attribute backend.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def backend
  @backend
end

#bin_pathObject

Returns the value of attribute bin_path.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def bin_path
  @bin_path
end

#listenObject

Returns the value of attribute listen.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def listen
  @listen
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def params
  @params
end

#pid_pathObject

Returns the value of attribute pid_path.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def pid_path
  @pid_path
end

#sbin_pathObject

Returns the value of attribute sbin_path.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def sbin_path
  @sbin_path
end

#storageObject

Returns the value of attribute storage.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def storage
  @storage
end

#telnetObject

Returns the value of attribute telnet.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def telnet
  @telnet
end

#use_sudoObject

Returns the value of attribute use_sudo.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def use_sudo
  @use_sudo
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/lacquer/varnishd.rb', line 3

def user
  @user
end

#working_dirObject

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

.configObject



21
22
23
# File 'lib/lacquer/varnishd.rb', line 21

def self.config
  YAML.load(File.read(config_file))[env].stringify_keys
end

.config_fileObject



17
18
19
# File 'lib/lacquer/varnishd.rb', line 17

def self.config_file
  root_path.join('config/varnishd.yml')
end

.envObject



13
14
15
# File 'lib/lacquer/varnishd.rb', line 13

def self.env
  Rails.env
end

.root_pathObject



9
10
11
# File 'lib/lacquer/varnishd.rb', line 9

def self.root_path
  Rails.root
end

Instance Method Details

#argsObject



81
82
83
# File 'lib/lacquer/varnishd.rb', line 81

def args
  options.map { |k, v| "#{k} #{v}" }.join(" ")
end

#generate_vclObject



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

#optionsObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lacquer/varnishd.rb', line 89

def options
  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_argsObject



85
86
87
# File 'lib/lacquer/varnishd.rb', line 85

def params_args
  params.map { |k, v| "-p #{k}=#{v}" }.join(" ")
end

#reloadObject



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} #{options['-f']}"
    use_cmd = "#{varnishadm_cmd} vcl.use #{reload_id}"
    execute "#{load_cmd} && #{use_cmd}"
  else
    start
  end
end

#render_vclObject



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

Returns:

  • (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

#startObject



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

#stopObject



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