Hello Magento Friends,
Today I am going to explain Magento 2: How To Setup Cron Time By Configuration
You can schedule cron dynamically with setup cron time by configuration. Sometimes developers set static time expressions to set cron in a custom module. So whenever the admin needs to make changes to the time expression, there is a need to change the crontab.xml file. Using the below steps the admin can quickly make changes to the time expression from the admin configuration and there is no need to make modifications in the crontab.xml file.
Let’s get started,
Steps to Setup Cron Time by Configuration in Magento 2:
Step 1: First you need to add crontab.xml in the following path
app\code\Vendor\Extension\etc\crontab.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> <group id="default"> <job instance="Vendor\Extension\Cron\Cronfile" method="execute" name="my_cron_job"> <config_path>crontab/default/jobs/my_cron_job/schedule/cron_expr</ </job> </group> </config>
Step 2: Need to add a new group on system.xml in the following the path
app\code\Vendor\Extension\etc\adminhtml\system.xml
<group id="configurable_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>Vendor\Extension\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>
Step 3: Now add Cronconfig.php in the following path
app\code\Vendor\Extension\Model\Config\Cronconfig.php
<?php namespace Vendor\Extension\Model\Config; class Cronconfig extends \Magento\Framework\App\Config\Value { const CRON_STRING_PATH = 'crontab/default/jobs/my_cron_job/schedule/cron_expr'; const CRON_MODEL_PATH = 'crontab/default/jobs/my_cron_job/run/model'; /** * @var \Magento\Framework\App\Config\ValueFactory */ protected $_configValueFactory; /** * @var mixed|string */ protected $_runModelPath = ''; /** * CronConfig1 constructor. * @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|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $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); } /** * @return CronConfig1 * @throws \Exception */ public function afterSave() { $time = $this->getData('groups/configurable_cron/fields/time/value'); $frequency = $this->getData('groups/configurable_cron/fields/frequency/value'); $cronExprArray = [ intval($time[1]), intval($time[0]), $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', '*', $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*', ]; $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(__('Some Thing Want Wrong , We can\'t save the cron expression.')); } return parent::afterSave(); } }
Step 4: Now add Cronfile.php in the following path
app\code\Vendor\Extension\Cron\Cronfile.php
<?php namespace Vendor\Extension\Cron; class Cronfile { public function execute() { //Add your cron code. } }
Conclusion:
Hence, now all are able to Setup Cron Time by Configuration in Magento 2. In case of any difficulty let me know in the comment section below. Stay Safe, Stay Home, and Stay Updated!
Happy Coding