Skip to content

Commit

Permalink
Return error messages from FPM healthchecks.
Browse files Browse the repository at this point in the history
  • Loading branch information
techanvil committed Dec 18, 2024
1 parent d66760a commit ecaa08a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
21 changes: 20 additions & 1 deletion assets/js/googlesitekit/datastore/site/first-party-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ const fetchGetFPMServerRequirementStatusStore = createFetchStore( {
API.get( 'core', 'site', 'fpm-server-requirement-status', undefined, {
useCache: false,
} ),
reducerCallback: settingsReducerCallback,
reducerCallback: createReducer(
( state, { settings: firstPartyModeSettings, healthcheck } ) => {
state.firstPartyModeSettings = firstPartyModeSettings;
state.firstPartyModeSavedSettings = firstPartyModeSettings;
state.healthcheck = healthcheck;
}
),
} );

const baseInitialState = {
firstPartyModeSettings: undefined,
firstPartyModeSavedSettings: undefined,
healthcheck: undefined,
};

const baseActions = {
Expand Down Expand Up @@ -190,6 +197,18 @@ const baseSelectors = {
return state.firstPartyModeSettings;
},

/**
* Gets the first-party mode healthcheck.
*
* @since n.e.x.t
*
* @param {Object} state Data store's state.
* @return {Object|undefined} First-party mode healthcheck, or undefined if not loaded.
*/
getFirstPartyModeHealthcheck: ( state ) => {
return state.healthcheck;
},

/**
* Checks if first-party mode is enabled.
*
Expand Down
56 changes: 48 additions & 8 deletions includes/Core/Tags/First_Party_Mode/First_Party_Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,23 @@ public function get_debug_fields() {
*
* @since 1.142.0
*
* @return void
* @return array
*/
public function healthcheck() {
$is_fpm_healthy = $this->is_endpoint_healthy( 'https://g-1234.fps.goog/mpath/healthy' );
$is_script_access_enabled = $this->is_endpoint_healthy( add_query_arg( 'healthCheck', '1', plugins_url( 'fpm/measurement.php', GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) );

$this->first_party_mode_settings->merge(
array(
'isFPMHealthy' => $is_fpm_healthy,
'isScriptAccessEnabled' => $is_script_access_enabled,
'isFPMHealthy' => $is_fpm_healthy['success'],
'isScriptAccessEnabled' => $is_script_access_enabled['success'],
)
);

return array(
'isFPMHealthy' => $is_fpm_healthy,
'isScriptAccessEnabled' => $is_script_access_enabled,
);
}

/**
Expand All @@ -188,20 +193,55 @@ public function on_admin_init() {
* @since 1.142.0 Relocated from REST_First_Party_Mode_Controller.
*
* @param string $endpoint The endpoint to check.
* @return bool True if the endpoint is healthy, false otherwise.
* @return array{success: bool, message: string} Success is true if the endpoint is healthy, false otherwise. Message is the error message if the endpoint is not healthy.
*/
protected function is_endpoint_healthy( $endpoint ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
set_error_handler(
function ( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, $severity, $severity, $file, $line );
}
);

try {
// phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown,WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$response = file_get_contents( $endpoint );
} catch ( \Exception $e ) {
return false;
} catch ( \ErrorException $e ) {
return array(
'success' => false,
'message' => $this->trim_string( $e->getMessage() ),
);
} finally {
restore_error_handler();
}

if ( 'ok' !== $response ) {
return false;
return array(
'success' => false,
'message' => $this->trim_string( $response ),
);
}

return strpos( $http_response_header[0], '200 OK' ) !== false;
return strpos( $http_response_header[0], '200 OK' ) === false
? array(
'success' => false,
'message' => $this->trim_string( $http_response_header[0] ),
)
: array(
'success' => true,
);
}

/**
* Trims a string to a maximum length.
*
* @since n.e.x.t
*
* @param string $message The message to trim.
* @param int|null $length Optional. The maximum length. Default is 1024.
* @return string
*/
private function trim_string( $message, $length = 1024 ) {
return strlen( $message ) > $length ? substr( $message, 0, $length ) . '...' : $message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ protected function get_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function () {
$this->first_party_mode->healthcheck();
return new WP_REST_Response( $this->first_party_mode_settings->get() );
$healthcheck = $this->first_party_mode->healthcheck();

return new WP_REST_Response(
array(
'settings' => $this->first_party_mode_settings->get(),
'healthcheck' => $healthcheck,
)
);
},
'permission_callback' => $can_manage_options,
),
Expand Down

0 comments on commit ecaa08a

Please sign in to comment.