From 3e8e42c1d17a3880c3ca72c5895303c1e6d8b97a Mon Sep 17 00:00:00 2001 From: Tim Broddin Date: Fri, 5 Apr 2024 15:06:47 +0200 Subject: [PATCH] Scheduled updates: Infer status from logs (#36752) * Infer status from logs * Project versions * Project versions * Project versions * Fix type for phan --- .../add-update-manager-infer-status-from-logs | 4 ++ .../packages/scheduled-updates/composer.json | 2 +- .../src/class-scheduled-updates-logs.php | 53 +++++++++++++++++++ .../src/class-scheduled-updates.php | 8 ++- .../add-update-manager-infer-status-from-logs | 5 ++ .../plugins/mu-wpcom-plugin/composer.json | 2 +- .../plugins/mu-wpcom-plugin/composer.lock | 4 +- .../mu-wpcom-plugin/mu-wpcom-plugin.php | 2 +- projects/plugins/mu-wpcom-plugin/package.json | 2 +- 9 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 projects/packages/scheduled-updates/changelog/add-update-manager-infer-status-from-logs create mode 100644 projects/plugins/mu-wpcom-plugin/changelog/add-update-manager-infer-status-from-logs diff --git a/projects/packages/scheduled-updates/changelog/add-update-manager-infer-status-from-logs b/projects/packages/scheduled-updates/changelog/add-update-manager-infer-status-from-logs new file mode 100644 index 0000000000000..d094a43621540 --- /dev/null +++ b/projects/packages/scheduled-updates/changelog/add-update-manager-infer-status-from-logs @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Infer scheduled update status from logs diff --git a/projects/packages/scheduled-updates/composer.json b/projects/packages/scheduled-updates/composer.json index ada4677d92ff4..a0f980c0f6e6f 100644 --- a/projects/packages/scheduled-updates/composer.json +++ b/projects/packages/scheduled-updates/composer.json @@ -49,7 +49,7 @@ }, "autotagger": true, "branch-alias": { - "dev-trunk": "0.6.x-dev" + "dev-trunk": "0.7.x-dev" }, "textdomain": "jetpack-scheduled-updates", "version-constants": { diff --git a/projects/packages/scheduled-updates/src/class-scheduled-updates-logs.php b/projects/packages/scheduled-updates/src/class-scheduled-updates-logs.php index 611b999bca6a9..2fa27cd0a5abd 100644 --- a/projects/packages/scheduled-updates/src/class-scheduled-updates-logs.php +++ b/projects/packages/scheduled-updates/src/class-scheduled-updates-logs.php @@ -154,6 +154,59 @@ public static function clear( string $schedule_id = null ) { update_option( self::OPTION_NAME, $logs ); } + /** + * Infers the status of a plugin update schedule from its logs. + * + * @param string $schedule_id The ID of the plugin update schedule. + * + * @return array|false An array containing the last run timestamp and status, or false if no logs are found. + * The array has the following keys: + * - 'last_run_timestamp': The timestamp of the last run, or null if the status is 'in-progress'. + * - 'last_run_status': The status of the last run, which can be one of the following: + * - 'in-progress': The update is currently in progress. + * - 'success': The update was successful. + * - 'failure': The update failed. + * - 'failure-and-rollback': The update failed and a rollback was performed. + * - 'failure-and-rollback-fail': The update failed and the rollback also failed. + */ + public static function infer_status_from_logs( $schedule_id ) { + $logs = self::get( $schedule_id ); + if ( is_wp_error( $logs ) || empty( $logs ) ) { + return false; + } + + $last_run = end( $logs ); + + $status = 'in-progress'; + $timestamp = time(); + + foreach ( $last_run as $log_entry ) { + $timestamp = $log_entry['timestamp']; + + if ( self::PLUGIN_UPDATES_SUCCESS === $log_entry['action'] ) { + $status = 'success'; + break; + } + if ( self::PLUGIN_UPDATES_FAILURE === $log_entry['action'] ) { + $status = 'failure'; + break; + } + if ( self::PLUGIN_UPDATE_FAILURE_AND_ROLLBACK === $log_entry['action'] ) { + $status = 'failure-and-rollback'; + break; + } + if ( self::PLUGIN_UPDATE_FAILURE_AND_ROLLBACK_FAIL === $log_entry['action'] ) { + $status = 'failure-and-rollback-fail'; + break; + } + } + + return array( + 'last_run_timestamp' => 'in-progress' === $status ? null : $timestamp, + 'last_run_status' => $status, + ); + } + /** * Splits the logs into runs based on the PLUGIN_UPDATES_START action. * diff --git a/projects/packages/scheduled-updates/src/class-scheduled-updates.php b/projects/packages/scheduled-updates/src/class-scheduled-updates.php index 5c3d0e68b5b8f..a5a4d9cae9275 100644 --- a/projects/packages/scheduled-updates/src/class-scheduled-updates.php +++ b/projects/packages/scheduled-updates/src/class-scheduled-updates.php @@ -20,7 +20,7 @@ class Scheduled_Updates { * * @var string */ - const PACKAGE_VERSION = '0.6.0'; + const PACKAGE_VERSION = '0.7.0-alpha'; /** * The cron event hook for the scheduled plugins update. @@ -175,8 +175,12 @@ public static function set_scheduled_update_status( $schedule_id, $timestamp, $s * @return array|null Last status of the scheduled update or null if not found. */ public static function get_scheduled_update_status( $schedule_id ) { - $statuses = get_option( 'jetpack_scheduled_update_statuses', array() ); + $status = Scheduled_Updates_Logs::infer_status_from_logs( $schedule_id ); + if ( false !== $status ) { + return $status; + } + $statuses = get_option( 'jetpack_scheduled_update_statuses', array() ); return $statuses[ $schedule_id ] ?? null; } diff --git a/projects/plugins/mu-wpcom-plugin/changelog/add-update-manager-infer-status-from-logs b/projects/plugins/mu-wpcom-plugin/changelog/add-update-manager-infer-status-from-logs new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/mu-wpcom-plugin/changelog/add-update-manager-infer-status-from-logs @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/mu-wpcom-plugin/composer.json b/projects/plugins/mu-wpcom-plugin/composer.json index 6ad9b2783204f..92eaf566e57df 100644 --- a/projects/plugins/mu-wpcom-plugin/composer.json +++ b/projects/plugins/mu-wpcom-plugin/composer.json @@ -46,6 +46,6 @@ ] }, "config": { - "autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_1_12" + "autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_1_13_alpha" } } diff --git a/projects/plugins/mu-wpcom-plugin/composer.lock b/projects/plugins/mu-wpcom-plugin/composer.lock index b6458de076c5b..294c75eef0255 100644 --- a/projects/plugins/mu-wpcom-plugin/composer.lock +++ b/projects/plugins/mu-wpcom-plugin/composer.lock @@ -198,7 +198,7 @@ "dist": { "type": "path", "url": "../../packages/scheduled-updates", - "reference": "2a989d1848db4a9ad658ecb04e93a7278a905b23" + "reference": "4dbf0c9b0bf8384ef49f5cbd8ab76713a2f41222" }, "require": { "php": ">=7.0" @@ -221,7 +221,7 @@ }, "autotagger": true, "branch-alias": { - "dev-trunk": "0.6.x-dev" + "dev-trunk": "0.7.x-dev" }, "textdomain": "jetpack-scheduled-updates", "version-constants": { diff --git a/projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php b/projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php index ceb3b45c780fd..fd766f7991ff6 100644 --- a/projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php +++ b/projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php @@ -3,7 +3,7 @@ * * Plugin Name: WordPress.com Features * Description: Test plugin for the jetpack-mu-wpcom package - * Version: 2.1.12 + * Version: 2.1.13-alpha * Author: Automattic * License: GPLv2 or later * Text Domain: jetpack-mu-wpcom-plugin diff --git a/projects/plugins/mu-wpcom-plugin/package.json b/projects/plugins/mu-wpcom-plugin/package.json index fb646f72f5ab7..2c0e330fc668e 100644 --- a/projects/plugins/mu-wpcom-plugin/package.json +++ b/projects/plugins/mu-wpcom-plugin/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@automattic/jetpack-mu-wpcom-plugin", - "version": "2.1.12", + "version": "2.1.13-alpha", "description": "Test plugin for the jetpack-mu-wpcom package", "homepage": "https://jetpack.com", "bugs": {