0

I am using a cronjob to export products and customers via an export profile. This cronjob generates the files export_customers.csv and export_products.csv for me. This works really great, but sometimes I am getting a weird output file like this:

"firstname";"lastname";"email";"website";"password_hash";"group";"sku";"weight";"name";"qty";"image";"price"
"";"";"";"";"";"";"";"";"";"";"";""
"";"";"";"";"";"";"";"";"";"";"";""
"";"";"";"";"";"";"";"";"";"";"";""

...

In this case it seems that the cronjob mix up the profiles by generating this two files and the results are two csv files, but one of them is broken. The cronjob for customers and products export runs for testing purposes every 5 minutes.

The Cron.php looks as follows:

<?php
class Fox_ImEx_Model_Cron {

    public function exportProducts() {

        $startTime = time();

        $profileId = 1;

        $profile = Mage::getModel('dataflow/profile');
        $userModel = Mage::getModel('admin/user');
        $userModel->setUserId(0);
        Mage::getSingleton('admin/session')->setUser($userModel);

        $profile->load($profileId);
        if (!$profile->getId()) {
            Mage::getSingleton('adminhtml/session')->addError('ERROR: Incorrect profile id');
        }

        Mage::register('current_convert_profile', $profile);
        $profile->run();
        $recordCount = 0;
        $batchModel = Mage::getSingleton('dataflow/batch');
        Mage::unregister('current_convert_profile');

        $duration = time() - $startTime;

        Mage::log('[ImEx] export products (duration: '.$duration.')');

    }

    public function exportCustomers() {

        $startTime = time();

        $profileId = 5;

        $profile = Mage::getModel('dataflow/profile');
        $userModel = Mage::getModel('admin/user');
        $userModel->setUserId(0);
        Mage::getSingleton('admin/session')->setUser($userModel);

        $profile->load($profileId);
        if (!$profile->getId()) {
            Mage::getSingleton('adminhtml/session')->addError('ERROR: Incorrect profile id');
        }

        Mage::register('current_convert_profile', $profile);
        $profile->run();
        $recordCount = 0;
        $batchModel = Mage::getSingleton('dataflow/batch');
        Mage::unregister('current_convert_profile');

        $duration = time() - $startTime;

        Mage::log('[ImEx] export customers (duration: '.$duration.')');

    }

}

Is there a way to avoid this problem?

  • have you enabled logging to check if any errors are being generated that might explain this behaviour? – paj Aug 18 '15 at 15:26
  • yes, but there will be no error file created – Fox Aug 19 '15 at 7:29
  • have you ran it with development mode enabled? Do you run the exports at the same time? If you run them individually, one after the other do you see the same problem? – paj Aug 19 '15 at 7:45
  • yes, development mode is enabled and yes the exports run sometimes at the same time, because the customer export runs every 10 minutes and the product export every 5 minutes and if they run together the broken csv will be generated. How can I solve this issue? Currently I run a server cronjob which execute the magento cron.php every 5 minutes, so I can't avoid that they run together sometimes? I can try to combine this two functions to one function, then the code should be executed sequentially, but then I can't set different execution times? – Fox Aug 19 '15 at 8:37
0

If the problem occurs due to both exports running simultaneously it may be possible to move your export methods to two shell crons e.g. in ./shell/FoxExport1.php, ./shell/FoxExport2.php

<?php
include_once 'abstract.php';
class Fox_Shell_Cron extends Mage_Shell_Abstract {

public function run() {
    $this->_doExport();
}

protected function _doExport() {

     // Export code 1
}
}
$shell = new Fox_Shell_Cron();
$shell->run();

You can then run the exports via two seperate cron tab jobs at different intervals ensuring that enough time is given for the first export to complete i.e.

- - - -  /usr/bin/php -f /pathtomagento/shell/FoxExport1.php
- - - -  /usr/bin/php -f /pathtomagento/shell/FoxExport2.php
| improve this answer | |
  • Ok thanks :). So there is no way to get it work with the little extension from above? It is a difference to use a shell cron vs a normal cron or it is the same if I use two php files which contains the export functions and then run two crons to execute them? – Fox Aug 19 '15 at 9:18
  • This moves the cron task from Magento to your host, so you can run the tasks individually (or from the command line php FoxExport1.php etc.) If it works will depend on if your code will run in this class. If not you could always do the whole thing external to Magento, loading the Magento app in an external php file and running the exports that way. Test it and see how it goes. – paj Aug 19 '15 at 9:26
  • sorry for my lagged feedback, but it is working correctly now – Fox Aug 19 '15 at 14:47

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.