Skip to content

Commit

Permalink
Merge pull request #128 from alleyinteractive/performance/schedule
Browse files Browse the repository at this point in the history
Improve garabge collector performance by using standalone interval
  • Loading branch information
srtfisher authored Sep 4, 2024
2 parents fa8169a + 32a637b commit 20a8c7b
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 35 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/all-pr-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "All Pull Request Tests"

on:
pull_request:
branches:
- develop
types: [opened, synchronize, reopened, ready_for_review]

jobs:
# We use a single job to ensure that all steps run in the same environment and
# reduce the number of minutes used.
pr-tests:
# Don't run on draft PRs
if: github.event.pull_request.draft == false
# Timeout after 10 minutes
timeout-minutes: 10
# Define a matrix of PHP/WordPress versions to test against
strategy:
matrix:
php: [8.1, 8.2, 8.3]
wordpress: ["latest"]
runs-on: ubuntu-latest
# Cancel any existing runs of this workflow
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}-P${{ matrix.php }}-WP${{ matrix.wordpress }}
cancel-in-progress: true
# Name the job in the matrix
name: "PR Tests PHP ${{ matrix.php }} WordPress ${{ matrix.wordpress }}"
steps:
- uses: actions/checkout@v4

- name: Run General Tests
# See https://github.com/alleyinteractive/action-test-general for more options
uses: alleyinteractive/action-test-general@develop

- name: Run PHP Tests
# See https://github.com/alleyinteractive/action-test-php for more options
uses: alleyinteractive/action-test-php@develop
with:
php-version: '${{ matrix.php }}'
wordpress-version: '${{ matrix.wordpress }}'
skip-wordpress-install: 'true'
13 changes: 0 additions & 13 deletions .github/workflows/coding-standards.yml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/unit-test.yml

This file was deleted.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a
CHANGELOG](https://keepachangelog.com/en/1.0.0/).

## 2.5.0 - 2024-09-03

- Change the garbage collector to schedule a single recurring event to clean up logs instead of `wp_schedule_single_event`.

## 2.4.4 - 2024-08-26

- Display the relative time for the log.

## 2.4.3 - 2024-07-02

- Fix a serialization of closure error when using an exception in a log's context.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"phpcbf": "phpcbf --standard=./phpcs.xml .",
"phpcs": "phpcs --standard=./phpcs.xml .",
"phpunit": "phpunit",
"release": "npx @alleyinteractive/create-release@latest",
"test": [
"@phpcs",
"@phpunit"
Expand Down
23 changes: 22 additions & 1 deletion inc/class-ai-logger-garbage-collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class AI_Logger_Garbage_Collector {
* Register hooks.
*/
public static function add_hooks() {
add_action( 'cron_schedules', [ static::class, 'add_cron_interval' ] );

if (
false === \has_action( static::CRON_HOOK )

Expand All @@ -38,11 +40,30 @@ public static function add_hooks() {

// Schedule the next run if it isn't already.
if ( false === \wp_next_scheduled( static::CRON_HOOK ) ) {
\wp_schedule_single_event( time() + ( HOUR_IN_SECONDS * 3 ), static::CRON_HOOK );
\wp_schedule_event( time(), 'every_three_hours', static::CRON_HOOK );
}
}
}

/**
* Add a custom cron interval.
*
* @param array $schedules Existing cron schedules.
* @return array
*/
public static function add_cron_interval( $schedules ): array {
if ( ! is_array( $schedules ) ) {
$schedules = [];
}

$schedules['every_three_hours'] = [
'interval' => HOUR_IN_SECONDS * 3,
'display' => esc_html__( 'Every 3 Hours', 'ai-logger' ),
];

return $schedules;
}

/**
* Run the garbage collector.
*
Expand Down
8 changes: 8 additions & 0 deletions inc/class-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,12 @@ public function generate( $args, $assoc_args ) {

WP_CLI::log( 'Generated ' . $assoc_args['count'] . ' log entries.' );
}

/**
* Run the Garbage Collector.
*/
public function cleanup() {
AI_Logger_Garbage_Collector::run_cleanup( false );
WP_CLI::success( 'Cleanup complete.' );
}
}
2 changes: 1 addition & 1 deletion logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Alley Logger
* Plugin URI: https://github.com/alleyinteractive/logger
* Description: A Monolog-based logging tool for WordPress. Supports storing log message in a custom post type or in individual posts and terms.
* Version: 2.4.4
* Version: 2.5.0
* Author: Alley Interactive
* Author URI: https://alley.com/
* Requires at least: 5.9
Expand Down
29 changes: 29 additions & 0 deletions tests/GarbageCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace AI_Logger\Tests;

use AI_Logger\AI_Logger_Garbage_Collector;
use Mantle\Testkit\Test_Case;

/**
* Test for the Garbage Collector.
*/
class GarbageCollectorTest extends Test_Case {
public function test_it_schedules_the_cron_event() {
$this->assertInCronQueue( AI_Logger_Garbage_Collector::CRON_HOOK );
}

public function test_it_runs_cleanup() {
$this->expectApplied( 'ai_logger_garbage_collector_max_age' )->andReturnInteger();

$old_log_id = static::factory()->post->for( 'ai_log' )->create( [
'post_date' => date( 'Y-m-d H:i:s', strtotime( '-1 month' ) ),
] );

$recent_log_id = static::factory()->post->for( 'ai_log' )->create();

AI_Logger_Garbage_Collector::run_cleanup( false );

$this->assertInstanceOf( \WP_Post::class, get_post( $recent_log_id ) );
$this->assertNull( get_post( $old_log_id ) );
}
}

0 comments on commit 20a8c7b

Please sign in to comment.