Skip to content

Commit

Permalink
Merge pull request #24 from newfold-labs/update/action-scheduler-rete…
Browse files Browse the repository at this point in the history
…ntion-period

filter action scheduler retention period and batch size
  • Loading branch information
wpalani authored Aug 15, 2024
2 parents 91021b0 + 53d04f4 commit ed7db2c
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions includes/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use NewfoldLabs\WP\Module\Performance\CacheTypes\Skip404;
use NewfoldLabs\WP\ModuleLoader\Container;

/**
* Performance Class
*/
class Performance {

/**
Expand Down Expand Up @@ -57,7 +60,7 @@ class Performance {
/**
* Constructor.
*
* @param Container $container
* @param Container $container the container
*/
public function __construct( Container $container ) {

Expand All @@ -71,15 +74,19 @@ public function __construct( Container $container ) {

// Ensure that purgeable cache types are enabled before showing the UI.
if ( $cachePurger->canPurge() ) {
add_action( 'admin_bar_menu', [ $this, 'adminBarMenu' ], 100 );
add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 100 );
}

$container->set( 'cachePurger', $cachePurger );

$container->set( 'hasMustUsePlugin', file_exists( WPMU_PLUGIN_DIR . '/endurance-page-cache.php' ) );

}

/**
* Constructor.
*
* @param Container $container the container
*/
public function configureContainer( Container $container ) {

global $is_apache;
Expand All @@ -94,17 +101,18 @@ function () {
}
)
);

}

/**
* Add hooks.
*
* @param Container $container the container
*/
public function hooks( Container $container ) {

add_action( 'admin_init', [ $this, 'registerSettings' ], 11 );
add_action( 'admin_init', array( $this, 'registerSettings' ), 11 );

new OptionListener( self::OPTION_CACHE_LEVEL, [ $this, 'onCacheLevelChange' ] );
new OptionListener( self::OPTION_CACHE_LEVEL, array( $this, 'onCacheLevelChange' ) );

/**
* On CLI requests, mod_rewrite is unavailable, so it fails to update
Expand All @@ -129,8 +137,47 @@ function () {
}
);

add_action( 'after_mod_rewrite_rules', [ $this, 'onRewrite' ] );
add_action( 'after_mod_rewrite_rules', array( $this, 'onRewrite' ) );
add_filter( 'action_scheduler_retention_period', array( $this, 'nfd_asr_default' ) );
add_filter( 'action_scheduler_cleanup_batch_size', array( $this, 'nfd_as_cleanup_batch_size' ) );
}

/**
* Update the default action scheduler retention period to 5 days instead of 30.
* The actions scheduler table tends to grow to gigantic sizes and this should help.
*
* @hooked action_scheduler_retention_period
* @see ActionScheduler_QueueCleaner::delete_old_actions()
*
* @param int $retention_period Minimum scheduled age in seconds of the actions to be deleted.
*
* @return int New retention period in seconds.
*/
public function nfd_asr_default( $retention_period ) {
return 5 * constant( 'DAY_IN_SECONDS' );
}

/**
* Increase the batch size for the cleanup process from default of 20 to 1000.
*
* @hooked action_scheduler_cleanup_batch_size
* @see ActionScheduler_QueueCleaner::get_batch_size()
*
* @param int $batch_size Existing batch size; default is 20.
*
* @return int 1000 when running the cleanup process, otherwise the existing batch size.
*/
public function nfd_as_cleanup_batch_size( $batch_size ) {
/**
* Apply only to {@see ActionScheduler_QueueCleaner::delete_old_actions()} and not to
* {@see ActionScheduler_QueueCleaner::reset_timeouts()} or
* {@see ActionScheduler_QueueCleaner::mark_failures()} batch sizes.
*/
if ( ! did_filter( 'action_scheduler_retention_period' ) ) {
return $batch_size;
}

return 1000;
}

/**
Expand All @@ -147,6 +194,8 @@ public function onRewrite() {
*/
public function onCacheLevelChange( $cacheLevel ) {
/**
* Respone Header Manager from container
*
* @var ResponseHeaderManager $responseHeaderManager
*/
$responseHeaderManager = $this->container->get( 'responseHeaderManager' );
Expand All @@ -159,6 +208,9 @@ public function onCacheLevelChange( $cacheLevel ) {
}
}

/**
* Register settings
*/
public function registerSettings() {

global $wp_settings_fields;
Expand Down Expand Up @@ -192,7 +244,7 @@ public function registerSettings() {
/**
* Add options to the WordPress admin bar.
*
* @param \WP_Admin_Bar $wp_admin_bar
* @param \WP_Admin_Bar $wp_admin_bar the admin bar
*/
public function adminBarMenu( \WP_Admin_Bar $wp_admin_bar ) {

Expand All @@ -204,42 +256,40 @@ public function adminBarMenu( \WP_Admin_Bar $wp_admin_bar ) {
if ( current_user_can( 'manage_options' ) ) {

$wp_admin_bar->add_node(
[
array(
'id' => 'nfd_purge_menu',
'title' => __( 'Caching', 'newfold-module-performance' ),
]
)
);

$wp_admin_bar->add_node(
[
array(
'id' => 'nfd_purge_menu-purge_all',
'title' => __( 'Purge All', 'newfold-module-performance' ),
'parent' => 'nfd_purge_menu',
'href' => add_query_arg( [ self::PURGE_ALL => true ] ),
]
'href' => add_query_arg( array( self::PURGE_ALL => true ) ),
)
);

if ( ! is_admin() ) {
$wp_admin_bar->add_node(
[
array(
'id' => 'nfd_purge_menu-purge_single',
'title' => __( 'Purge This Page', 'newfold-module-performance' ),
'parent' => 'nfd_purge_menu',
'href' => add_query_arg( [ self::PURGE_URL => true ] ),
]
'href' => add_query_arg( array( self::PURGE_URL => true ) ),
)
);
}

$wp_admin_bar->add_node(
[
array(
'id' => 'nfd_purge_menu-cache_settings',
'title' => __( 'Cache Settings', 'newfold-module-performance' ),
'parent' => 'nfd_purge_menu',
'href' => admin_url( 'options-general.php#' . Performance::SETTINGS_ID ),
]
)
);
}

}

}

0 comments on commit ed7db2c

Please sign in to comment.