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 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>MagentoCronModelConfigSourceFrequency</source_model>
<backend_model>CustomAutoOrderCancelModelConfigCronConfig</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="VendorModuleCronCancelOrder" 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 VendorModuleModelConfig;
class CronConfig extends MagentoFrameworkAppConfigValue
{
/**
* 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 MagentoFrameworkAppConfigValueFactory
*/
protected $_configValueFactory;
/**
* @var string
*/
protected $_runModelPath = '';
/**
* @param MagentoFrameworkModelContext $context
* @param MagentoFrameworkRegistry $registry
* @param MagentoFrameworkAppConfigScopeConfigInterface $config
* @param MagentoFrameworkAppCacheTypeListInterface $cacheTypeList
* @param MagentoFrameworkAppConfigValueFactory $configValueFactory
* @param MagentoFrameworkModelResourceModelAbstractResource $resource
* @param MagentoFrameworkDataCollectionAbstractDb $resourceCollection
* @param string $runModelPath
* @param array $data
*/
public function __construct(
MagentoFrameworkModelContext $context,
MagentoFrameworkRegistry $registry,
MagentoFrameworkAppConfigScopeConfigInterface $config,
MagentoFrameworkAppCacheTypeListInterface $cacheTypeList,
MagentoFrameworkAppConfigValueFactory $configValueFactory,
MagentoFrameworkModelResourceModelAbstractResource $resource = null,
MagentoFrameworkDataCollectionAbstractDb $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 == MagentoCronModelConfigSourceFrequency::CRON_MONTHLY ? '1' : '*', //Day of the Month
'*', //Month of the Year
$frequency == MagentoCronModelConfigSourceFrequency::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();
}
}