-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_batch.rb
More file actions
69 lines (60 loc) · 2.1 KB
/
Copy pathasync_batch.rb
File metadata and controls
69 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
# A folder of long PDFs, submitted to the queue and collected as they finish.
#
# Two things make this different from a loop of analyze calls:
#
# 1. Nothing is held open for 60 seconds. A 50-page contract can take minutes, and a
# synchronous request would be killed at 60 s with a 504 sync_timeout.
# 2. A stable idempotency_key per file means re-running the script after a crash replays
# the stored responses instead of paying for the batch twice.
#
# ruby examples/async_batch.rb ./contracts 4
require "date"
require "vision_api"
folder = ARGV[0] || "./contracts"
workers = (ARGV[1] || 4).to_i
vision = VisionAPI.new
batch = Date.today.iso8601 # one idempotency namespace per day
queue = Queue.new
Dir.glob(File.join(folder, "*.pdf")).sort.each { |path| queue << path }
results = Queue.new
# The rate limit is 60 requests/minute per key, and polling counts. Four concurrent files
# at one poll every 2 s is comfortably inside it.
threads = Array.new([workers, queue.size].min) do
Thread.new do
while (path = queue.pop(true) rescue nil)
name = File.basename(path)
begin
ref = vision.analyze_async(
file: path,
preset: "contract",
pages: "1-50",
# Derived from the file, not random: a re-run of the same batch must not re-charge.
idempotency_key: "#{batch}:#{name}"
)
task = vision.wait_for_task(ref["task_id"], poll_interval: 2, max_wait: 20 * 60)
results << { name: name, credits: task["credits_used"] }
rescue VisionAPI::TaskFailedError => e
# A failed task costs 0 credits — the reservation is released in full.
results << { name: name, error: e.code }
rescue VisionAPI::APIError => e
results << { name: name, error: e.code }
end
end
end
end
threads.each(&:join)
results.close
spent = 0
count = 0
until results.empty?
r = results.pop
count += 1
if r[:error]
puts "✗ #{r[:name]}: #{r[:error]}"
else
spent += r[:credits].to_i
puts "✓ #{r[:name]}: #{r[:credits]} credits"
end
end
puts "\n#{count} files, #{spent} credits"