v0.3.1 — self-healing cron scheduling (#8)
Adds PRV_Cron_Guard hooked on init to re-schedule managed cron hooks when missing after rsync deploys. Delegates to PRV_Cron::schedule() and PRV_Prune_Cron::schedule(). Idempotent. Two wp_next_scheduled() reads per request; no DB writes unless event missing.
This commit is contained in:
69
includes/core/class-prv-cron-guard.php
Normal file
69
includes/core/class-prv-cron-guard.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Self-healing cron guard: re-schedules managed cron events when missing.
|
||||
*
|
||||
* @package PrVision
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Idempotent guard that ensures all managed WP-Cron events are scheduled.
|
||||
*
|
||||
* WordPress fires the plugin activator only on activation, not on rsync
|
||||
* file-sync updates (our deploy method). This guard runs on `init` and
|
||||
* re-schedules any missing cron event by delegating to the canonical
|
||||
* scheduler method for that hook -- so recurrence and first-run offset are
|
||||
* always consistent with the activator.
|
||||
*
|
||||
* Managed hooks:
|
||||
* - `prv_weekly_probe` -- scheduled via PRV_Cron::schedule()
|
||||
* - `prv_daily_prune` -- scheduled via PRV_Prune_Cron::schedule()
|
||||
*
|
||||
* Hot-path cost: two `wp_next_scheduled()` checks per request. No DB
|
||||
* writes unless an event is actually missing.
|
||||
*
|
||||
* Who triggers: init action (registered in PRV_Plugin::init()).
|
||||
* Dependencies: PRV_Cron, PRV_Prune_Cron.
|
||||
*
|
||||
* @see class-prv-cron.php -- Canonical weekly probe scheduler.
|
||||
* @see class-prv-prune-cron.php -- Canonical daily prune scheduler.
|
||||
* @see class-prv-activator.php -- Original schedule call on activation.
|
||||
* @package PrVision
|
||||
*/
|
||||
class PRV_Cron_Guard {
|
||||
|
||||
/**
|
||||
* Register the guard on the `init` hook.
|
||||
*
|
||||
* Side effects: Adds action for `init`.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks(): void {
|
||||
add_action( 'init', array( $this, 'ensure_scheduled' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure every managed cron hook is scheduled.
|
||||
*
|
||||
* For each managed hook, checks `wp_next_scheduled()`. If the event is
|
||||
* missing, delegates to the canonical scheduler (same method the
|
||||
* activator calls) so recurrence and offset are never duplicated here.
|
||||
* Calling this when all events are already scheduled is a pure no-op.
|
||||
*
|
||||
* Side effects: May call PRV_Cron::schedule() or
|
||||
* PRV_Prune_Cron::schedule() when an event is missing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function ensure_scheduled(): void {
|
||||
if ( ! wp_next_scheduled( PRV_CRON_HOOK ) ) {
|
||||
PRV_Cron::schedule();
|
||||
}
|
||||
|
||||
if ( ! wp_next_scheduled( PRV_Prune_Cron::PRUNE_HOOK ) ) {
|
||||
PRV_Prune_Cron::schedule();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,13 +17,15 @@ declare(strict_types=1);
|
||||
* Who triggers: plugins_loaded action (pr-vision.php).
|
||||
* Dependencies: PRV_Upgrader, PRV_Cron, PRV_Admin_Page, PRV_Settings_Page,
|
||||
* PRV_Settings_Controller, PRV_Costs_Page, PRV_Call_Log_Page,
|
||||
* PRV_Call_Detail_Ajax, PRV_Prune_Cron, PRV_Collector_Registry.
|
||||
* PRV_Call_Detail_Ajax, PRV_Prune_Cron, PRV_Cron_Guard,
|
||||
* PRV_Collector_Registry.
|
||||
*
|
||||
* @see ARCHITECTURE.md -- Boot sequence diagram.
|
||||
* @see class-prv-upgrader.php -- Runs migrations on every boot.
|
||||
* @see class-prv-costs-page.php -- [v0.3.0] Costs admin sub-page.
|
||||
* @see class-prv-call-log-page.php -- [v0.3.0] Call Log admin sub-page.
|
||||
* @see class-prv-prune-cron.php -- [v0.3.0] Daily prune cron.
|
||||
* @see class-prv-cron-guard.php -- [v0.3.1] Self-healing cron guard.
|
||||
* @package PrVision
|
||||
*/
|
||||
class PRV_Plugin {
|
||||
@@ -50,6 +52,9 @@ class PRV_Plugin {
|
||||
$prune = new PRV_Prune_Cron();
|
||||
$prune->register_hooks();
|
||||
|
||||
$guard = new PRV_Cron_Guard();
|
||||
$guard->register_hooks();
|
||||
|
||||
if ( is_admin() ) {
|
||||
$page = new PRV_Admin_Page();
|
||||
$page->register_hooks();
|
||||
|
||||
Reference in New Issue
Block a user