Question

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

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top