bestit/commercetools-async-pool
Laravel-friendly async pool for commercetools PHP SDK requests. Schedule, batch, and execute API calls concurrently with configurable limits, retries, and callbacks, helping speed up imports, sync jobs, and background processing while keeping resource usage under control.
Installation Add the package via Composer:
composer require bestit/commercetools-async-pool
Basic Configuration Require the package in your project:
use Bestit\CommercetoolsAsyncPool\AsyncPool;
use Bestit\CommercetoolsAsyncPool\Client\CommercetoolsClient;
First Use Case: Batch API Requests
Initialize the pool with a CommercetoolsClient (assuming you have a ClientBuilder or similar):
$client = new CommercetoolsClient($clientBuilder);
$pool = new AsyncPool($client, 5); // 5 concurrent requests
Queue requests (e.g., fetching products in parallel):
$requests = [
$client->getRequest('GET', '/products/1'),
$client->getRequest('GET', '/products/2'),
// ...
];
$results = $pool->execute($requests);
Request Batching Group related API calls (e.g., fetching product variants, inventory updates) into a single batch:
$requests = collect($productIds)->map(fn($id) =>
$client->getRequest('GET', "/products/{$id}/variants")
);
$pool->execute($requests->toArray());
Error Handling
Use execute() with a callback for error handling:
$results = $pool->execute($requests, function($request, $response) {
if ($response->isError()) {
logger()->error("Failed: {$request->getUrl()}", ['error' => $response->getBody()]);
}
});
Integration with Laravel Queues Dispatch long-running batches to a queue job:
class ProcessProductBatch implements ShouldQueue
{
public function handle()
{
$pool = new AsyncPool($this->client, 10);
$results = $pool->execute($this->requests);
// Process results...
}
}
Dynamic Pool Sizing Adjust concurrency based on system load (e.g., via config):
$pool = new AsyncPool($client, config('async_pool.max_concurrency'));
Retry Logic
Combine with a retry package (e.g., spatie/laravel-queue-retries) for transient failures.
Rate Limiting
Commercetools enforces rate limits. Monitor response headers (X-RateLimit-Remaining) and throttle requests if needed.
Memory Leaks Large batches may exhaust memory. Use chunking:
$chunkedRequests = array_chunk($requests, 100);
foreach ($chunkedRequests as $chunk) {
$pool->execute($chunk);
}
Idempotency
Ensure requests are idempotent (e.g., PATCH instead of POST for updates) to avoid duplicate side effects.
Logging
Enable debug logging for the CommercetoolsClient to inspect raw requests/responses:
$client = new CommercetoolsClient($clientBuilder, [
'debug' => true,
]);
Timeouts Set timeouts on the underlying HTTP client (e.g., Guzzle) to avoid hanging:
$clientBuilder->setHttpClient(new \GuzzleHttp\Client([
'timeout' => 30,
]));
Custom Response Handling
Extend AsyncPool to transform responses:
$pool = new AsyncPool($client, 5, new class {
public function transform($response) {
return json_decode($response->getBody(), true);
}
});
Progress Tracking
Use Laravel’s ProgressBar for CLI feedback:
use Symfony\Component\Console\Helper\ProgressBar;
$progress = new ProgressBar($output, count($requests));
$results = $pool->execute($requests, function() use ($progress) {
$progress->advance();
});
Middleware
Inject middleware (e.g., auth tokens) via the CommercetoolsClient constructor.
How can I help you explore Laravel packages today?