This article will help you to understand how the cron group works in Magento 2.
A cron group is a logical group that allows you to easily run a cron for more than one process at a time. Most Magento modules use the default cron group; some modules use the index group.
Example : <your component base dir>/<vendorname>/module-<name>/etc/crontab.xml
<job name="<job_name>" instance="<classpath>" method="<method>">
Here when defining the crontab for the module we need to define the group name too. Here group_name is the name of the cron group. The group name doesn’t have to be unique. We can run the cron for one group at a time.
Create custom cron group
Declare a new group and specify its configuration options (all of which run in the store’s view scope) through the cron_groups.xml file, located at:
<your component base dir>/<vendorname>/module-<name>/etc/cron_groups.xml
- group_name – Name of the custom group.
- schedule_generate_every – Frequency (in minutes) that schedules are written to the cron_schedule table.
- schedule_ahead_for – Time (in minutes) in advance that schedules are written to the cron_schedule table.
- schedule_lifetime – Window of time (in minutes) that cron job must start or will be considered missed (“too late” to run).
- history_cleanup_every – Time (in minutes) that cron history is kept in the database.
- history_success_lifetime – Time (in minutes) that the record of successfully completed cron jobs is kept in the database.
- history_failure_lifetime – Time (in minutes) that the record of failed cron jobs is kept in the database.
- use_separate_process – This feature is available only for Magento 2.1 and later.
Verify your custom cron group
Run Magento cron jobs for your custom group:
php bin/magento cron:run --group="group_name"
Execute this multiple times to check and confirm.
Clean the Magento cache:
php bin/magento cache:clean
Log in to the Magento Admin as an administrator.
Click Stores > Configuration > Advanced > System.
In the right pane, expand Cron. Your group of crons are displayed as follows:
Thanks for reading! 🙂