add listen back
arnaud sellenet
committed Apr 14, 2014
commit da7b19881340604d265c235e06eff1da03124b45
Showing 2
changed files with
70 additions
and 3 deletions
locomotive/wagon.rb b/lib/locomotive/wagon.rb
+6
-3
| @@ | @@ -1,5 +1,8 @@ |
| require 'locomotive/wagon/version' | |
| require 'locomotive/wagon/logger' | |
| + | require 'locomotive/wagon/listen' |
| + | require 'better_errors' |
| + | |
| module Locomotive | |
| module Wagon | |
| @@ | @@ -53,7 +56,7 @@ module Locomotive |
| use_listen = Process.pid != parent_pid && !options[:disable_listen] | |
| end | |
| - | Locomotive::Steam::Listen.instance.start(reader) if use_listen |
| + | Locomotive::Wagon::Listen.instance.start(reader) if use_listen |
| server.start | |
| end | |
| @@ | @@ -180,11 +183,11 @@ module Locomotive |
| # @param [ Object ] An instance of the reader is the get_reader parameter has been set. | |
| # | |
| def self.require_mounter(path, get_reader = false) | |
| - | Locomotive::Wagon::Logger.setup(path, false) |
| + | Locomotive::Common::Logger.setup(path, false) |
| require 'locomotive/mounter' | |
| - | Locomotive::Mounter.logger = Locomotive::Wagon::Logger.instance.logger |
| + | Locomotive::Mounter.logger = Locomotive::Common::Logger.instance.logger |
| if get_reader | |
| begin | |
locomotive/wagon/listen.rb b/lib/locomotive/wagon/listen.rb
+64
-0
| @@ | @@ -0,0 +1,64 @@ |
| + | require 'listen' |
| + | |
| + | module Locomotive::Wagon |
| + | class Listen |
| + | |
| + | attr_accessor :reader |
| + | |
| + | def self.instance |
| + | @@instance = new |
| + | end |
| + | |
| + | def start(reader) |
| + | # if $parent_pid && $parent_pid == Process.pid |
| + | # puts "bypassing Listen in the parent process" |
| + | # return false |
| + | # end |
| + | |
| + | puts "Listening here: #{Process.pid}" |
| + | |
| + | self.reader = reader |
| + | |
| + | self.definitions.each do |definition| |
| + | self.apply(definition) |
| + | end |
| + | end |
| + | |
| + | def definitions |
| + | [ |
| + | ['config', /\.yml/, [:site, :content_types, :pages, :snippets, :content_entries, :translations]], |
| + | ['app/views', /\.liquid/, [:pages, :snippets]], |
| + | ['app/content_types', /\.yml/, [:content_types, :content_entries]], |
| + | ['data', /\.yml/, :content_entries] |
| + | ] |
| + | end |
| + | |
| + | protected |
| + | |
| + | def apply(definition) |
| + | reloader = Proc.new do |modified, added, removed| |
| + | resources = [*definition.last] |
| + | names = resources.map { |n| "\"#{n}\"" }.join(', ') |
| + | |
| + | Locomotive::Common::Logger.info "* Reloaded #{names} at #{Time.now}" |
| + | |
| + | begin |
| + | reader.reload(resources) |
| + | rescue Exception => e |
| + | Locomotive::Common::MounterException.new('Unable to reload', e) |
| + | end |
| + | end |
| + | |
| + | filter = definition[1] |
| + | path = File.join(self.reader.mounting_point.path, definition.first) |
| + | path = File.expand_path(path) |
| + | |
| + | listener = ::Listen.to(path, only: filter, &reloader) |
| + | |
| + | # non blocking listener |
| + | listener.start #(false) |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |