How To Setup Cron Time by System Configuration in Magento 2?
In this article, we will discuss about How to Setup Cron Time by System Configuration in Magento 2. Using Setup Cron time by system configuration, you have schedule cron dynamically.
Steps to Magento 2 Setup Cron By Configuration:
Step 1 – Create system.xml file
First we create a system.xml file in the app/code/Mageants/Blog/etc/admin.html and add the following code.
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <tab id="mageants" translate="label" sortOrder="100"> <label>Mageants</label> </tab> <section id="crop_setup" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>Blog</label> <tab>Mageants</tab> <resource>Mageants_Blog::config_extension</resource> <group id="blog_cron" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Cron Setup</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>Mageants\Blog\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> |
After you implement the above code, please check output in to the admin panel
and switch to Stores -> Configuration -> Mageants → Blog, You will see the Cron Setup:
Step 2 – Create a CronConfig.php file
After creating system.xml file, we will create a file “CronConfig.php” in the app/code/Mageants/Blog/Model/Config and add the following code.
<?php namespace Mageants\Blog\Model\Config; class CronConfig extends \Magento\Framework\App\Config\Value { /** * Cron string path */ const CRON_STRING_PATH = 'crontab/default/jobs/mageants_blog_cron/schedule/cron_expr'; /** * Cron model path */ const CRON_MODEL_PATH = 'crontab/default/jobs/mageants_blog_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/blog_cron/fields/time/value'); $frequency = $this->getData('groups/blog_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(); } } |
CronConfig class insert the cron time config value in core_config_data table, that we have used in crontab.xml file.
Also Read:- How To Setup Cron Job In Magento 2
Step 3 – Create a crontab.xml file
Create crontab.xml file in app/code/Mageants/Blog/etc and add the following code:
<?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 name="mageants_blog_cron" instance="Mageants\Blog\Cron\Mycron" method="execute"> <config_path>crontab/default/jobs/mageants_blog_cron/schedule/cron_expr</config_path> </job> </group> </config> |
It is a config path “crotab/defaults/jobs/mageants_blog_cron/schedue/cron_expr“ it has time schedule value.
Also, you can see your cron time entry in “core_config_data” table, which time set in Frequency and Start Time fields will be displayed in the system configuration.
Step 4 – Create a Mycron.php file
Now, we need to create a Cron file “Mycron .php” in the app/code/Mageants/Blog/Cron and add the following code.
<?php namespace Mageants\Blog\Cron; class Mycron { public function execute() { //add Your cron code here } } |
After following above steps, run below commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
Conclusion:
Using above article, you can easily understand How to Setup Cron Time by Configuration in Magento 2. If you have any query regarding above code implementation then you can contact us or let us know in comment section.