How To

Magento 2: How To Setup Cron Time By Configuration

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

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Also tell how to run this cron,
    when I run with php bin/magento cron:run , it is not executable and also not visible in cron_schedule table

    • Kindly confirm, you have also created remaining file for creating the extension and the extension is enabled into system.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago