add varnishncsa logging

This commit is contained in:
John Bintz 2011-09-29 12:18:20 -04:00
parent b21cfe7090
commit 599db803dc
3 changed files with 87 additions and 8 deletions

View File

@ -6,6 +6,7 @@ require 'fileutils'
module Guard
class Lacquer < Guard
autoload :Varnishd, 'guard/lacquer/varnishd'
autoload :VarnishNCSA, 'guard/lacquer/varnishncsa'
def initialize(watchers = [], options = {})
super
@ -14,11 +15,27 @@ module Guard
:port => 3001,
:backend => '127.0.0.1:3000',
:storage => 'file,tmp/cache/varnish.store,32M',
:sbin_path => File.split(`which varnishd`.strip).first,
:pid_file => 'tmp/pids/varnish.pid'
:pid_dir => 'tmp/pids',
:log_dir => 'log',
:name => File.split(Dir.pwd).last
}.merge(options)
@backend = Varnishd.new(@options)
if @options[:varnish_path]
@options.merge!(
:sbin_path => File.join(@options[:varnish_path], 'sbin'),
:bin_path => File.join(@options[:varnish_path], 'bin'),
)
else
@options.merge!(
:sbin_path => File.split(`which varnishd`.strip).first,
:bin_path => File.split(`which varnishncsa`.strip).first
)
end
p @options
@frontend = Varnishd.new(@options)
@logger = VarnishNCSA.new(@options)
if !File.file?(varnish_erb = 'config/varnish.vcl.erb')
UI.info "No config/varnish.vcl.erb found, copying default from Lacquer..."
@ -29,14 +46,16 @@ module Guard
end
def start
@backend.stop if @backend.running?
@backend.start
[ @frontend, @logger ].each do |which|
which.stop if which.running?
which.start
end
notify "Varnish started on port #{@options[:port]}, with backend #{@options[:backend]}."
end
def stop
@backend.stop
[ @frontend, @logger ].each(&:stop)
notify "Until next time..."
end

View File

@ -1,8 +1,10 @@
require 'guard/lacquer'
require 'lacquer'
require 'lacquer/varnishd'
class Guard::Lacquer::Varnishd < ::Lacquer::Varnishd
attr_accessor :pid_file
attr_accessor :pid_dir
def self.root_path
Pathname.new(Dir.pwd)
@ -12,14 +14,25 @@ class Guard::Lacquer::Varnishd < ::Lacquer::Varnishd
ENV['RAILS_ENV'] || 'development'
end
def self.app_name
self.name.downcase.split("::").last
end
def initialize(options)
if options[:backend].split(':').first.empty?
options[:backend] = "127.0.0.1#{options[:backend]}"
end
self.pid_file = self.class.root_path.join(options[:pid_file])
options[:working_dir] ||= options[:name]
self.pid_dir = options[:pid_dir] || 'tmp/pids'
options[:listen] = "127.0.0.1:#{options[:port]}"
super(Hash[options.collect { |k, v| [ k.to_s, v ] }])
end
def pid_file
self.class.root_path.join(self.pid_dir).join("#{self.class.app_name}.#{self.class.env}.pid")
end
end

View File

@ -0,0 +1,47 @@
require 'guard/lacquer'
class Guard::Lacquer
class VarnishNCSA < Varnishd
attr_accessor :bin_path, :log_dir
def initialize(settings)
settings[:working_dir] ||= settings[:name]
self.working_dir, self.bin_path, self.log_dir = settings[:working_dir], settings[:bin_path], settings[:log_dir]
self.pid_dir = settings[:pid_dir] || 'tmp/pids'
end
def start
if running?
log("Aready running")
return
end
execute("#{varnishncsa_cmd} #{args}")
end
def stop
if running?
execute("kill #{pid}")
pid_file.delete
else
log("pid file not found or #{self.class.app_name} not running")
end
end
def options
opt = {}
opt["-P"] = pid_file
opt["-n"] = working_dir if working_dir.present?
opt["-a"] = '-D'
opt["-w"] = self.class.root_path.join(self.log_dir).join("varnishncsa.#{self.class.env}.log")
opt
end
protected
def varnishncsa_cmd
Pathname.new(self.bin_path).join('varnishncsa')
end
end
end