cancel
Showing results for 
Search instead for 
Did you mean: 

Create 13.000 products in code causes timeout error - please HELP (urgent!)

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Create 13.000 products in code causes timeout error - please HELP (urgent!)

Hi,

I fetch 13.000 products from a external API and want to save them in my shop. The import is triggered via cron.

After fetching, I do the following code, which causes a timeout:

 

private function apiArticlesToShop($apiArticles)
{
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$imageSaveDir = Mage::getBaseDir("media") . DS . "import" . DS;

    try
    {
        foreach ($articles as $articleStruct)
        {
            $product = Mage::getModel("catalog/product");
            $productId = $product->getIdBySku($articleStruct["internal_id"]);
            if(!$productId)
            {
                $product->setStoreId(1);
                $product->setWebsiteIds([1]);
                $product->setCreatedAt(strtotime("now"));
                $product->setUpdatedAt(strtotime("now"));
                $product->setTypeId("simple");
                $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
                $product->setSku($articleStruct["internal_id"]);
                $product->setCategoryIds([26]);
                $product->setEan($articleStruct["ean"]);
                $product->setWeight($articleStruct["weight"] * 1000);

                // images
                $product->setMediaGallery(["images"=>[], "values"=>[]]);
                foreach ($articleStruct["images"] as $i => $imageUrl)
                {
                    $saveLocation = $imageSaveDir . basename($imageUrl);
                    // save image in file system
                    if (!file_exists($saveLocation))
                    {
                        $curl = $this->getCurlClient($imageUrl);
                        $data = curl_exec($curl);
                        curl_close($curl);

                        file_put_contents($saveLocation, $data);
                    }

                    if ($i == 0)
                        $product->addImageToMediaGallery($saveLocation, ["image", "thumbnail", "small_image"], false, false);
                    else
                        $product->addImageToMediaGallery($saveLocation, [], false, false);
                }
            }
            else
            {
                $product = $product->load($productId);
            }

            $product->setName($articleStruct["title"]);
            $product->setDescription("");
            $product->setShortDescription("");
            $product->setTaxClassId(1);
            $product->setPrice($articleStruct["price_without_rebate"]);

            // stock data
            $product->setStockData([
                        "use_config_manage_stock" => 0,
                        "manage_stock" => 1,
                        "min_sale_qty" => $articleStruct["minimum_purchase"],
                        "max_sale_qty" => $articleStruct["stock"],
                        "is_in_stock" => $articleStruct["stock"] > 0 ? 1 : 0,
                        "qty" => $articleStruct["stock"]
             ]);
             $product->setStatus(1);
             $product->save();
        }
    }
    catch (Exception $e)
    {
        Mage::log($e->getMessage());
    }

}

 this code works fine, products are created.

but after a while I get a timeout:

[27-Sep-2018 13:09:26 UTC] PHP Fatal error:  Maximum execution time of 240 seconds exceeded in /../magento/app/Mage.php on line 0

I dont know where ths 240 seconds come from. php_ini is configured with many more time.

sometime, the same error occurs with other file and line number... but always 240 seconds.

 

on top of my import file I have this line

ini_set('MAX_EXECUTION_TIME', -1);

because I though it would  help... but it does not. 

 

Problem is not the fetching but the saving...

 

PLEASE HELP ME!

2 REPLIES 2

Re: Create 13.000 products in code causes timeout error - please HELP (urgent!)

Hello FloC3,

 

It could be a few things here.
I will be guessing as I have no idea what setup of hosting you have: Dedicated/VPS/Shared

 

ini_set('MAX_EXECUTION_TIME', -1);
Might not work if your PHP setup is running in safe mode.

 

If you using shared hosting or current Magento setup as a sub account.
local php.ini will be restricted by global php.ini or hosting provider.

 

Try checking with phpinfo() for more details (example below)

 

HFXOTbc.png

Re: Create 13.000 products in code causes timeout error - please HELP (urgent!)

Hi,

 

the php.ini of the hosting has set a max_execution_time of 18000 and my htaccess does the same.