Skip to content

Commit

Permalink
Listen earlier in EventedFileUpdateChecker
Browse files Browse the repository at this point in the history
Some files like routes.rb may be very large and vary between the initialization of the app and the first request. In these scenarios if we are using a forked process we cannot rely on the files to be unchanged between when the code is booted and the listener is started. 

For that reason we start a listener on the main process immediately, when we detect that a process does not have a listener started we force the updated state to be true, so we are guaranteed to catch any changes made between the code initialization and the fork.
  • Loading branch information
schneems committed Jun 6, 2016
1 parent bd38e92 commit a8f29f6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions activesupport/lib/active_support/evented_file_update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module ActiveSupport
# EventedFileUpdateChecker#execute is run or when EventedFileUpdateChecker#execute_if_updated
# is run and there have been changes to the file system.
#
# Note: To start listening to change events you must first call
# EventedFileUpdateChecker#updated? inside of each process.
# Note: Forking will cause the first call to `updated?` to return `true`.
#
# Example:
#
Expand All @@ -41,10 +40,11 @@ def initialize(files, dirs = {}, &block)
@dirs[@ph.xpath(dir)] = Array(exts).map { |ext| @ph.normalize_extension(ext) }
end

@block = block
@updated = Concurrent::AtomicBoolean.new(false)
@lcsp = @ph.longest_common_subpath(@dirs.keys)
@pid_hash = Concurrent::Hash.new
@block = block
@updated = Concurrent::AtomicBoolean.new(false)
@lcsp = @ph.longest_common_subpath(@dirs.keys)
@pid_hash = Concurrent::Hash.new
@parent_pid = Process.pid

if (@dtw = directories_to_watch).any?
# Loading listen triggers warnings. These are originated by a legit
Expand All @@ -58,6 +58,7 @@ def initialize(files, dirs = {}, &block)
end
end
end
boot!
end

def updated?
Expand All @@ -82,6 +83,7 @@ def execute_if_updated
def boot!
Listen.to(*@dtw, &method(:changed)).start
@pid_hash[Process.pid] = true
@updated.make_true if @parent_pid != Process.pid
end

def changed(modified, added, removed)
Expand Down

0 comments on commit a8f29f6

Please sign in to comment.