How to create cron and command in Magento 2

We had a requirement on our image optimiser module to optimise images using cron schedule but we did’t want to hard code schedule time so we explored and found that you can give config path to run cron job instead of hard coding schedule time in crontab.xml.

In this tutorial, we are going to cover two things today how to create cron using config path setting and run cron job manually using command line interface (CLI)

As usual we are going to do step by step guide to achieve both cron creating and running cron job manually -:

Create Cron Job using system.xml (config path)

Step 1 – Create system configuration under etcadminhtmlsystem.xml

 <group id="image_optimiser_settings" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <field id="image_optimiser_schedule" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Image Optimiser Schedule</label> <comment> <![CDATA[ This will allow you to define schedule how often you want to optimise images <pre>
* * * * *
| | | | |
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
Example: 0 0 * * * Daily at midnight
</pre> ]]> </comment> </field> 

Step 2 – Create cron schedule file to pick up scheduled time from system configuration path etccrontab.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!--
/** * Scommerce Mage Image Optimiser for Cron Schedule * @category Scommerce * @package Scommerce_ImageOptimiser * @author Scommerce Mage <core@2572495e08.nxcli.net> */
--> <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="scommerce_image_optimiser" instance="ScommerceImageOptimiserCronImageOptimiser" method="execute"> <config_path>scommerce/image_optimiser_settings/image_optimiser_schedule </config_path> </job> </group>
</config>

N.B. config_path where you will define actual path mentioned in Step 1

Step 3 – Create main cron file ImageOptimiser and execute function as defined in Step 2 under Cron folder. This will be used to execute main function of your model class.

 /** * Cron job to run image optimisation * * @category Scommerce * @package Scommerce_ImageOptimiser * @author Scommerce Mage <core@2572495e08.nxcli.net> * */ namespace ScommerceImageOptimiserCron;
use ScommerceImageOptimiseModelImageOptimise; class ImageOptimiser
{ /** * @var ImageOptimise */ private $_imageOptimiser; /** * Image Optimisation * @param ImageOptimise $imageOptimiser */ public function __construct( ImageOptimise $imageOptimiser ) { $this-> _imageOptimiser = $imageOptimiser; } public function execute() { $this->_imageOptimiser->imageOptimise(); }
}

Create Magento 2 command to execute cron job manually through CLI

Step 1 – Create an entry in etcdi.xml for adding a new command in Magento 2 to run your cron job

 <?xml version="1.0" encoding="UTF-8"?>
<!--
/** * Scommerce Mage Image Optimiser for Cron Schedule * @category Scommerce * @package Scommerce_ImageOptimiser * @author Scommerce Mage <core@2572495e08.nxcli.net> */
--> <type name="MagentoFrameworkConsoleCommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="imageoptimisation" xsi:type="object">ScommerceImageOptimiserConsoleCommandImageOptimiser</item> </argument> </arguments>
</type>

Step 2 – Create command file to be used for executing your cron job manually under ConsoleCommand

/** * Console Command to run image optimisation * * @category Scommerce * @package Scommerce_ImageOptimiser * @author Scommerce Mage <core@2572495e08.nxcli.net> * */ namespace ScommerceImageOptimiserConsoleCommand; use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use MagentoFrameworkConsoleCli;
use ScommerceImageOptimiserCronImageOptimiser as CronIO; class ImageOptimiser extends Command
{ /** * @var CronIO */ private $_cronIO; /** * @param CronIO $cronIO */ public function __construct( CronIO $cronIO ) { parent::__construct(); $this->_cronIO = $cronIO; } protected function configure() { $this->setName("scommerce:imageoptimiser:imageoptimisation")->setDescription('Scommerce Mage Image Optimisation'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { $this-> _cronIO->execute(); return Cli::RETURN_SUCCESS; }
} 

N.B – scommerce:imageoptimiser:imageoptimisation, imageoptimisation is exactly same as defined in Step 1 in di.xml file

Step 3 – Run the following command to execute your cron job code manually

php bin/magento scommerce:imageoptimiser:imageoptimisation

Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Similar Posts