0

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

3
  • Additional information request. RAM size, # cores, any SSD or NVME devices on MySQL Host server? Post on pastebin.com and share the links. From your SSH login root, Text results of: B) SHOW GLOBAL STATUS; after minimum 24 hours UPTIME C) SHOW GLOBAL VARIABLES; D) SHOW FULL PROCESSLIST; E) complete MySQLTuner report AND Optional helpful information, if available includes - htop OR top for most active apps, ulimit -a for a Linux/Unix list of limits, iostat -xm 5 3 for IOPS by device and core/cpu count, for server workload tuning analysis to provide suggestions. Commented Oct 17, 2020 at 15:14
  • Data needs to be from your production instance, please. Commented Oct 17, 2020 at 15:15
  • When you are ready to speed up the process completion, please post the data requested on Oct 17, 2020. Commented Nov 29, 2020 at 20:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.