Merge pull request #1877 from middleman/bugfix/restarting-external-pipeline

Allow restarting external-pipeline without orphaning processes
This commit is contained in:
Thomas Reynolds 2016-04-12 10:23:38 -07:00
commit 4791e01f10
4 changed files with 49 additions and 12 deletions

View file

@ -235,7 +235,8 @@ module Middleman
:before, # Before Rack requests :before, # Before Rack requests
:before_render, :before_render,
:after_render, :after_render,
:before_server :before_server,
:reload
]) ])
@middleware = Set.new @middleware = Set.new

View file

@ -12,6 +12,7 @@ class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
return if app.mode?(:config) return if app.mode?(:config)
require 'servolux'
require 'thread' require 'thread'
require 'fileutils' require 'fileutils'
@ -25,31 +26,63 @@ class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
latency: options[:latency], latency: options[:latency],
frontmatter: false frontmatter: false
@current_thread = nil
app.reload(&method(:reload!))
logger.info "== Executing: `#{options[:command]}`" logger.info "== Executing: `#{options[:command]}`"
if app.build? || options[:disable_background_execution] if app.build? || options[:disable_background_execution]
watch_command! watch_command!(false)
@watcher.poll_once!
else else
::Thread.new { watch_command! } watch_command!(true)
end end
end end
def watch_command! def reload!
::IO.popen(options[:command], 'r') do |pipe| if @current_thread
while buf = pipe.gets logger.info "== Stopping: `#{options[:command]}`"
@current_thread.stop
@current_thread = nil
end
end
def watch_command!(async)
@current_thread = ::Servolux::Child.new(
command: options[:command],
suspend: 2
)
@current_thread.start
watch_thread = Thread.new do
while buf = @current_thread.io.gets
without_newline = buf.sub(/\n$/, '') without_newline = buf.sub(/\n$/, '')
logger.info "== External: #{without_newline}" unless without_newline.empty? logger.info "== External: #{without_newline}" unless without_newline.empty?
end end
end
unless $?.success? @current_thread.wait
if !@current_thread.exitstatus.nil? && @current_thread.exitstatus != 0
logger.error '== External: Command failed with non-zero exit status' logger.error '== External: Command failed with non-zero exit status'
exit(1) exit(1)
end end
end
@watcher.poll_once! watch_thread.join unless async
rescue ::Errno::ENOENT => e rescue ::Errno::ENOENT => e
logger.error "== External: Command failed with message: #{e.message}" logger.error "== External: Command failed with message: #{e.message}"
exit(1) exit(1)
end end
private
def print_command(stdout)
while buf = stdout.gets
without_newline = buf.sub(/\n$/, '')
logger.info "== External: #{without_newline}" unless without_newline.empty?
end
end
end end

View file

@ -103,6 +103,8 @@ module Middleman
def reload def reload
app.logger.info '== The Middleman is reloading' app.logger.info '== The Middleman is reloading'
app.execute_callbacks(:reload)
begin begin
app = initialize_new_app app = initialize_new_app
rescue => e rescue => e

View file

@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_dependency('erubis') s.add_dependency('erubis')
s.add_dependency('fast_blank') s.add_dependency('fast_blank')
s.add_dependency('parallel') s.add_dependency('parallel')
s.add_dependency('servolux')
# Helpers # Helpers
s.add_dependency('activesupport', ['~> 4.2']) s.add_dependency('activesupport', ['~> 4.2'])