I'm tring to update all products of an eshop with a php script ( magento command ). I'm preparing an array with all stock data and while looping over all shop products i'm updating the stock using the code bellow in a function
enter code here
if ($isInStock === null)
$isInStock = ($quantity > 0 ? SourceItem::STATUS_IN_STOCK : SourceItem::STATUS_OUT_OF_STOCK);
$inventorySourceItem = $this->objectManager->create('\Magento\Inventory\Model\SourceItem');
$existingInventorySourceItem = $inventorySourceItem->getCollection()
->addFieldToFilter('sku', $sku)
->addFieldToFilter('source_code', $inventorySourceCode)
->getFirstItem();
$existingInventorySourceItem->setQuantity($quantity);
$existingInventorySourceItem->setStockStatus($isInStock);
$existingInventorySourceItem->setStatus($isInStock);
$existingInventorySourceItem->setSku($sku);
$existingInventorySourceItem->setSourceCode($inventorySourceCode);
$existingInventorySourceItem->save();
try {
$this->objectManager->get('Magento\InventoryApi\Api\SourceItemsSaveInterface')->execute([$existingInventorySourceItem]);
} catch (Exception $e) {
echo $e->getMessage();
}
The problem is tha as the loop continues, the execution of the SourceItemsSaveInterface goes slower, and the script consumes more cpu, untill it reaches 90% of cpu.
As far as i've seen in my code, everything is looking good, and the same code executes well at my development machine so i think there is something related with the server although i cant find any clue. Keep in mind that mysql doenst seem to have a hard time during the process.
Already tried to add limit to the existingInventorySourceItem query and a sleep function beetween loops