Skip to content

Respect doing_cron transient in wp cron event run --due-now#130

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-no-doubles-flag-for-cron
Draft

Respect doing_cron transient in wp cron event run --due-now#130
Copilot wants to merge 3 commits intomainfrom
copilot/add-no-doubles-flag-for-cron

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

When --due-now runs take longer than the invocation interval, the next invocation sees the same in-flight events still in the DB and executes them a second time — duplicating emails, etc.

Changes

  • src/Cron_Event_Command.php — mirrors WordPress core's wp-cron.php lock pattern in run() when --due-now is used:

    • Before executing: reads doing_cron transient; if fresh (within WP_CRON_LOCK_TIMEOUT, default 60 s), emits a warning and returns early
    • Acquires lock via set_transient( 'doing_cron', … ) before running events
    • Releases lock via delete_transient( 'doing_cron' ) after completion (or before an early error exit)
  • features/cron-event.feature — new scenario covering the transient lock behaviour: sets the transient to simulate a concurrent run, asserts the warning fires and no events execute, then clears the transient and confirms normal execution resumes

# Second invocation while first is still running:
$ wp cron event run --due-now
Warning: A cron event run is already in progress; skipping.
Success: Executed a total of 0 cron events.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.wordpress.org
    • Triggering command: /usr/bin/curl curl -s REDACTED (dns block)
  • downloads.wordpress.org
    • Triggering command: /usr/bin/curl curl -sSfL REDACTED -main/dist/ripgrep/bin/linux-x64/rg (dns block)
    • Triggering command: /usr/bin/curl curl -sSfL REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>wp cron event run --due-now doesn't respect doing_cron transient</issue_title>
<issue_description>I use wp cron event run --due-now as my production cron. Every 5 minutes. But it's a heavy cron. Sometimes it takes more then 5 minutes. When that happens some events get executed twice. Mostly sending emails.

I would suggest a --no-doubles flag or something like that to prevent the cron from doing hooks twice.

When the flag is set, wp-cli should check for the doing_cron transient like core does. And set it if it's starting. Like core does.

Relevant core code

Why is it doing things twice

At the start of a cron all tasks are gathered with _get_cron_array, events get deleted once the start. And the remaing cron array is still executed. And still in the DB list So after 5 minutes the new cron get also does a _get_cron_array and will get all events that the first cron also is still handling (and deleting as it goes).</issue_description>

<agent_instructions>See previous attempts at https://gist.github.com/danielbachhuber/e76c5c5e1479c385863cc86f5f21eb87 and https://gist.github.com/danielbachhuber/800870993ce975f7e775219180cc37fa</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber 👍 Seems reasonable. @schlessera I think this should even be the default behavior. I wouldn't consider this a breaking change, I'd classify this as a bug: We're diverting from expected Core behavior.

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add no-doubles flag for wp cron event run Respect doing_cron transient in wp cron event run --due-now Mar 15, 2026
Copilot AI requested a review from swissspidy March 15, 2026 08:03
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp cron event run --due-now doesn't respect doing_cron transient

2 participants