2

I created a cron job as per standard method

Vendor\Module\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 name="my_cronjob" instance="Vendor\Module\Cron\Clean" method="execute">
            <schedule>00 00 * * *</schedule>
        </job>
    </group>
</config>

In My

Vendor\Module\Cron\Clean.php

class Clean extends \Magento\Backend\App\Action
{
    /**
     * @var connection
     */
    private $connection;

    protected $_logger;
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;ory;
        parent::__construct($context);
    }
    public function execute()
    {
        $this->_logger->debug('Sync Starts!!');
    }
}

To schedule a cron i created an admin Config as

enter image description here

My Question is how i can Schedule a cron on basis of these fields

2

1 Answer 1

3

Maybe this is your idea, you should create a configuration:

  • 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 name="masteringAddItem" instance="Mastering\SampleModule\Cron\AddItem" method="execute">
            <config_path>mastering/general/cron_expression</config_path>
        </job>
    </group>
</config>

  • adminhtml/system.xml

<?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="mastering" translate="label" sortOrder="10000">
            <label>Mastering</label>
        </tab>
        <section id="mastering" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>Mastering</label>
            <tab>mastering</tab>
            <resource>Mastering_SampleModule::mastering</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Item creation by schedule</label>
                <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="cron_expression" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Add Item Cron Expression</label>
                </field>
            </group>
        </section>
    </system>
</config>

It able configuration time for conjob in the admin page!

4
  • Use can enter * * * * * to field
    – Jundat95
    Oct 28, 2019 at 7:22
  • i dont want to * * * * *?.. my question is to use dynmaic values
    – Waqar Ali
    Oct 28, 2019 at 8:20
  • I think it is a dynamic config value in admin page
    – Jundat95
    Oct 28, 2019 at 8:23
  • how can i add enable/disable under Stores -> config -> Advanced -> System under Cron?
    – Mohit Rane
    Feb 1, 2022 at 7:37

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.