class Guard::JekyllPlus::Server

Public Class Methods

new(config) click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 10
def initialize(config)
  @thread = nil
  @config = config
  @pid = nil
end

Public Instance Methods

start() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 16
def start
  @rack = rack_available?
  @rack ? start_rack : start_jekyll

  msg = 'watching and serving using %s at %s:%s%s'
  type = @rack ? 'rack' : 'jekyll'
  msg = format(msg, type, @config.host, @config.port, @config.baseurl)
  @config.info msg
end
stop() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 26
def stop
  @rack ? stop_rack : stop_jekyll
end

Private Instance Methods

config_file() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 49
def config_file
  (@config.rack_config || local_config || default_config)
end
default_config() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 45
def default_config
  (Pathname(__FILE__).expand_path.dirname + '../../rack/config.ru').to_s
end
local_config() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 41
def local_config
  File.exist?('config.ru') ? 'config.ru' : nil
end
rack_available?() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 32
def rack_available?
  @use_rack ||= begin
                   Kernel.require 'rack'
                   true
                 rescue LoadError
                   false
                 end
end
start_jekyll() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 53
def start_jekyll
  # NOTE: must use process for Jekyll, because:
  #
  # 1) webrat has a shutdown that needs to be called. Since Jekyll
  # doesn't expose the server instance, the only way to call it is to
  # send "INT" to the process - but that also causes Pygments to crash
  # (because it's opening a pipe with Mentos)
  #
  # 2) you can't use the 'detach' option of Jekyll, because you don't
  # have access to the pid, because it's only logged and never returned
  #
  # You'll likely get a "Couldn't cleanly terminate all actors" error,
  # because Celluloid doesn't gracefully handle forking.
  #
  fail "Server already running at: #{@pid.inspect}" unless @pid.nil?
  @pid = Process.fork do
    ::Jekyll::Commands::Serve.process(@config.jekyll_serve_options)
  end
end
start_rack() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 73
def start_rack
  fail 'already running!' unless @pid.nil?
  # Run rack in a process, because the only way to shut down Webrick
  # cleanly is through calling shutdown(), and Rack sets up and INT
  # handler specifically to do this - so we need to call INT.
  #
  # Webrick needs to shutdown
  # explicitly - and that's handled by Rack (it traps INT),
  ENV['RACK_ROOT'] = @config.server_root
  s = ::Rack::Server.new(config: config_file,
                         Port: @config.port,
                         Host: @config.host,
                         environment: @config.rack_environment)

  @config.info "Using: #{s.server} as server"

  thin = s.server == Rack::Handler::Thin
  Thin::Logging.silent = @config.rack_environment.nil? if thin

  @pid = Process.fork { s.start }
end
stop_jekyll() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 99
def stop_jekyll
  stop_pid
end
stop_pid() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 103
def stop_pid
  return if @pid.nil?
  Process.kill('INT', @pid)
  Process.wait(@pid)
  @pid = nil
end
stop_rack() click to toggle source
# File lib/guard/jekyll_plus/server.rb, line 95
def stop_rack
  stop_pid
end