Skip to content

Instantly share code, notes, and snippets.

@charmoney
Last active July 27, 2022 16:18
Show Gist options
  • Save charmoney/4273a0c5222f622ab1aba6c290a16f1a to your computer and use it in GitHub Desktop.
Save charmoney/4273a0c5222f622ab1aba6c290a16f1a to your computer and use it in GitHub Desktop.
<?php
/*
* The ActionScheduler WordPress plugin removes 20 complete & canceled status actions older than
* the filtered `action_scheduler_retention_period` value in seconds. In large scale applications
* generating more than 20 actions per minute on average, this can cause run away growth of the
* wp_actionscheduler_actions and wp_actionscheduler_logs tables.
*
* This gist adds a second cleanup pass every time AS normally cleans up & runs the queue.
*
* No support provided. No warranties expressed or implied.
*
* @author Chris Harmoney <chris@cornershopcreative.com>
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Direct access not allowed' );
}
/**
* Change Action Scheduler default purge to 2 days
*/
function charmoney_gist_action_scheduler_retention_period() {
return 2 * DAY_IN_SECONDS;
}
add_filter( 'action_scheduler_retention_period', 'charmoney_gist_action_scheduler_retention_period' );
/**
* Manually purge additional old completed AS records to prevent runaway table growth.
*/
function charmoney_gist_as_rq_extra_purge() {
$clean = 1000;
$cleaner = new \ActionScheduler_QueueCleaner( null, $clean );
$cleaner->delete_old_actions();
}
add_action( 'action_scheduler_run_queue', 'charmoney_gist_as_rq_extra_purge', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment