Skip to content

Commit f2f084d

Browse files
committed
Delegate our parallel processing to a gem
1 parent 4ef04b5 commit f2f084d

File tree

2 files changed

+16
-48
lines changed

2 files changed

+16
-48
lines changed

docurium.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
2020
s.add_dependency "rugged", "~>0.21"
2121
s.add_dependency "redcarpet", "~>3.0"
2222
s.add_dependency "ffi-clang", "~> 0.5"
23+
s.add_dependency "parallel", "~> 1.17.0"
2324
s.add_development_dependency "rake", "~> 12"
2425
s.add_development_dependency "minitest", "~> 5.11"
2526

lib/docurium.rb

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require 'rugged'
1111
require 'redcarpet'
1212
require 'redcarpet/compat'
13+
require 'parallel'
1314
require 'thread'
1415

1516
# Markdown expects the old redcarpet compat API, so let's tell it what
@@ -135,68 +136,34 @@ def generate_docs(options)
135136
versions = vers
136137
end
137138

138-
nversions = versions.size
139-
output = Queue.new
140-
pipes = {}
141-
versions.each do |version|
142-
# We don't need to worry about joining since this process is
143-
# going to die immediately
144-
read, write = IO.pipe
145-
pid = Process.fork do
146-
read.close
147-
148-
data = generate_doc_for(version)
149-
examples = format_examples!(data, version)
150-
151-
Marshal.dump([version, data, examples], write)
152-
write.close
153-
end
154-
155-
pipes[pid] = read
156-
write.close
157-
end
158-
159-
print "Generating documentation [0/#{nversions}]\r"
160139
head_data = nil
161-
162-
# This may seem odd, but we need to keep reading from the pipe or
163-
# the buffer will fill and they'll block and never exit. Therefore
164-
# we can't rely on Process.wait to tell us when the work is
165-
# done. Instead read from all the pipes concurrently and send the
166-
# ruby objects through the queue.
167-
Thread.abort_on_exception = true
168-
pipes.each do |pid, read|
169-
Thread.new do
170-
result = read.read
171-
output << Marshal.load(result)
172-
end
173-
end
174-
175-
for i in 1..nversions
176-
version, data, examples = output.pop
177-
140+
nversions = versions.count
141+
Parallel.each_with_index(versions, finish: -> (version, index, result) do
142+
version, data, examples = result
178143
# There's still some work we need to do serially
179144
tally_sigs!(version, data)
180145
force_utf8(data)
181146
sha = @repo.write(data.to_json, :blob)
182147

183-
print "Generating documentation [#{i}/#{nversions}]\r"
148+
puts "Adding documentation for #{version} [#{index}/#{nversions}]"
184149

185150
# Store it so we can show it at the end
186-
if version == 'HEAD'
187-
head_data = data
188-
end
151+
head_data = data if version == 'HEAD'
189152

190153
output_index.add(:path => "#{version}.json", :oid => sha, :mode => 0100644)
191154
examples.each do |path, id|
192155
output_index.add(:path => path, :oid => id, :mode => 0100644)
193156
end
157+
end) do |version, index|
158+
puts "Generating documentation for #{version} [#{index}/#{nversions}]"
159+
data = generate_doc_for(version)
160+
examples = format_examples!(data, version)
161+
[version, data, examples]
162+
end
194163

195-
if head_data
196-
puts ''
197-
show_warnings(data)
198-
end
199-
164+
if head_data
165+
puts ''
166+
show_warnings(head_data)
200167
end
201168

202169
# We tally the signatures in the order they finished, which is

0 commit comments

Comments
 (0)