Skip to content

Implement schedule monitor scheduler #11599

@benbowler

Description

@benbowler

Feature Description

  • Schedule a daily event using wp_schedule_event with daily recurrence
  • Check if the feature is enabled. If so, check for missing frequency initiator events. If a missing event is found, then recreate it.

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

Acceptance criteria

  • A daily scheduled event automatically recreates any missing initiator schedules for weekly, monthly, and quarterly email reports using the existing schedule helpers added in Implement “Initiator” scheduled events #11546
  • Monitor runs only when Email Reports is enabled; when disabled, or the plugin is reset or uninstalled, the scheduled event should be removed.
  • Only missing schedules are restored; existing ones are left untouched.

Implementation Brief

  • Extend Core\Email_Reporting\Email_Reporting_Scheduler
    • Define a new action constant, e.g. const ACTION_MONITOR = 'googlesitekit_email_reporting_monitor'.
    • Add a helper schedule_monitor() that checks wp_next_scheduled( self::ACTION_MONITOR ) and, if missing, registers a recurring event with wp_schedule_event( time(), 'daily', self::ACTION_MONITOR ).
    • Update unschedule_all() to invoke wp_unschedule_hook( self::ACTION_MONITOR ) so the daily event is removed when the feature is disabled.
    • Ensure ACTION_MONITOR is added to Uninstallation::SCHEDULED_EVENTS.
  • Create Core\Email_Reporting\Monitor_Task class
    • It should accept Email_Reporting_Scheduler via its constructor.
    • Implement handle_monitor_action()
      • It should bail immediately when Email_Reporting_Settings::is_email_reporting_enabled() returns false.
    • For each supported frequency (WEEKLY, MONTHLY, QUARTERLY), call wp_next_scheduled( Email_Reporting_Scheduler::ACTION_INITIATOR, array( $frequency ) ).
      • If it returns false, invoke $scheduler->schedule_initiator_once( $frequency ) (the method already checks for if the CRON is scheduled, so no need to do anything else besides invoking it)
  • Update Google\Site_Kit\Core\Email_Reporting\Email_Reporting::register()
    • Within existing feature toggle listener - when the feature is enabled, call $scheduler->schedule_monitor() in addition to the initial initiators.
    • Instantiate Monitor_Task, and hook into Email_Reporting_Scheduler::ACTION_MONITOR invoking 'handle_monitor_action' callback of Core\Email_Reporting\Monitor_Task

Test Coverage

  • Add basic test coverage for Monitor_Task

QA Brief

  • Enable the feature flag and install a plugin that lets you see WP scheduled events (like WP Crontrol).
  • Toggle the feature in the admin settings, when the feature is enabled you should see a daily registered event: googlesitekit_email_reporting_monitor

Changelog entry

  • Implement monitor scheduler for Email Reporting events.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0High priorityPHPTeam SIssues for Squad 1Type: EnhancementImprovement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions