From 599db803dcd038f5158e8ba8bb49107a0e608b16 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 29 Sep 2011 12:18:20 -0400 Subject: [PATCH] add varnishncsa logging --- lib/guard/lacquer.rb | 31 +++++++++++++++++---- lib/guard/lacquer/varnishd.rb | 17 ++++++++++-- lib/guard/lacquer/varnishncsa.rb | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 lib/guard/lacquer/varnishncsa.rb diff --git a/lib/guard/lacquer.rb b/lib/guard/lacquer.rb index 919d45d..80873bd 100644 --- a/lib/guard/lacquer.rb +++ b/lib/guard/lacquer.rb @@ -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 diff --git a/lib/guard/lacquer/varnishd.rb b/lib/guard/lacquer/varnishd.rb index c79776c..156746a 100644 --- a/lib/guard/lacquer/varnishd.rb +++ b/lib/guard/lacquer/varnishd.rb @@ -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 diff --git a/lib/guard/lacquer/varnishncsa.rb b/lib/guard/lacquer/varnishncsa.rb new file mode 100644 index 0000000..e217143 --- /dev/null +++ b/lib/guard/lacquer/varnishncsa.rb @@ -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 +