Skip to content

Commit 8f2312e

Browse files
committed
added backoff
1 parent c47c2ea commit 8f2312e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Model/Product/Enricher.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Magento\Catalog\Model\Product;
88
use OpenAI\Factory;
99
use OpenAI\Client;
10+
use OpenAI\Responses\Meta;
11+
use OpenAI\Exceptions\ErrorException;
1012

1113
class Enricher
1214
{
@@ -73,13 +75,43 @@ public function enrichAttribute($product, $attributeCode)
7375
if($result = $response->choices[0]) {
7476
$product->setData($attributeCode, $result->message?->content);
7577
}
78+
$this->backoff($response->meta());
7679
}
7780
}
7881

82+
public function backoff(Meta $meta)
83+
{
84+
if($meta->requestLimit->remaining < 1) {
85+
sleep($this->strToSeconds($meta->requestLimit->reset));
86+
}
87+
// 1 token ~= 0.75 word
88+
// do not use config value
89+
if($meta->tokenLimit->remaining < 1000) {
90+
sleep($this->strToSeconds($meta->tokenLimit->reset));
91+
}
92+
}
93+
94+
private function strToSeconds(string $time)
95+
{
96+
preg_match('/(?:([0-9]+)h)?(?:([0-9]+)m)?(?:([0-9]+)s)?/', $time, $matches);
97+
98+
$hours = isset($matches[1]) ? intval($matches[1]) : 0;
99+
$minutes = isset($matches[2]) ? intval($matches[2]) : 0;
100+
$seconds = isset($matches[3]) ? intval($matches[3]) : 0;
101+
102+
return $hours * 3600 + $minutes * 60 + $seconds;
103+
}
104+
79105
public function execute(Product $product)
80106
{
81107
foreach ($this->getAttributes() as $attributeCode) {
82-
$this->enrichAttribute($product, $attributeCode);
108+
try {
109+
$this->enrichAttribute($product, $attributeCode);
110+
} catch (ErrorException $e) {
111+
// try it one more time just in case we failed to catch the limit in backoff
112+
sleep(60);
113+
$this->enrichAttribute($product, $attributeCode);
114+
}
83115
}
84116
//@TODO: throw exception?
85117
}

0 commit comments

Comments
 (0)