As a commerce agency, we understand that timeouts and memory issues are common challenges for Magento store owners during bulk processing. Your staff may often experience frustrations with tasks like product updates or data synchronization with external systems, which can result in lost productivity. These problems not only impact your team but also disrupt the smooth operation of your eCommerce platform.
Why Do Timeouts Happen?
In Magento, bulk actions like updating products or synchronizing data with external systems are resource-intensive. These tasks, when performed directly through the admin panel, can easily cause timeouts, especially when handling large datasets or slow external APIs. When resources are tied up in this way, the store’s frontend can experience downtime, leading to a poor user experience and reduced revenue.
How Can Queues Help?
Sample Queue-Based Data Synchronization Implementation
1.
Service Class
['identifier' => $item], $dataItems);
$data = ['data_items' => $formattedData];
$this->httpClient->setUri($baseUrl);
$this->httpClient->setMethod(LaminasClient::POST);
$this->httpClient->setHeaders([
'Content-Type' => 'application/json',
'Authorization' => $secretKey,
'locale' => $locale
]);
$this->httpClient->setRawBody(json_encode($data));
$response = $this->httpClient->send();
return $response->isSuccess();
} catch (\Exception $e) {
$this->logger->error('Error during API communication: ' . $e->getMessage());
return false;
}
}
}
2.
Queue Handler Class
syncService->syncData(
$data['items'],
$data['secret_key'],
$data['base_url'],
$data['locale']
);
if (!$success) {
$this->logger->error('Data sync failed.');
}
} catch (\Exception $e) {
$this->logger->error('Error during sync execution: ' . $e->getMessage());
}
}
}
3.
Controller Class
The Controller Class initiates the queue job. It retrieves the data to be synchronized and pushes it into the queue, allowing it to be processed asynchronously without affecting the store’s performance.
collectionFactory->create();
$items = array_column($collection->toArray(), 'sku');
$messageData = [
'items' => $items,
'secret_key' => $this->getSecretKey(),
'base_url' => $this->getBaseUrl(),
'locale' => 'default_locale'
];
$this->publisher->publish('sync_queue', json_encode($messageData));
$this->messageManager->addSuccessMessage(__('Data re-sync initiated.'));
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
$this->messageManager->addErrorMessage(__('Error occurred during sync initiation.'));
}
}
private function getBaseUrl(): string
{
return $this->scopeConfig->getValue('your/config/base_url');
}
private function getSecretKey(): string
{
return $this->scopeConfig->getValue('your/config/secret_key');
}
}
Necessary XML Configuration Files
1.
queue.xml (Queue Configuration)
Location: app/code/NeoSolax/YourModule/etc/queue.xml
2.
consumers.xml (Consumer Configuration)
Location: app/code/NeoSolax/YourModule/etc/consumers.xml
3.
communication.xml (Publisher Configuration)
Location: app/code/NeoSolax/YourModule/etc/communication.xml
4.
di.xml (Dependency Injection Configuration)
Location: app/code/NeoSolax/YourModule/etc/di.xml
- Magento\Framework\MessageQueue\Publisher
NeoSolax\Service\DataSyncService
Psr\Log\LoggerInterface
5.
Optional crontab.xml (Cron Jobs for Queue Processing)
Location: app/code/NeoSolax/YourModule/etc/crontab.xml
* * * * *
queue/consumers_wait_for_messages
sync_queue_consumer
- This cron job is configured to run every minute (* * * * *), ensuring that the queue consumer is continually processing messages.
- The consumerName should match the consumer defined in consumers.xml.
6.
env.php (Optional Configuration for Message Queue Systems like RabbitMQ)
In case you’re using an external message broker like RabbitMQ, you will need to configure env.php for your queue.
Location: app/etc/env.php
[
'consumers_wait_for_messages' => 1, // Ensures consumers wait for new messages before timing out
'amqp' => [
'host' => 'rabbitmq', // Your RabbitMQ host
'port' => '5672', // Default RabbitMQ port
'user' => 'guest', // RabbitMQ user
'password' => 'guest', // RabbitMQ password
'virtualhost' => '/' // RabbitMQ virtual host
]
]
];
- This file defines how your Magento installation interacts with a message broker like RabbitMQ.
- The queue configuration ensures that your consumers are ready to process messages as soon as they arrive.
Conclusion
- Service and handler classes to process tasks asynchronously.
- Queue configuration in queue.xml and consumers.xml to define the queue and its consumers.
- A publisher configuration in communication.xml to manage message sending.
- Dependency injection configurations in di.xml to ensure that the appropriate services are connected.
- Optional crontab.xml and env.php files for managing asynchronous task execution and message brokers like RabbitMQ.
With this system in place, Magento 2 becomes more scalable and efficient, ensuring smooth operation even during intensive bulk processing tasks.
Leave A Comment