Skip to content

WordPress background-process audit — Action Scheduler queue health, WP-Cron, runaway jobs

Action Scheduler, WP-Cron, and system cron all run background work on WordPress. The audit that finds the runaway job, the orphaned queue, and the silent cron failure.

A WooCommerce site has a database that doubled in size over four months. The store manager swears nothing has changed. You open the dashboard and the front end is fine. Then you check wp_actionscheduler_actions and find 1.2 million rows — 800k of them in failed status, 300k pending, scheduled by a plugin that was uninstalled six months ago. The cron-based scheduler has been retrying them on every page load, the failed-job log table is the biggest table in the database, and nobody noticed because the front end is fine and the cron failures emit no alerts. Action Scheduler, WP-Cron, and system cron are three overlapping background-job systems sharing one database. The audit that finds the runaway job is the one that looks at all three.

What’s happening

WordPress ships with WP-Cron, a poor-man’s scheduler that runs scheduled tasks on the back of incoming HTTP requests. Every page load checks for due cron events and runs them inline. No incoming traffic means no cron execution, which is why production WordPress installs disable WP-Cron and run it from system cron instead (* * * * * wp cron event run --due-now).

Action Scheduler, originally built by Automattic for WooCommerce, is a separate job queue that lives inside WordPress as a library (woocommerce/action-scheduler). It’s the default background-job system for WooCommerce, Yoast indexable updates, and a long tail of plugins. Actions get scheduled in wp_actionscheduler_actions, run by a worker that’s typically triggered from WP-Cron’s action_scheduler_run_queue hook, and logged in wp_actionscheduler_logs.

The result is a tangle:

  • WP-Cron is the trigger
  • Action Scheduler is the queue
  • Some plugins (Jetpack, Yoast) use Action Scheduler
  • Others (older backup plugins, some SEO plugins) still use raw WP-Cron events
  • Some scheduled work bypasses both and uses wp_schedule_event with a custom hook

When something goes wrong, the symptoms manifest in different places:

  • Database growth → usually Action Scheduler actionscheduler_logs and actionscheduler_actions
  • p95 latency spikes → usually WP-Cron running a heavy task inline on a page load
  • Front-end traffic that “feels off” → could be either; symptom is the same
  • Mailbox full of cron alerts → system cron emitting them via MTA

The most common failure pattern is the orphaned action: a plugin was uninstalled, but its scheduled actions are still in the queue. Action Scheduler tries to run them, finds the callback function doesn’t exist, marks them failed, and re-enqueues them. The queue grows without bound.

The second most common pattern is the unbounded-fanout action: a plugin schedules one action per WooCommerce order, every order schedules one, and a black-Friday spike of 50,000 orders creates 50,000 actions that all want to run in the next ten minutes. The worker drains slowly, the database is hammered, the front end slows.

How to verify

Query Action Scheduler directly to characterize queue health:

-- top-level status breakdown
SELECT status, COUNT(*) FROM wp_actionscheduler_actions GROUP BY status;

-- expected output, healthy:
-- complete  | 5000
-- pending   | 50
-- in-progress | 1
-- failed    | < 100  (and these should be old, investigated)

-- which hooks are queueing the most actions?
SELECT hook, status, COUNT(*) AS n
FROM wp_actionscheduler_actions
GROUP BY hook, status
ORDER BY n DESC
LIMIT 20;

-- log table size
SELECT
  ROUND(SUM(data_length+index_length)/1024/1024, 1) AS size_mb,
  TABLE_ROWS AS rows
FROM information_schema.tables
WHERE table_name LIKE 'wp_actionscheduler%';

From WP-CLI, a faster surface:

# all status counts
wp action-scheduler queue

# pending actions for a specific hook
wp action-scheduler list --status=pending --hook=woocommerce_run_subscription_payment_retry --per_page=10

# what's failed and why
wp action-scheduler list --status=failed --per_page=20 --fields=action_id,hook,scheduled_date_gmt,log_entries

# what's overdue
wp action-scheduler list --status=pending --per_page=20 --fields=action_id,hook,scheduled_date_gmt | awk 'NR==1 || $3 < strftime("%Y-%m-%d %H:%M:%S", systime() - 3600)'

For raw WP-Cron events:

wp cron event list
wp cron event list --due-now --format=table

# any events whose hook has no listener?
wp eval '
foreach (_get_cron_array() as $ts => $events) {
  foreach ($events as $hook => $crons) {
    if (!has_action($hook)) {
      echo "orphan: $hook (scheduled for " . date("Y-m-d H:i", $ts) . ")\n";
    }
  }
}
'

For system cron, the diagnostics are at the OS layer:

# what's scheduled?
crontab -u www-data -l

# did it run recently?
grep CRON /var/log/syslog | tail -30
# or on systemd:
journalctl -u cron --since "1 hour ago" | tail

# is wp cron event run --due-now finishing in reasonable time?
sudo -u www-data time wp cron event run --due-now --path=/var/www/example.com 2>&1 | tail -5

Anything that takes more than 60 seconds means a scheduled hook has unbounded work and is blocking the next cron tick.

The fix

The runbook for an Action Scheduler audit has six steps. Apply in order.

Step 1 — kill the orphans. Identify hooks whose plugin is no longer installed and delete pending and failed actions for those hooks:

# list orphan hooks
wp eval '
global $wpdb;
$rows = $wpdb->get_results("SELECT DISTINCT hook FROM {$wpdb->prefix}actionscheduler_actions WHERE status IN (\"pending\", \"failed\")");
foreach ($rows as $r) {
  if (!has_action($r->hook)) echo $r->hook . "\n";
}
' > /tmp/orphan-hooks.txt

# review the list, then delete
while IFS= read -r hook; do
  wp action-scheduler delete --hook="$hook" --status=failed --batch=500
  wp action-scheduler delete --hook="$hook" --status=pending --batch=500
done < /tmp/orphan-hooks.txt

Step 2 — purge the log table. actionscheduler_logs retains a row per state transition per action. On a busy site this grows fast. Configure retention:

// functions.php or a custom mu-plugin
add_filter('action_scheduler_retention_period', function () {
  return 14 * DAY_IN_SECONDS;  // default is 30 days; 14 is more aggressive
});

add_filter('action_scheduler_queue_runner_concurrent_batches', function () {
  return 1;  // default is 5; reduce if cron is hammering the DB
});

Then run the cleanup task once to apply the new retention:

wp action-scheduler clean

Step 3 — rate-limit the fanout hooks. For plugins that schedule per-order or per-user actions, add a delay and a chunked batch. WooCommerce subscriptions and the email-on-order class of plugins are the usual offenders. Look at the scheduled_date_gmt distribution:

SELECT
  DATE_FORMAT(scheduled_date_gmt, '%Y-%m-%d %H:00') AS hour,
  COUNT(*) AS scheduled
FROM wp_actionscheduler_actions
WHERE status = 'pending'
  AND scheduled_date_gmt > NOW()
GROUP BY hour
ORDER BY hour
LIMIT 24;

If a single hour shows 30,000+ scheduled actions, that hour will be a bad time for the front end. Spread them with a deterministic offset:

// custom mu-plugin — re-schedule a fanout hook to distribute
add_filter('woocommerce_email_async_action_args', function ($args, $order_id) {
  // spread by order_id mod 600 seconds (ten minutes)
  $args[0]['offset'] = ($order_id % 600);
  return $args;
}, 10, 2);

Step 4 — disable WP-Cron’s self-trigger, use system cron exclusively. In wp-config.php:

define('DISABLE_WP_CRON', true);

Then a system cron entry per WordPress install:

# /etc/cron.d/wp-cron-example
* * * * * www-data /usr/local/bin/wp cron event run --due-now --path=/var/www/example.com >/dev/null 2>&1

Make sure the wp user has a sensible PATH and the cron entry’s error output is going somewhere. The biggest silent failure is a system cron that runs but errors out — the cron daemon emails the wp-data user, the mail goes to /var/spool/mail/www-data, and nobody reads it. Pipe errors to syslog:

* * * * * www-data /usr/local/bin/wp cron event run --due-now --path=/var/www/example.com 2>&1 | logger -t wp-cron

Step 5 — run Action Scheduler from its own cron, not the WP-Cron tick. For sites under heavy AS load, the WP-Cron tick can become the bottleneck. Run the queue runner directly:

* * * * * www-data /usr/local/bin/wp action-scheduler run --batch-size=25 --batches=4 --path=/var/www/example.com >/dev/null 2>&1

The --batch-size and --batches flags bound how much work each minute does, preventing the queue runner from consuming all the PHP-FPM workers.

Step 6 — alert on the leading indicators. A simple monitoring check that catches the slow build-up:

#!/bin/bash
# /usr/local/bin/check-wp-actionscheduler

PATH=/var/www/example.com
PENDING=$(wp action-scheduler queue --path=$PATH --format=json | jq '.[] | select(.status=="pending") | .count')
FAILED=$(wp action-scheduler queue --path=$PATH --format=json | jq '.[] | select(.status=="failed") | .count')
OVERDUE=$(wp action-scheduler list --path=$PATH --status=pending --per_page=100 --format=json | \
          jq "[.[] | select(.scheduled_date_gmt < (now - 3600 | strftime(\"%Y-%m-%d %H:%M:%S\")))] | length")

# thresholds
if [ "${PENDING:-0}" -gt 5000 ]; then echo "CRIT pending=$PENDING"; exit 2; fi
if [ "${FAILED:-0}" -gt 100  ]; then echo "WARN failed=$FAILED"; exit 1; fi
if [ "${OVERDUE:-0}" -gt 50  ]; then echo "CRIT overdue=$OVERDUE"; exit 2; fi
echo "OK pending=$PENDING failed=$FAILED overdue=$OVERDUE"
exit 0

Wire this into your monitoring (Netdata, Grafana, Uptime Kuma, or whatever sits on the box) and treat the alerts as P3 — they catch the slow drift, not the emergency.


Stack Harbor audits and tunes the WordPress background-job system as part of the enterprise WordPress support operating model — including Action Scheduler retention, WP-Cron handoff to system cron, and the queue-health monitoring. If your database growth is being driven by background-job tables, we can run the audit.