0

How can I override the crontab.xml of CustomModule1 from CustomModule2?

I know that if it was a Magento module from vendor I could just use the same path on app/code to override it, but I need a way to change the schedule on the crontab.xml of an extension that wasn't installed with composer, without changing the original.

3
  • What exactly you are trying to achieve so we can help to sort out you problem. Commented Jan 21, 2018 at 12:19
  • If you want to change the frequency of when the cron runs, which is what I think you're trying to ask, you can "override" it in the database table core_config _data with a path like this: crontab/default/jobs/my_cron_job/schedule/cron_expr' and set the value to whatever cron expression you want (e.g. */5 * * * *) Commented Jan 22, 2018 at 2:36
  • What I'm trying to achieve is described in the question: "I need a way to change the schedule on the crontab.xml of an extension that wasn't installed with composer, without changing the original". I need to know how to do that (if it's possible) from a custom module (something that can be commited using Git & that I can replicate to other projects). Commented Jan 23, 2018 at 9:22

1 Answer 1

1

It can be achieved by by overriding the crontab.xml file of CustomModule1 in your CustomModule2.

For testing purpose, I have overridden a few crons from Magento_Indexer module in my custom module as 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 |
+------------------------------+---------------------+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.