Skip to content

Commit

Permalink
Scheduled updates: Infer status from logs (#36752)
Browse files Browse the repository at this point in the history
* Infer status from logs

* Project versions

* Project versions

* Project versions

* Fix type for phan
  • Loading branch information
TimBroddin authored Apr 5, 2024
1 parent 0d85a4a commit 3e8e42c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Infer scheduled update status from logs
2 changes: 1 addition & 1 deletion projects/packages/scheduled-updates/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions projects/plugins/mu-wpcom-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 3e8e42c

Please sign in to comment.