Make Sitemap::Store more thread-safe.

This commit is contained in:
Ben Hollis 2012-10-20 21:19:13 -07:00
parent 0d806277f9
commit c40102cf53

View file

@ -1,5 +1,6 @@
# Used for merging results of metadata callbacks
require "active_support/core_ext/hash/deep_merge"
require 'monitor'
module Middleman
@ -25,6 +26,8 @@ module Middleman
@_cached_metadata = {}
@resource_list_manipulators = []
@needs_sitemap_rebuild = true
@lock = Monitor.new
reset_lookup_cache!
# Register classes which can manipulate the main site map list
@ -48,31 +51,38 @@ module Middleman
# Rebuild the list of resources from scratch, using registed manipulators
# @return [void]
def rebuild_resource_list!(reason=nil)
@lock.synchronize do
@needs_sitemap_rebuild = true
end
end
# Find a resource given its original path
# @param [String] request_path The original path of a resource.
# @return [Middleman::Sitemap::Resource]
def find_resource_by_path(request_path)
@lock.synchronize do
request_path = ::Middleman::Util.normalize_path(request_path)
ensure_resource_list_updated!
@_lookup_by_path[request_path]
end
end
# Find a resource given its destination path
# @param [String] request_path The destination (output) path of a resource.
# @return [Middleman::Sitemap::Resource]
def find_resource_by_destination_path(request_path)
@lock.synchronize do
request_path = ::Middleman::Util.normalize_path(request_path)
ensure_resource_list_updated!
@_lookup_by_destination_path[request_path]
end
end
# Get the array of all resources
# @param [Boolean] include_ignored Whether to include ignored resources
# @return [Array<Middleman::Sitemap::Resource>]
def resources(include_ignored=false)
@lock.synchronize do
ensure_resource_list_updated!
if include_ignored
@resources
@ -80,6 +90,7 @@ module Middleman
@resources.reject(&:ignored?)
end
end
end
# Register a handler to provide metadata on a file path
# @param [Regexp] matcher
@ -201,6 +212,7 @@ module Middleman
# rebuild_resource_list! since the last time it was run. This is
# very expensive!
def ensure_resource_list_updated!
@lock.synchronize do
return unless @needs_sitemap_rebuild
@needs_sitemap_rebuild = false
@ -219,12 +231,15 @@ module Middleman
newres
end
end
end
private
def reset_lookup_cache!
@lock.synchronize {
@_lookup_by_path = {}
@_lookup_by_destination_path = {}
}
end
end
end