Skip to content

Releases: TonyMerlin/M2_MailchimpCronFix

Release list

Version 1.0.0

Choose a tag to compare

@TonyMerlin TonyMerlin released this 04 Sep 11:15
707bcb4

Merlin_MailchimpCronFix

Magento 2 compatibility module for adjusting the Mailchimp / Ebizmarts cron schedule on high-volume stores.

Purpose

Merlin_MailchimpCronFix overrides the default Mailchimp ecommerce cron schedule provided by:

mailchimp/mc-magento2
Ebizmarts_MailChimp

The default Mailchimp module schedules the ecommerce synchronisation job every five minutes:

<job name="ebizmarts_ecommerce"
     instance="Ebizmarts\MailChimp\Cron\Ecommerce"
     method="execute">
    <schedule>*/5 * * * *</schedule>
</job>

On The Appliance Depot production Magento installation, this job was consistently taking approximately:

343–345 seconds

to complete.

Because five minutes is only 300 seconds, the Mailchimp ecommerce cron job was taking longer to execute than its configured interval.

This resulted in:

  • overlapping cron activity,
  • Mailchimp jobs being marked as missed,
  • Mailchimp jobs remaining in error / incomplete states,
  • increased cron process contention,
  • Magento cron schedules falling approximately 14 minutes behind,
  • delayed execution of unrelated cron jobs,
  • delayed execution of consumers_runner,
  • increased risk of message queue backlogs.

Changes

This module overrides the Mailchimp ecommerce cron schedule from:

Every 5 minutes

to:

Every 15 minutes

Cron expression:

*/15 * * * *

This provides significantly more headroom for the Mailchimp ecommerce synchronisation process.

With a typical runtime of approximately 5 minutes 45 seconds:

Configured interval:  900 seconds
Observed runtime:      ~345 seconds
Available headroom:    ~555 seconds

The module may also override the Mailchimp cron group's schedule_lifetime.

The original Mailchimp configuration uses:

<schedule_lifetime>2</schedule_lifetime>

This means a Mailchimp job which does not begin within two minutes of its scheduled execution time may be marked as missed.

The compatibility module increases this to:

<schedule_lifetime>10</schedule_lifetime>

This allows additional tolerance for temporary cron scheduling pressure without changing Magento's global cron configuration.

Original Mailchimp Configuration

The original cron configuration is provided by:

vendor/mailchimp/mc-magento2/etc/crontab.xml

and contains:

<group id="mailchimp">
    <job name="ebizmarts_ecommerce"
         instance="Ebizmarts\MailChimp\Cron\Ecommerce"
         method="execute">
        <schedule>*/5 * * * *</schedule>
    </job>
</group>

The Mailchimp cron group configuration is provided by:

vendor/mailchimp/mc-magento2/etc/cron_groups.xml

with:

<group id="mailchimp">
    <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>

No files under vendor/ are modified by this extension.

Module Structure

app/code/Merlin/MailchimpCronFix/
├── registration.php
├── README.md
└── etc/
    ├── module.xml
    ├── crontab.xml
    └── cron_groups.xml

Installation

Copy the module to:

app/code/Merlin/MailchimpCronFix

Enable the module:

php bin/magento module:enable Merlin_MailchimpCronFix

Run Magento setup upgrade:

php bin/magento setup:upgrade

If the store is running in production mode, complete the normal deployment procedure as required by the environment.

Clean Magento configuration/cache as appropriate:

php bin/magento cache:clean

For production environments, installation should be performed during a controlled deployment or maintenance window.

Verification

Confirm that the module is enabled:

php bin/magento module:status Merlin_MailchimpCronFix

Monitor the Mailchimp ecommerce cron job:

SELECT
    schedule_id,
    job_code,
    status,
    scheduled_at,
    executed_at,
    finished_at,
    TIMESTAMPDIFF(
        SECOND,
        executed_at,
        finished_at
    ) AS runtime_seconds
FROM cron_schedule
WHERE job_code = 'ebizmarts_ecommerce'
ORDER BY schedule_id DESC
LIMIT 20;

After installation, new ebizmarts_ecommerce schedules should be generated approximately every 15 minutes rather than every five minutes.

Expected scheduled times should resemble:

12:00
12:15
12:30
12:45
13:00

rather than:

12:00
12:05
12:10
12:15
12:20

Cron Health Monitoring

The following query can be used to identify delayed Magento cron jobs:

SELECT
    job_code,
    status,
    COUNT(*) AS jobs,
    MIN(scheduled_at) AS oldest_scheduled,
    MAX(scheduled_at) AS newest_scheduled
FROM cron_schedule
WHERE scheduled_at <= NOW()
  AND status IN ('pending', 'running')
GROUP BY job_code, status
ORDER BY oldest_scheduled;

To monitor Magento message queue consumer scheduling:

SELECT
    schedule_id,
    status,
    scheduled_at,
    executed_at,
    TIMESTAMPDIFF(
        SECOND,
        scheduled_at,
        executed_at
    ) AS delay_seconds
FROM cron_schedule
WHERE job_code = 'consumers_runner'
  AND status = 'success'
ORDER BY schedule_id DESC
LIMIT 20;

Under normal operation, consumers_runner should execute close to its scheduled time rather than several minutes late.

Background

This extension was created after investigation of a large Magento MySQL message queue backlog.

The production database had accumulated approximately:

queue_message
~12–14 million rows
~6.6 GiB

queue_message_status
~12–14 million rows
~1.4 GiB

Almost all of the backlog belonged to:

send_conversion_event_to_meta

More than 10 million Meta conversion messages were in NEW status and had not been processed.

After the historical queue was safely purged, the Meta conversion consumer was benchmarked and found to be operating normally.

Further investigation showed that Magento cron execution was approximately 14 minutes behind schedule.

The Mailchimp ecommerce cron was identified as one of the major sources of cron pressure.

Observed Mailchimp ecommerce runtimes included:

343 seconds
345 seconds

while the job was configured to execute every:

300 seconds

The cron schedule therefore did not provide enough time for a normal execution to complete before the next run became due.

Mailchimp Ecommerce Workload

The Ebizmarts\MailChimp\Cron\Ecommerce task performs substantial ecommerce synchronisation work.

Depending on store configuration, this includes processing:

  • subscribers,
  • products,
  • customers,
  • orders,
  • carts,
  • promotional rules,
  • coupon codes,
  • previous API responses,
  • Mailchimp batch operations.

For large Magento catalogues or stores with significant order/customer activity, this can make the ecommerce synchronisation considerably heavier than a typical Magento cron task.

The 15-minute schedule used by this module is therefore intended to reduce cron contention while maintaining frequent Mailchimp ecommerce synchronisation.

Compatibility

Designed for:

Magento 2
Ebizmarts_MailChimp
mailchimp/mc-magento2

Initially developed against:

mailchimp/mc-magento2 103.4.78

Design Principles

This module:

  • does not modify vendor files,
  • does not replace Mailchimp classes,
  • does not change Mailchimp synchronisation logic,
  • changes only cron scheduling behaviour,
  • remains isolated from the original extension,
  • can be removed cleanly if no longer required.

Removal

Disable the module:

php bin/magento module:disable Merlin_MailchimpCronFix

Run the normal Magento deployment/setup process:

php bin/magento setup:upgrade

Once removed, the original Mailchimp cron schedule supplied by mailchimp/mc-magento2 will become active again.

Notes

The 15-minute interval is based on observed production runtimes and should be reviewed if Mailchimp synchronisation behaviour changes significantly.

If average execution time later falls substantially, the interval may be reduced.

If execution time approaches the 15-minute interval, the underlying Mailchimp synchronisation workload should be investigated rather than simply increasing the cron interval indefinitely.

Author

Merlin

Developed for The Appliance Depot Magento platform.