Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Periodic Server Requirements Health Checks Using WP-Cron Job #9768

Closed
3 tasks done
hussain-t opened this issue Nov 26, 2024 · 8 comments
Closed
3 tasks done
Labels
P0 High priority Team M Issues for Squad 2 Type: Enhancement Improvement of an existing feature

Comments

@hussain-t
Copy link
Collaborator

hussain-t commented Nov 26, 2024

Feature Description

Introduce periodic server requirement health checks for First-party mode to ensure it remains functional and responsive to any changes in server configurations. This will proactively detect and handle any issues that might impact the feature’s functionality.

The health checks will validate server configurations and update the First-party mode settings accordingly. If the checks fail, the feature will automatically revert to the default setup.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • A periodic WP-Cron job should be implemented to perform server requirement health checks for First-party mode every hour.
  • The health checks should include:
    • FPFE Health Check: Validate connectivity to the FPFE /healthy endpoint (e.g., https://G-1234.fps.goog/mpath/healthy).
    • Direct PHP Script Access Check: Verify access to the proxy script (fpm/measurement.php) via an HTTP request with the healthCheck query parameter.
    • The cron job should update the googlesitekit_first_party_mode settings based on the health check results:
      • If either the FPFE health check or script access check fails:
        • Set isFPMHealthy or isScriptAccessEnabled to false in the settings.
        • Automatically disable First-party mode, reverting to the default configuration.
      • If both checks pass:
        • Ensure isFPMHealthy and isScriptAccessEnabled are set to true.

Implementation Brief

  • Create a new file includes/Core/Tags/First_Party_Mode/First_Party_Mode_Cron.php modelled on includes/Core/Remote_Features/Remote_Features_Cron.php:

    • The CRON_ACTION const should be googlesitekit_cron_first_party_mode_healthchecks.
    • The schedule interval should be time() + HOUR_IN_SECONDS.
  • Update includes/Core/Tags/First_Party_Mode/First_Party_Mode.php:

    • Create a new method healthcheck which:
      • Runs the same health checks as the fpm-server-requirement-status REST endpoint:
        $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 ) ) );
      • Update all First-Party Mode settings using $this->first_party_mode_settings->merge.
    • Create a new private class value which accepts First_Party_Mode_Cron called $cron.
    • Update __construct, set $this->cron to a new instance of First_Party_Mode_Cron passing array( $this, 'healthcheck' ).
    • Create a new method on_admin_init modelled on Remote_Features_Provider.
    • Update register, adding an action to call $this->on_admin_init() on admin_init.
  • Update includes/Core/Util/Uninstallation.php to clear the First_Party_Mode_Cron::CRON_ACTION.

Test Coverage

  • Create new test coverage for the First_Party_Mode_Cron class.

QA Brief

  1. Install Advanced Cron Manager plugin and activate.

  2. Activate Site Kit and make sure that either Analytics or the Ads module is active and connected. Run the following command.

  googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

the output would be (considering that the test site is a fresh install) like:

    {
        "isEnabled": null,
        "isFPMHealthy": null,
        "isScriptAccessEnabled": null
    }
  1. Visit the Site Kit dashboard. Now, go to Tools > Cron Manager. In the list of cron jobs, googlesitekit_cron_first_party_mode_healthchecks should be available.

  2. Click Execute now for the job and let it complete, there would be a message Event "googlesitekit_cron_first_party_mode_healthchecks" has been executed once the job is completed.

  3. Refresh the Site Kit dashboard and run following command.

  googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

the output should be like

    {
        "isEnabled": null,
        "isFPMHealthy": true,
        "isScriptAccessEnabled": true
    }
  1. Now, run following command to enable FPM
  googlesitekit.data.dispatch('core/site').setFirstPartyModeEnabled( true );
  googlesitekit.data.dispatch('core/site').saveFirstPartyModeSettings();

Run,

  googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

the output should now be

    {
        "isEnabled": true,
        "isFPMHealthy": true,
        "isScriptAccessEnabled": true
    }
  1. Update the fpm/measurement.php file. The following code should be update from
  echo 'ok';
  exit;

to

  echo 'NOT ok';
  exit;

save the file on server.

  1. Refresh the Site Kit dashboard, run the cron again manually from Advanced cron manager screen as stated previously.

  2. Refresh the Site Kit dashboard and run following command.

  googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

the output should be like

    {
        "isEnabled": true,
        "isFPMHealthy": true,
        "isScriptAccessEnabled": false
    }

Changelog entry

  • Add periodic server requirement health checks for first-party mode.
@hussain-t hussain-t added P0 High priority Type: Enhancement Improvement of an existing feature Team M Issues for Squad 2 labels Nov 26, 2024
@ivonac4 ivonac4 added the Next Up Issues to prioritize for definition label Nov 27, 2024
@hussain-t hussain-t assigned hussain-t and unassigned hussain-t Dec 2, 2024
@techanvil techanvil self-assigned this Dec 2, 2024
@techanvil
Copy link
Collaborator

AC ✅

@techanvil techanvil removed their assignment Dec 2, 2024
@benbowler benbowler assigned benbowler and unassigned benbowler Dec 3, 2024
@nfmohit nfmohit self-assigned this Dec 3, 2024
@nfmohit
Copy link
Collaborator

nfmohit commented Dec 3, 2024

Nice work on the IB, thank you @benbowler!

  • Update includes/Core/Util/Uninstallation.php to clear the Conversion_Reporting_Cron::CRON_ACTION.

I believe you meant First_Party_Mode_Cron::CRON_ACTION, so I've updated that accordingly. Please let me know if that's incorrect.

IB ✅

@nfmohit nfmohit removed their assignment Dec 3, 2024
@ankitrox ankitrox self-assigned this Dec 3, 2024
@ankitrox ankitrox removed their assignment Dec 3, 2024
@eugene-manuilov eugene-manuilov removed the Next Up Issues to prioritize for definition label Dec 3, 2024
@nfmohit nfmohit self-assigned this Dec 3, 2024
@hussain-t
Copy link
Collaborator Author

Hi @benbowler, @nfmohit, regarding this point:

If either setting is false, set isEnabled to false.

We should keep the isEnabled to remain true while setting the other properties (isFPMHealthy or isScriptAccessEnabled) to false if their respective checks fail. This is because we want to retain the information that First-party mode was enabled but has been interrupted and rolled back due to server configuration failures.


Create a new method healthcheck which:
Runs the same health checks as the fpm-server-requirement-status REST endpoint:

An implementation detail, but it's worth noting that these checks can be extracted into a reusable method.

cc: @techanvil

@nfmohit
Copy link
Collaborator

nfmohit commented Dec 4, 2024

Thank you @hussain-t !

In that case, I'll remove the part about changing isEnabled in the IB.

CC: @ankitrox since you're working on this in Execution.

@nfmohit nfmohit assigned ankitrox and unassigned nfmohit Dec 5, 2024
@ankitrox ankitrox assigned nfmohit and unassigned ankitrox Dec 5, 2024
@nfmohit nfmohit removed their assignment Dec 6, 2024
@kelvinballoo kelvinballoo self-assigned this Dec 8, 2024
@kelvinballoo
Copy link
Collaborator

QA Update ⚠

Hi @ankitrox , few observations/queries:

ITEM 1:
First sound I executed googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings(); on a fresh install, I was expecting the following based on the QAB:

    {
        "isEnabled": null,
        "isFPMHealthy": null,
        "isScriptAccessEnabled": null
    }

That said, I got it as null, true, true as per the screenshot attached below.
Is there any concern here?

Image

ITEM 2:

For the part of the QAB where we are to update the fpm/measurement.php file, could you elaborate on the steps of where to amend that.
I am using instawp and I have access to the DB editor but there are so many tables and thus, I am unsure where to amend it.

@ankitrox
Copy link
Collaborator

ankitrox commented Dec 9, 2024

@kelvinballoo Thanks for checking this.

ITEM 1:

Actually, this behaviour is expected because when we setup the fresh site, there is no cron job scheduled. Thus a job is scheduled and run instantly which changes these flags in settings.

ITEM 2:

This is a simple change in wp-content/plugins/google-site-kit/fpm/measurement.php file. If you are not sure about how to do that, we can ask anyone from engineering to test this point.

Please let me know if you have any further queries.

Thank you.

@ankitrox ankitrox removed their assignment Dec 9, 2024
@kelvinballoo
Copy link
Collaborator

QA update ⚠

Thanks @ankitrox , noted on this. I was able to verify the fpm measurement file in instawp after you gave the address.

One item to verify:
On the last step:
After we have updated the file, executed the cron job, and executed the getFirstPartyModeSettings() script,
I saw the attached uncaught error.
Does that warrant any fixes.

Image

________________________________________________________

Other than that, the following were verified good, executed as per QAB:

  • Activated Site Kit and made sure that either Analytics or the Ads module is active and connected.
    After running the command:
    googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

    The output is null, true, true and is as expected per latest check with Ankit.

    Image

  • Under Tools > Cron Manager, googlesitekit_cron_first_party_mode_healthchecks is available.
    After executing the job and let it complete, the message "googlesitekit_cron_first_party_mode_healthchecks" appears.
    It executes hourly.

    Image

    9768.-.03.Cron.has.been.executed.mov
  • After refreshing the Site Kit dashboard and running the following command:
    googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();
    The result is null, true, true.

    Image

  • Now, after running following commands to enable FPM:
    googlesitekit.data.dispatch('core/site').setFirstPartyModeEnabled( true );
    googlesitekit.data.dispatch('core/site').saveFirstPartyModeSettings();
    The results become true, true, true.

    9768.-.05.-.updating.settings.mov
  • After updating the fpm measurement file to 'NOT ok', executing the CRON and refreshing dashboard, the results are true, true, False

    9768.-.06.-.Fpm.measurement.file.update.mov

@kelvinballoo
Copy link
Collaborator

QA update ✅

Confirmed with @ankitrox that the highlighted error is only because of Advanced cron manager, nothing related to Site Kit. As such, I will proceed with to Approval.

The following were verified good, executed as per QAB:

  • Activated Site Kit and made sure that either Analytics or the Ads module is active and connected.
    After running the command:
    googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();

    The output is null, true, true and is as expected per latest check with Ankit.

    Image

  • Under Tools > Cron Manager, googlesitekit_cron_first_party_mode_healthchecks is available.
    After executing the job and let it complete, the message "googlesitekit_cron_first_party_mode_healthchecks" appears.
    It executes hourly.

    Image

    9768.-.03.Cron.has.been.executed.mov
  • After refreshing the Site Kit dashboard and running the following command:
    googlesitekit.data.select( 'core/site' ).getFirstPartyModeSettings();
    The result is null, true, true.

    Image

  • Now, after running following commands to enable FPM:
    googlesitekit.data.dispatch('core/site').setFirstPartyModeEnabled( true );
    googlesitekit.data.dispatch('core/site').saveFirstPartyModeSettings();
    The results become true, true, true.

    9768.-.05.-.updating.settings.mov
  • After updating the fpm measurement file to 'NOT ok', executing the CRON and refreshing dashboard, the results are true, true, False

    9768.-.06.-.Fpm.measurement.file.update.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P0 High priority Team M Issues for Squad 2 Type: Enhancement Improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

9 participants