Cleanup LiveReloadReactor (#6607)

Merge pull request 6607
This commit is contained in:
ashmaroli 2017-12-15 02:13:18 +05:30 committed by jekyllbot
parent 71e3bce63d
commit acb82c92a8
1 changed files with 20 additions and 46 deletions

View File

@ -14,7 +14,6 @@ module Jekyll
attr_reader :thread attr_reader :thread
def initialize def initialize
@thread = nil
@websockets = [] @websockets = []
@connections_count = 0 @connections_count = 0
@started_event = Utils::ThreadEvent.new @started_event = Utils::ThreadEvent.new
@ -25,7 +24,7 @@ module Jekyll
# There is only one EventMachine instance per Ruby process so stopping # There is only one EventMachine instance per Ruby process so stopping
# it here will stop the reactor thread we have running. # it here will stop the reactor thread we have running.
EM.stop if EM.reactor_running? EM.stop if EM.reactor_running?
Jekyll.logger.debug("LiveReload Server:", "halted") Jekyll.logger.debug "LiveReload Server:", "halted"
end end
def running? def running?
@ -33,32 +32,18 @@ module Jekyll
end end
def handle_websockets_event(ws) def handle_websockets_event(ws)
ws.onopen do |handshake| ws.onopen { |handshake| connect(ws, handshake) }
connect(ws, handshake) ws.onclose { disconnect(ws) }
end ws.onmessage { |msg| print_message(msg) }
ws.onerror { |error| log_error(error) }
ws.onclose do
disconnect(ws)
end
ws.onmessage do |msg|
print_message(msg)
end
ws.onerror do |error|
log_error(error)
end
end end
# rubocop:disable Metrics/MethodLength
def start(opts) def start(opts)
@thread = Thread.new do @thread = Thread.new do
# Use epoll if the kernel supports it # Use epoll if the kernel supports it
EM.epoll EM.epoll
EM.run do EM.run do
EM.error_handler do |e| EM.error_handler { |e| log_error(e) }
log_error(e)
end
EM.start_server( EM.start_server(
opts["host"], opts["host"],
@ -70,17 +55,11 @@ module Jekyll
end end
# Notify blocked threads that EventMachine has started or shutdown # Notify blocked threads that EventMachine has started or shutdown
EM.schedule do EM.schedule { @started_event.set }
@started_event.set EM.add_shutdown_hook { @stopped_event.set }
end
EM.add_shutdown_hook do Jekyll.logger.info "LiveReload address:",
@stopped_event.set "http://#{opts["host"]}:#{opts["livereload_port"]}"
end
Jekyll.logger.info(
"LiveReload address:", "#{opts["host"]}:#{opts["livereload_port"]}"
)
end end
end end
@thread.abort_on_exception = true @thread.abort_on_exception = true
@ -90,17 +69,15 @@ module Jekyll
# http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol # http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol
def reload(pages) def reload(pages)
pages.each do |p| pages.each do |p|
msg = { json_message = JSON.dump({
:command => "reload", :command => "reload",
:path => p.url, :path => p.url,
:liveCSS => true, :liveCSS => true,
} })
Jekyll.logger.debug("LiveReload:", "Reloading #{p.url}") Jekyll.logger.debug "LiveReload:", "Reloading #{p.url}"
Jekyll.logger.debug(JSON.dump(msg)) Jekyll.logger.debug "", json_message
@websockets.each do |ws| @websockets.each { |ws| ws.send(json_message) }
ws.send(JSON.dump(msg))
end
end end
end end
@ -110,7 +87,7 @@ module Jekyll
if @connections_count == 1 if @connections_count == 1
message = "Browser connected" message = "Browser connected"
message += " over SSL/TLS" if handshake.secure? message += " over SSL/TLS" if handshake.secure?
Jekyll.logger.info("LiveReload:", message) Jekyll.logger.info "LiveReload:", message
end end
ws.send( ws.send(
JSON.dump( JSON.dump(
@ -134,18 +111,15 @@ module Jekyll
# Not sure what the 'url' command even does in LiveReload. The spec is silent # Not sure what the 'url' command even does in LiveReload. The spec is silent
# on its purpose. # on its purpose.
if msg["command"] == "url" if msg["command"] == "url"
Jekyll.logger.info("LiveReload:", "Browser URL: #{msg["url"]}") Jekyll.logger.info "LiveReload:", "Browser URL: #{msg["url"]}"
end end
end end
private private
def log_error(e) def log_error(e)
Jekyll.logger.warn( Jekyll.logger.error "LiveReload experienced an error. " \
"LiveReload experienced an error. "\ "Run with --trace for more information."
"Run with --verbose for more information." raise e
)
Jekyll.logger.debug("LiveReload Error:", e.message)
Jekyll.logger.debug("LiveReload Error:", e.backtrace.join("\n"))
end end
end end
end end