-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_batch.php
More file actions
58 lines (50 loc) · 2 KB
/
Copy pathasync_batch.php
File metadata and controls
58 lines (50 loc) · 2 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
<?php
/**
* 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.
*
* php examples/async_batch.php ./contracts
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use VisionApi\Client;
use VisionApi\Exception\ApiException;
use VisionApi\Exception\TaskFailedException;
$folder = $argv[1] ?? './contracts';
$vision = new Client();
$batch = date('Y-m-d'); // one idempotency namespace per day
// Submit everything first, then collect. PHP is single-threaded here, so the parallelism
// is the API's: all the files are already being worked on while we poll the first one.
$pending = [];
foreach (glob($folder . '/*.pdf') ?: [] as $path) {
$name = basename($path);
$ref = $vision->analyzeAsync([
'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}",
]);
$pending[$name] = $ref['task_id'];
echo "queued {$name} → {$ref['task_id']}\n";
}
$spent = 0;
foreach ($pending as $name => $taskId) {
try {
$task = $vision->waitForTask($taskId, ['poll_interval' => 2.0, 'max_wait' => 1200.0]);
$spent += $task['credits_used'];
echo "✓ {$name}: {$task['credits_used']} credits\n";
} catch (TaskFailedException $e) {
// A failed task costs 0 credits — the reservation is released in full.
echo "✗ {$name}: {$e->errorCode}\n";
} catch (ApiException $e) {
echo "✗ {$name}: {$e->errorCode}\n";
}
}
echo "\n", count($pending), " files, {$spent} credits\n";