0

I have created the below code for the export of the CSV in my cron class file. But I unable to get it export file and when I checked in database it was showed me to status running for my job_code colum in cron_schedule table.

crontab.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
      <job name="sdfdf" instance="KiwiCommerce\AdminActivity\Cron\Test" method="execute">
          <schedule>* * * * *</schedule>
      </job>
    </group>
</config>

Test.php

<?php
namespace KiwiCommerce\AdminActivity\Cron;

class Test {

    protected $fileFactory;
    protected $csvProcessor;
    protected $directoryList;
    protected $_logger;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\File\Csv $csvProcessor,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList
    ) {
        $this->fileFactory = $fileFactory;
        $this->csvProcessor = $csvProcessor;
        $this->directoryList = $directoryList;
        parent::__construct($context, $customerSession);
    }

    /**
     * Method executed when cron runs in server
     */
    public function execute() {

        $fileName = 'csv_filename.csv';
        $filePath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR)
          . "/" . $fileName;

        $customer = $this->_customerSession->getCustomer();
        $personalData = $this->getPresonalData($customer);

        $this->csvProcessor
            ->setDelimiter(';')
            ->setEnclosure('"')
            ->saveData(
                $filePath,
                $personalData
            );

        return $this->fileFactory->create(
            $fileName,
            [
                'type' => "filename",
                'value' => $fileName,
                'rm' => true,
            ],
              \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
            'application/octet-stream'
        );
    }

    protected function getPresonalData( \Magento\Customer\Model\Customer $customer )
    {
        $result = [];
        $customerData = $customer->getData();
        $result[] = [
            'address_id',
            'firstname',
            'middlename',
            'lastname',
           'email',
            'company',
            'street',
            'telephone',
            'fax',
        ];

        $result[] = [
            null,
            $customerData['firstname'],
            $customerData['middlename'],
            $customerData['lastname'],
            $customerData['email'],
            null,
            null,
            null,
            null,
        ];


        $addressId = 1;
        foreach ($customer->getAddresses() as $address) {
            $result[] = [
                $addressId,
                $address['firstname'],
                $address['middlename'],
                $address['lastname'],
                null,
                $address['company'],
                $address['street'],
                $address['telephone'],
                $address['fax'],
            ];
            $addressId++;
        }

        return $result;
    }
}
  • how long it has been in the status of running? – aravind Sep 4 at 10:11
  • @aravind It continues to show me running status. – AKM Sep 4 at 10:16
  • delete the cron directly in cron_schedule db and re run the cron using CLI commands and check the status of cron – aravind Sep 4 at 10:43
  • It may be caused due to fatal error in your php code..Double Check your php code which gets executed on cron run (KiwiCommerce\AdminActivity\Cron\Test ) – aravind Sep 4 at 10:47

Your Answer

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

Browse other questions tagged or ask your own question.