1
<?php

/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/**
 * Import model
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */

namespace AAR\Import\Model\Import\ScheduleImportExport;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\Framework\Filesystem;
use Magento\ScheduledImportExport\Model\Scheduled\Operation\Data;

class Import extends \Magento\ScheduledImportExport\Model\Import implements
    \Magento\ScheduledImportExport\Model\Scheduled\Operation\OperationInterface {

    /**
     * Run single import through cron
     *
     * @param \Magento\ScheduledImportExport\Model\Scheduled\Operation $operation
     * @param $fileInfo
     * @return bool
     */
    public function runSingleSchedule(\Magento\ScheduledImportExport\Model\Scheduled\Operation $operation, $fileInfo)
    {
        $sourceFile = $fileInfo['file_path'];
        $result = false;
        if ($sourceFile) {
            $this->createHistoryReport($sourceFile, $operation->getEntityType());
            $result = $this->validateSource(
                \Magento\ImportExport\Model\Import\Adapter::findAdapterFor(
                    $sourceFile,
                    $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
                    $this->getData(\Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR)
                )
            );
        }
        $isAllowedForcedImport = $operation->getForceImport() &&
            $this->getProcessedRowsCount() != $this->getInvalidRowsCount();
        if ($isAllowedForcedImport || $result) {
            $this->setData(
                self::FIELD_NAME_VALIDATION_STRATEGY,
                $operation->getForceImport()
                ? ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS
                : ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR
            );
            $result = $this->importSource();
        }

        if ($result) {
            $this->invalidateIndex();
        }
        return (bool)$result;
    }



    /**
    * Run multiple import through cron
    *
    * @param \Magento\ScheduledImportExport\Model\Scheduled\Operation $operation
    * @return bool
    */
    public function runSchedule(\Magento\ScheduledImportExport\Model\Scheduled\Operation $operation) {
        $result = false;
        $filesArray = [];
        $file = $operation->getFileInfo();
        $isMultiple = (strpos($file['file_name'], '*') !== false);
        $rootDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);

        if(strpos($file['file_name'], '*') !== false){
            $filePath = rtrim($file['file_path'], '\\/') . '/' . strstr($file['file_name'], '.', true);
            // Get all files matching a pattern
            $paths = glob($rootDirectory->getAbsolutePath($filePath));
            foreach ($paths AS $path) {
                $updatedFile = $file;
                $updatedFile['file_path'] = $path;
                $updatedFile['file_name'] = basename($path);
                $mergedFile = array_merge(
                        $updatedFile,
                        [
                            'entity' => $operation->getEntityType(),
                            'behavior' => $operation->getBehavior(),
                            'operation_type' => $operation->getOperationType(),
                            'run_at' => $operation->getStartTime(),
                            'scheduled_operation_id' => $operation->getId(),
                        ]
                    );
                $filesArray[] = $mergedFile;
            }
        } else {
            $filesArray[] = $file;
        }
        //echo '<pre>',print_r(var_dump($filesArray)),'</pre>';
        //      exit;
        //print_r($filesArray); exit;
        if (count($filesArray > 0)){
            foreach($filesArray AS $fileInfo){
                $result = $this->runSingleSchedule($operation, $fileInfo);

                if ($result) {
                    // Move file to archive
                    $archiveDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\\/') . '/archive');
                    if (!file_exists($archiveDir) && !is_dir($archiveDir)) {
                        mkdir($archiveDir);
                    }
                    rename($fileInfo['file_path'], $archiveDir . '/' . basename($fileInfo['file_name']));
                } else {
                    // Move file to fail
                    $failDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\\/') . '/fail');
                    if (!file_exists($failDir) && !is_dir($failDir)) {
                      mkdir($failDir);
                    }
                    copy($fileInfo['file_path'], $failDir . '/' . basename($fileInfo['file_name']));
                    break;
                }   
            }
        }

        return (bool)$result;

    }
}

Above is the code for the problem I am encountering in Magento 2. I am trying to import multiple files via csv import. I have created a module to adjust Magento’s native import scheduler. As I understand it, Magento does not natively allow for multiple uploads in a batch sequence, this module adjusts that. This process will be used to keep our data in sync with one server to another.

The first server sends csv files in upwards of 5k rows to Magento server. These are placed in an import directory. When the scheduler is fired via cron – it absorbs any csv files in the directory. If it is successful it moves the file to an archive folder. If it fails it COPIES it to a fail directory.

This works for importing products at 5k slices --it works wonderfully. I have then tried to import/update the products table with quantities via an inventory csv(5k slices) and am getting different results. The first file will be picked up and imported into the folder. The second file will fail. IF we run the scheduler again, it will pick up the previously failed file, run it successfully – but fail on the next file.

Not aren’t quite sure why products would go in successfully, but the inventory csv batch would fail.

Debugging I have found that $result = $this->importSource(); returns true the first file, but does not return a result the second time.

Has anyone tried importing multiple files via Magento 2 scheduler? How can I ensure that the function importSource returns a value at some point?

0

I've just come across the same issue, where the first file would import but the second would fail.

Adding <type name="Magento\ImportExport\Model\ResourceModel\Import\Data" shared="false" /> to our module's di.xml file fixed the issue for us.

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.