Simplify watcher to not fork by using 'listen' in non-blocking mode
This commit is contained in:
parent
37fc5b2b8c
commit
dcc6c5b5e1
|
@ -523,7 +523,7 @@ module Middleman
|
||||||
#
|
#
|
||||||
# @return [Class]
|
# @return [Class]
|
||||||
def server(&block)
|
def server(&block)
|
||||||
@@servercounter ||= 1
|
@@servercounter ||= 0
|
||||||
@@servercounter += 1
|
@@servercounter += 1
|
||||||
const_set("MiddlemanApplication#{@@servercounter}", Class.new(Middleman::Application))
|
const_set("MiddlemanApplication#{@@servercounter}", Class.new(Middleman::Application))
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
# File changes are forwarded to the currently running app via HTTP
|
# File changes are forwarded to the currently running app via HTTP
|
||||||
require "net/http"
|
require "net/http"
|
||||||
|
|
||||||
module Middleman
|
|
||||||
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
|
|
||||||
end
|
|
||||||
|
|
||||||
require "win32/process" if ::Middleman::WINDOWS
|
|
||||||
|
|
||||||
require "fileutils"
|
require "fileutils"
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
|
@ -17,6 +11,7 @@ module Middleman
|
||||||
def start(options)
|
def start(options)
|
||||||
self.singleton = new(options)
|
self.singleton = new(options)
|
||||||
self.singleton.watch! unless options[:"disable-watcher"]
|
self.singleton.watch! unless options[:"disable-watcher"]
|
||||||
|
self.singleton.start
|
||||||
end
|
end
|
||||||
|
|
||||||
def ignore_list
|
def ignore_list
|
||||||
|
@ -35,15 +30,7 @@ module Middleman
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
register_signal_handlers
|
register_signal_handlers
|
||||||
start
|
|
||||||
end
|
|
||||||
|
|
||||||
# What command is sent to kill instances
|
|
||||||
# @return [Symbol, Fixnum]
|
|
||||||
def kill_command
|
|
||||||
::Middleman::WINDOWS ? 1 : "TERM"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def watch!
|
def watch!
|
||||||
|
@ -52,7 +39,8 @@ module Middleman
|
||||||
# Watcher Library
|
# Watcher Library
|
||||||
require "listen"
|
require "listen"
|
||||||
|
|
||||||
Listen.to(Dir.pwd, :relative_paths => true) do |modified, added, removed|
|
listener = Listen.to(Dir.pwd, :relative_paths => true)
|
||||||
|
listener.change do |modified, added, removed|
|
||||||
added_and_modified = modified + added
|
added_and_modified = modified + added
|
||||||
|
|
||||||
if added_and_modified.length > 0
|
if added_and_modified.length > 0
|
||||||
|
@ -63,46 +51,13 @@ module Middleman
|
||||||
local.run_on_deletion(removed)
|
local.run_on_deletion(removed)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
# Don't block this thread
|
||||||
|
listener.start(false)
|
||||||
def pid_name
|
|
||||||
".mm-pid-#{@options[:port]||4567}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def kill_pid!
|
|
||||||
if File.exists?(pid_name)
|
|
||||||
current_pid = File.open(pid_name, 'rb') { |f| f.read }
|
|
||||||
begin
|
|
||||||
Process.kill(kill_command, current_pid.to_i)
|
|
||||||
rescue
|
|
||||||
ensure
|
|
||||||
FileUtils.rm(pid_name) if File.exists?(pid_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Start Middleman in a fork
|
|
||||||
# @return [void]
|
|
||||||
def start
|
|
||||||
if @options[:"disable-watcher"]
|
|
||||||
bootup
|
|
||||||
else
|
|
||||||
kill_pid!
|
|
||||||
|
|
||||||
@server_job = fork {
|
|
||||||
# trap("INT") { exit(0) }
|
|
||||||
# trap("TERM") { exit(0) }
|
|
||||||
# trap("QUIT") { exit(0) }
|
|
||||||
bootup
|
|
||||||
}
|
|
||||||
|
|
||||||
File.open(pid_name, "w+") { |f| f.write(@server_job) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Start an instance of Middleman::Application
|
# Start an instance of Middleman::Application
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def bootup
|
def start
|
||||||
env = (@options[:environment] || "development").to_sym
|
env = (@options[:environment] || "development").to_sym
|
||||||
is_logging = @options.has_key?(:debug) && @options[:debug]
|
is_logging = @options.has_key?(:debug) && @options[:debug]
|
||||||
|
|
||||||
|
@ -124,12 +79,8 @@ module Middleman
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def stop
|
def stop
|
||||||
puts "== The Middleman is shutting down"
|
puts "== The Middleman is shutting down"
|
||||||
if !@options[:"disable-watcher"]
|
# TODO: Figure out some way to actually unload the whole thing
|
||||||
kill_pid!
|
# or maybe just re-exec this same thing
|
||||||
# Process.kill(::Middleman::WINDOWS ? :KILL : :TERM, @server_job)
|
|
||||||
# Process.wait @server_job
|
|
||||||
# @server_job = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Simply stop, then start
|
# Simply stop, then start
|
||||||
|
@ -172,7 +123,7 @@ module Middleman
|
||||||
trap("TERM") { stop }
|
trap("TERM") { stop }
|
||||||
trap("QUIT") { stop; exit(0) }
|
trap("QUIT") { stop; exit(0) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Whether the passed files are config.rb, lib/*.rb or helpers
|
# Whether the passed files are config.rb, lib/*.rb or helpers
|
||||||
# @param [Array<String>] paths Array of paths to check
|
# @param [Array<String>] paths Array of paths to check
|
||||||
# @return [Boolean] Whether the server needs to reload
|
# @return [Boolean] Whether the server needs to reload
|
||||||
|
|
Loading…
Reference in a new issue