In Magento 2, cron jobs are used to schedule and automate tasks, such as reindexing and sending emails and more.
Magento 2 Cron Installation ensures that all store-related tasks are executed correctly, automating essential processes and helping you identify and troubleshoot any potential errors that may arise.
Managed via XML files, they can be set up through the admin panel or command line ensuring proper configuration. We can also create custom cron in Magento 2
Cron groups in Magento 2
Magento 2 uses different cron groups to manage various types of scheduled tasks. The default Magento crontab typically includes three main cron groups: default
, index
, and consumer
.
1. Default Cron Group (default
)
This group is responsible for a wide variety of background processes that keep the Magento store running smoothly.
- Email Sending: Sending transactional emails, such as order confirmations, shipment notifications, etc.
- Cache Cleanup: Regularly cleans up expired cache entries to free up space and ensure that the cache is up-to-date.
- Catalog Price Rules: Applying and updating catalog price rules.
- Sales Reports: Generating and updating various sales reports.
- Log Cleaning: Cleans up old log data to maintain database performance and reduce storage usage.
- Currency Rate Update: Updates currency exchange rates if the store supports multiple currencies.
- Product Alert: Sends product stock and price alerts to customers who have subscribed to notifications.
- Customer Segments: Updates customer segment data based on defined rules.
- Magento Updates: Checks for Magento updates and notifications from the Magento Marketplace.
2. Index Cron Group (index
)
Reindexing ensures that data such as product prices, inventory levels, and category associations are up to date and optimized for performance.
- Product Price Indexing: Recalculates and updates product prices, taking into account discounts, customer group pricing, and other factors.
- Stock Status Indexing: Updates the inventory levels of products, ensuring that the display stock status (in stock/out of stock) is correct.
- Category Products Indexing: Updates the relationships between products and categories.
- Search Indexing: Updates the search index to ensure that product searches return accurate results.
- URL Rewrites: Updates the URL rewrite table and ensures that friendly URLs are generated and old URLs redirect properly.
- Catalog Rule Indexing: Applies catalog price rules across the product catalog.
- Customer Data Indexing: Updates customer-related data, such as customer group assignments and other attributes.
3. Consumer Cron Group (consumer
)
The consumer
cron group handles tasks related to message queues. So we use Message queues for asynchronous processing
- Message Queue Consumers: These are processes that consume messages from the queue and execute the associated tasks. Each message in the queue represents a specific job.
- Asynchronous Order Processing: Handles tasks related to processing orders in the background and ensuring that the checkout process remains fast and responsive.
- Bulk API Operations: Processes bulk API requests and that are queued for later execution.
- Inventory Reservations: Manages the reservation of inventory for orders that are being processed asynchronously.
- Mass Actions: Handles mass actions triggered by admin operations, such as updating multiple products, categories, and customers at once.
Cron schedule by Configuration in Magento 2
Go To Store > Configuration > Advance > System
- Generate Schedules Every: This field specifies how often (in minutes) Magento should generate new cron job entries in the
cron_schedule
table. - Schedule Ahead for: Defines how far into the future Magento should generate cron jobs.
- Missed if Not Run Within: Determines how long Magento will wait before considering a cron job as missed because it hasn’t run within the expected time frame.
- History Cleanup Every: Magento cleans up old cron job entries from the
cron_schedule
table. - Success History Lifetime: How long to keep successful cron job entries in the database.
- Failure History Lifetime: How long to keep failed cron job entries in the database.
- Use Separate Process: The job is execute in a separate PHP process, isolated from the main cron process.
Example Scenario of Schedule Ahead for
Imagine you set up a cron job to indexer (Generate Schedules Every) for 5 minutes and your “Schedule Ahead for” is 1 hour:
- So At 12:00 PM, Magento will generate 12 entries for the index cron job (for 12:05, 12:10, 12:15, …, and up to 1:00 PM).
Example Scenario of Missed if Not Run Within
Imagine you set up a Missed if Not Run Within for 15 minutes and Schedule The cron job to run at 2:00 AM:
- Due to server load and other issues, the cron process is delayed. Magento will mark it as “missed.” because the job does not start by 2:15 AM just because of Missed if Not Run Within.
- So the job will not be executed at all for that day and ensure that only timely and relevant reports are generated.
Set custom cron By XML file
Create crontab.xml as follows in the app/code/Vendor/Module/etc. directory and copy 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="custom_cronjob" instance="Vendor\Module\Cron\SampleCron" method="execute"> <schedule>* * * * *</schedule> </job> </group> </config>
- group id: Name of the cron group
- job name: Unique ID for this cron job.
- classpath: Class Path.
- method: Method in the classpath to call.
- time: Schedule in cron format
* * * * * time format | | | | | | | | | +---------- Day of week (0 - 7) (Sunday=0 or 7) | | | +------------ Month (1 - 12) | | +-------------- Day of the month (1 - 31) | +---------------- Hour (0 - 23) +------------------ Minute (0 - 59)
After it compiles the code and clears the cache
php bin/magento setup:di:compile
php bin/magento cache:clean
This is a simple step for creating a custom theme in Magento 2.
If you require technical support, feel free to email us at [email protected].
Additionally, explore a wide array of solutions to boost your store’s capabilities by visiting the Adobe Commerce modules section.
For expert advice or to create tailored features, hire Adobe Commerce Developers for your project.
Be the first to comment.