0

im working on cron with config path to schedule according to expression set from admin, value getting saved in db after save configuration but cron not working nor showing in cron_schedule table.

here is my config in admin enter image description here

here is my system.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="autoordercancel_config" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Order Cancel</label>
            <tab>vendor_module</tab>
            <resource>Vendor_Module::conf</resource>
            <group id="cancelorder_cron" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Cron Settings</label>
                <field id="frequency" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Frequency</label>
                    <source_model>Magento\Cron\Model\Config\Source\Frequency</source_model>
                    <backend_model>Custom\AutoOrderCancel\Model\Config\CronConfig</backend_model>
                </field>
                <field id="time" translate="label comment" sortOrder="2" type="time" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Start Time</label>
                </field>
            </group>
        </section>
    </system>
</config>

here is my crontab.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="index">
        <job instance="Vendor\Module\Cron\CancelOrder" method="execute" name="custom_autoordercancel_cron">
            <config_path>crontab/default/jobs/custom_autoordercancel_cron/schedule/cron_expr</config_path>
        </job>
    </group>
</config>

here is my CronConfig.php

<?php

 
namespace Vendor\Module\Model\Config;
 
class CronConfig extends \Magento\Framework\App\Config\Value
{
    /**
     * Cron string path
     */
    const CRON_STRING_PATH = 'crontab/default/jobs/custom_autoordercancel_cron/schedule/cron_expr';
 
    /**
     * Cron model path
     */
    const CRON_MODEL_PATH = 'crontab/default/jobs/custom_autoordercancel_cron/run/model';
 
    /**
     * @var \Magento\Framework\App\Config\ValueFactory
     */
    protected $_configValueFactory;
 
    /**
     * @var string
     */
    protected $_runModelPath = '';
 
    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param string $runModelPath
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Config\ValueFactory $configValueFactory,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        $runModelPath = '',
        array $data = []
    ) {
        $this->_runModelPath = $runModelPath;
        $this->_configValueFactory = $configValueFactory;
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }
 
    /**
     * {@inheritdoc}
     *
     * @return $this
     * @throws \Exception
     */
    public function afterSave()
    {
        $time = $this->getData('groups/cancelorder_cron/fields/time/value');
        $frequency = $this->getData('groups/cancelorder_cron/fields/frequency/value');
 
        $cronExprArray = [
            intval($time[1]), //Minute
            intval($time[0]), //Hour
            $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', //Day of the Month
            '*', //Month of the Year
            $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*', //Day of the Week
        ];
 
        $cronExprString = join(' ', $cronExprArray);
 
        try {
            $this->_configValueFactory->create()->load(
                self::CRON_STRING_PATH,
                'path'
            )->setValue(
                $cronExprString
            )->setPath(
                self::CRON_STRING_PATH
            )->save();
            $this->_configValueFactory->create()->load(
                self::CRON_MODEL_PATH,
                'path'
            )->setValue(
                $this->_runModelPath
            )->setPath(
                self::CRON_MODEL_PATH
            )->save();
        } catch (\Exception $e) {
            throw new \Exception(__('We can\'t save the cron expression.'));
        }
 
        return parent::afterSave();
    }
}
3
  • You have not shared code of remaining files of your module. Could you please share that as well? Dec 8, 2022 at 5:02
  • also module and routes.xml remaing and cant paste helper code here Dec 8, 2022 at 7:27
  • No Issues, just add skeleton of Vendor\Module\Cron\CancelOrder class. Dec 8, 2022 at 8:12

1 Answer 1

0

Maybe broken because Cache. Try to run with manually.

bin/magento cache:flush
bin/magento cron:remove
bin/magento cron:install
bin/magento cron:run --group index

Let's check your job name (custom_autoordercancel_cron) in the job_code.cron_schedule db and check the Status.

3
  • already tried this but no success. Dec 8, 2022 at 2:39
  • Are you check Cron.log file ? Dec 8, 2022 at 2:47
  • yes no traces there Dec 8, 2022 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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