How does the cron group work in Magento 2?
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
<config> <group id="<group_name>"> <job name="<job_name>" instance="<classpath>" method="<method>"> <schedule><time></schedule> </job> </group> </config>
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 and we can run the cron for one group at a time.
Create a 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
Example:
<config> <group id="<group_name>"> <schedule_generate_every>1</schedule_generate_every> <schedule_ahead_for>4</schedule_ahead_for> <schedule_lifetime>2</schedule_lifetime> <history_cleanup_every>10</history_cleanup_every> <history_success_lifetime>60</history_success_lifetime> <history_failure_lifetime>600</history_failure_lifetime> <use_separate_process>1</use_separate_process> </group> </config>
Where:
- 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
Step 1
Run Magento cron jobs for your custom group:
php bin/magento cron:run --group="group_name"
Execute this multiple times to check and confirm.
Step 2
Clean the Magento cache:
php bin/magento cache:clean
Step 3
Log in to the Magento Admin as an administrator.
Step 4
Click Stores > Configuration > Advanced > System.
Step 5
In the right pane, expand Cron. Your group of crons are displayed as follows:
Thanks for reading! ?