0

I have written a custom import script that reads data from CSV and creates/updates products in my store. The script is scheduled to be executed once per day (in the night) with the cron. This is the cron configuration in the custom module that executes the import

<crontab>
        <jobs>
            <custom_import>
                <schedule>
                    <cron_expr>0 1 * * *</cron_expr>
                </schedule>
                <run>
                    <model>custom_import/observer::import</model>
                </run>
            </custom_import>
       </jobs>
</crontab>

The process takes a long time to execute (there are about 4000 products to process) and on the production server the max_execution_time is set to 30 (5 minutes) and I can not change it (set_time_limit does not work).

So I need to do the import in another way. I thought that the best way would be to have a 'trigger' script to be executed with cron. Then trigger script would start the import and before the time limit is reached re-run the import process.

I was wondering how this could be achieved in Magento or if there is a better way to do that.

1

A long term cron web execution should be disencouraged and splitting the import may result in a very mess code ;)

My suggestion is to workaround it with two possible options:

Option 1 - cron by shell:

You can both call your cron via shell (best if using AOE_Scheduler module).

Option 2 - script by shell:

You can create a Magento shell script to add to your cron:

<?php
require_once('abstract.php');

class My_Import extends Mage_Shell_Abstract {
    public function run() {
        Mage::getSingleton('custom_import/observer')->import();
    }
}

$script = new My_Import();
$script->run();

When running from shell you do not have the same time limits as the web has.

This script must be created inside /shell folder.

You can run it with:

php -q scriptname.php

And of cource you can add to your crontab for a scheduled importer.

  • I managed to do it with the option 2. I did not know that Magento can override that settings with shell scripts. Thanks a lot! – gianis6 Feb 25 '16 at 14:20
  • 1
    It is not Magento itself, it is because it uses php-cli instead of standard PHP. PHP from cli is usually configured with no time restrictions. – Phoenix128_RiccardoT Feb 25 '16 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.