Magento 2: How override native crontab.xml
Question
I would like to override some cron jobs of native crontab.xml
for example,
vendor/amzn/amazon-pay-and-login-magento-2-module/src/Payment/etc/crontab.xml
<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="get_amazon_capture_updates" instance="Amazon\Payment\Cron\GetAmazonCaptureUpdates" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
<job name="get_amazon_authorization_updates" instance="Amazon\Payment\Cron\GetAmazonAuthorizationUpdates" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
<job name="amazon_payments_process_queued_refunds" instance="Amazon\Payment\Cron\ProcessAmazonRefunds" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
I've created a very simple extension here
app/code/CustomVendor/Cron/
with file
app/code/CustomVendor/Cron/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="get_amazon_capture_updates" instance="Amazon\Payment\Cron\GetAmazonCaptureUpdates" method="execute">
<schedule>0 1 * * 1</schedule>
</job>
<job name="get_amazon_authorization_updates" instance="Amazon\Payment\Cron\GetAmazonAuthorizationUpdates" method="execute">
<schedule>0 1 * * 1</schedule>
</job>
<job name="amazon_payments_process_queued_refunds" instance="Amazon\Payment\Cron\ProcessAmazonRefunds" method="execute">
<schedule>0 1 * * 1</schedule>
</job>
<job name="magento_newrelicreporting_cron" instance="Magento\NewRelicReporting\Model\Cron" method="runCron">
<schedule>0 2 * * 1</schedule>
</job>
</group>
</config>
I ran the commands:
php bin/magento setup:upgrade
php bin/magento cron:run
php bin/magento setup:cron:run
Checked cron_schedule table. As I see it does not help.
What I did wrong. Please help.
Solution
Best way is to add this kind of record for core_config_data
table :
path : crontab/default/jobs/get_amazon_capture_updates/schedule/cron_expr
value : 0 1 * * 1
scope: default
scope_id: 0
You have to do this for all cronjobs you want to update. I also suggest to install module https://github.com/Ethan3600/magento2-CronjobManager which allows you to change schedule for each cron job directly from the admin panel.
OTHER TIPS
I have just overridden a few native cron jobs which were present in the Magento_Indexer
module.
The concept is, to override a native cron, you have to create a crontab.xml file in your custom module and use the same name of cron job in your custom module which is mentioned in the native cron job. Also add the dependency (sequence
node )in etc/module.xml
.
The process I followed to achieve it via custom module is given below.
Added following code to etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Amitshree_Mymodule" setup_version="1.0.0">
<sequence>
<module name="Magento_Indexer"/>
</sequence>
</module>
</config>
And added below code to etc/crontab.xml
file
<?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="index">
<job name="indexer_reindex_all_invalid" instance="Magento\Indexer\Cron\ReindexAllInvalid" method="execute">
<schedule>5 * * * *</schedule>
</job>
<job name="indexer_update_all_views" instance="Magento\Indexer\Cron\UpdateMview" method="execute">
<schedule>9 * * * *</schedule>
</job>
<job name="indexer_clean_all_changelogs" instance="Magento\Indexer\Cron\ClearChangelog" method="execute">
<schedule>4 * * * *</schedule>
</job>
</group>
</config>
Question: How to verify it works properly?
Answer: Follow below steps to verify it.
- After making above changes flush your cache.
- run command
php bin/magento cron:run
from root of your project - Connect to your database via sql command
use magento2;
run below sql queries
1. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_reindex_all_invalid';
Output:
+-----------------------------+---------------------+
| job_code | scheduled_at |
+-----------------------------+---------------------+
| indexer_reindex_all_invalid | 2020-02-26 11:05:00 |
+-----------------------------+---------------------+
2. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_update_all_views';
Output:
+--------------------------+---------------------+
| job_code | scheduled_at |
+--------------------------+---------------------+
| indexer_update_all_views | 2020-02-26 11:09:00 |
+--------------------------+---------------------+
3. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_clean_all_changelogs';
Output:
+------------------------------+---------------------+
| job_code | scheduled_at |
+------------------------------+---------------------+
| indexer_clean_all_changelogs | 2020-02-26 11:04:00 |
+------------------------------+---------------------+