Skip to content

Commit ef8483a

Browse files
committed
Use true_sleep so sleeping works properly in Horizon jobs
1 parent 318092b commit ef8483a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"psr-4": {
4040
"Luminarix\\Shopify\\GraphQLClient\\": "src/",
4141
"Luminarix\\Shopify\\GraphQLClient\\Database\\Factories\\": "database/factories/"
42-
}
42+
},
43+
"files": [
44+
"src/functions.php"
45+
]
4346
},
4447
"autoload-dev": {
4548
"psr-4": {

src/Services/RedisRateLimitService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function waitIfNecessary(float $requestedQueryCost): void
9494

9595
if ($waitTime > 0) {
9696
// Sleep for the calculated wait time
97-
Sleep::sleep($waitTime);
97+
true_sleep($waitTime);
9898
}
9999
}
100100
}

src/functions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (!function_exists('true_sleep')) {
6+
function true_sleep(float|int $seconds): void
7+
{
8+
$end = microtime(true) + $seconds;
9+
while (($remaining = $end - microtime(true)) > 0) {
10+
usleep((int)($remaining * 1_000_000));
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)