From 573c8a982b2e699c252175f59638b44a07daebc6 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 3 Sep 2024 13:00:57 -0400 Subject: [PATCH 1/3] Improve garabge collector performance by using standalone interval --- .github/workflows/all-pr-tests.yml | 42 +++++++++++++++++++++++ .github/workflows/coding-standards.yml | 13 ------- .github/workflows/unit-test.yml | 20 ----------- inc/class-ai-logger-garbage-collector.php | 23 ++++++++++++- inc/class-cli.php | 8 +++++ tests/GarbageCollectorTest.php | 29 ++++++++++++++++ 6 files changed, 101 insertions(+), 34 deletions(-) create mode 100644 .github/workflows/all-pr-tests.yml delete mode 100644 .github/workflows/coding-standards.yml delete mode 100644 .github/workflows/unit-test.yml create mode 100644 tests/GarbageCollectorTest.php diff --git a/.github/workflows/all-pr-tests.yml b/.github/workflows/all-pr-tests.yml new file mode 100644 index 00000000..cfc90462 --- /dev/null +++ b/.github/workflows/all-pr-tests.yml @@ -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' diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml deleted file mode 100644 index 78fc7708..00000000 --- a/.github/workflows/coding-standards.yml +++ /dev/null @@ -1,13 +0,0 @@ -name: Coding Standards - -on: - push: - branches: - - develop - pull_request: - schedule: - - cron: '0 0 * * *' - -jobs: - coding-standards: - uses: alleyinteractive/.github/.github/workflows/php-coding-standards.yml@main diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml deleted file mode 100644 index 2baf6069..00000000 --- a/.github/workflows/unit-test.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: Testing Suite - -on: - push: - branches: - - develop - pull_request: - schedule: - - cron: '0 0 * * *' - -jobs: - php-tests: - strategy: - matrix: - php: [8.2] - wordpress: ["latest"] - uses: alleyinteractive/.github/.github/workflows/php-tests.yml@main - with: - php: ${{ matrix.php }} - wordpress: ${{ matrix.wordpress }} diff --git a/inc/class-ai-logger-garbage-collector.php b/inc/class-ai-logger-garbage-collector.php index 1983a4ca..5bea48e9 100644 --- a/inc/class-ai-logger-garbage-collector.php +++ b/inc/class-ai-logger-garbage-collector.php @@ -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 ) @@ -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. * diff --git a/inc/class-cli.php b/inc/class-cli.php index a96f4837..89de141d 100644 --- a/inc/class-cli.php +++ b/inc/class-cli.php @@ -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.' ); + } } diff --git a/tests/GarbageCollectorTest.php b/tests/GarbageCollectorTest.php new file mode 100644 index 00000000..568d8a23 --- /dev/null +++ b/tests/GarbageCollectorTest.php @@ -0,0 +1,29 @@ +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 ) ); + } +} From 79d3d5e7e1c919698fbf41c04694dc4744250a38 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 3 Sep 2024 13:02:58 -0400 Subject: [PATCH 2/3] CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2fb9ee0..38e81fe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. From 32a637b9aa0ec1dbc09a865e0cf22ae8040c3d8a Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 3 Sep 2024 13:03:51 -0400 Subject: [PATCH 3/3] Bump version --- composer.json | 1 + logger.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bb088c1e..687d0cb8 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/logger.php b/logger.php index 52732997..bb319002 100644 --- a/logger.php +++ b/logger.php @@ -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