Skip to content

Commit

Permalink
fix: retry components that error in jekyll due to a thread race
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Oct 5, 2021
1 parent 02b3436 commit 1cc3172
Showing 1 changed file with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,47 @@ def self.build_from_location(base_path, site)
Jekyll.logger.info "Bookshop:",
"Parsing Stories from #{base_path}"

threads = []
Dir.glob("**/*.{bookshop}.{toml,tml,tom,tm}", :base => base_path).each do |f|
threads << Thread.new do
raw_file = File.read(base_path + "/" + f)
component = load_component(raw_file)

transform_component(f, component).each do |transformed_component|
add_structure(site.config["_array_structures"], transformed_component)
files = Dir.glob("**/*.{bookshop}.{toml,tml,tom,tm}", :base => base_path);

read_attempts = 0
while files.size > 0 && read_attempts < 5
processing = files.map(&:clone)
files = []
threads = []
processing.each do |f|
threads << Thread.new do
begin
raw_file = File.read(base_path + "/" + f)
component = load_component(raw_file)

transform_component(f, component).each do |transformed_component|
add_structure(site.config["_array_structures"], transformed_component)
end
rescue
Thread.current[:output] = f
end
end
end
threads.each do |t|
t.join
files << t[:output] if t[:output]
end
Jekyll.logger.info "Bookshop:",
"Had trouble reading #{files.size} file(s), retrying" if files.size > 0
read_attempts += 1
end

if files.size > 0
Jekyll.logger.error "Bookshop:",
"❌ Failed to read #{files.size} file(s)"
files.each do |f|
Jekyll.logger.error "Bookshop:",
"❌ Failed to read #{f}"
end
Jekyll.logger.error "Bookshop:",
"❌ Check your TOML files are valid."
exit 1
end
threads.each(&:join)
end

def self.setup_helpers
Expand Down

0 comments on commit 1cc3172

Please sign in to comment.