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