120, 'display' => __( 'Every 2 Minutes', 'ai-product-tools' ), ); } if ( ! isset( $schedules['aipt_five_minutes'] ) ) { $schedules['aipt_five_minutes'] = array( 'interval' => 300, 'display' => __( 'Every 5 Minutes', 'ai-product-tools' ), ); } return $schedules; } /** * Prevent the could_not_set noise from no-op reschedule races (@since 2.6.2). * * Hooks into pre_reschedule_event. When WP core's wp_cron() auto-reschedules * one of our recurring hooks and the next scheduled time computed by core * lands on the exact timestamp that already holds an entry, the subsequent * update_option('cron', ...) call returns false (no-op detection) and WP * core emits a could_not_set WP_Error that wp_cron() writes to error_log. * That noise is what surfaces in customer site monitors as "the plugin * caused this error". See https://core.trac.wordpress.org/ticket/57271. * * Returns `true` (success short-circuit) ONLY when both apply: * - The hook is the recurring automation processor (foreign hooks * including action_scheduler_run_queue are intentionally not in scope). * - wp_next_scheduled() reports a collision within 1s of the timestamp * WP core would target ($event->timestamp + $event->interval). The 1s * tolerance covers clock skew between concurrent wp-cron.php hits. * * All other cases return null so WP core's normal reschedule path runs. * * @param null|bool|\WP_Error $pre Prior filter result. If non-null, passthrough. * @param object $event Event object (hook, timestamp, interval, args, schedule). * @return null|bool|\WP_Error `true` to short-circuit, null to let WP proceed. */ public function preventNoopRescheduleRace( $pre, $event ) { if ( null !== $pre ) { return $pre; } if ( ! is_object( $event ) || empty( $event->hook ) || empty( $event->interval ) ) { return null; } if ( ! $this->isOwnedCronHook( (string) $event->hook ) ) { return null; } $args = isset( $event->args ) && is_array( $event->args ) ? $event->args : array(); $now = (int) time(); $timestamp = (int) $event->timestamp; $interval = (int) $event->interval; if ( $timestamp >= $now ) { $target_ts = $now + $interval; } else { $target_ts = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); } $existing = wp_next_scheduled( $event->hook, $args ); if ( false === $existing ) { return null; } if ( abs( (int) $existing - $target_ts ) <= 1 ) { return true; } return null; } /** * Check whether the given hook is one of ours subject to the reschedule guard. * * The recurring hooks in CronRegistry::GUARDED_HOOKS (the automation * processor). The single-event job run hook (JobRunHook::HOOK) is NOT in scope because * pre_reschedule_event only fires for recurring events; their duplicate-scheduling race is fixed via * wp_next_scheduled guards inside JobScheduler::processPendingJobs. * * @since 2.6.2 * * @param string $hook Hook name. * @return bool True if this filter is authorized to intervene. */ private function isOwnedCronHook( string $hook ): bool { return in_array( $hook, CronRegistry::GUARDED_HOOKS, true ); } /** * Process automation jobs cron callback. * * One tick does both jobs of the automation cron: queue the jobs that are * ready, then watch the running ones (release stale claims, give a stalled * job a new tick). The monitor used to be a recurring event of its own that * woke every store every two minutes; every threshold it checks * (STALE_CLAIM_MINUTES 15, JOB_TIMEOUT 30 minutes) is a multiple of this * five-minute tick. Each part has its own catch, so a failure in one never * skips the other. */ public function processAutomationJobs(): void { $scheduler = $this->automationScheduler(); if ( null === $scheduler ) { return; } try { $scheduler->processPendingJobs(); } catch ( \Throwable $e ) { Logger::error( 'cron', 'Automation job processing failed', array( 'error' => $e->getMessage() ) ); } try { $scheduler->monitorRunningJobs(); } catch ( \Throwable $e ) { Logger::error( 'cron', 'Automation job monitoring failed', array( 'error' => $e->getMessage() ) ); } } /** * Build the job scheduler the automation tick uses. * * @return JobScheduler|null Null when the automation classes are missing or cannot be built; the failure is logged. */ private function automationScheduler(): ?JobScheduler { try { if ( ! class_exists( '\AIPT\Api\AutomationJobs\AutomationJobManager' ) ) { Logger::error( 'cron', 'Automation job manager class not found', array( 'hook' => 'aipt_process_automation_jobs' ) ); return null; } return new JobScheduler( new AutomationJobManager(), new JobLogger() ); } catch ( \Throwable $e ) { Logger::error( 'cron', 'Automation job processing failed', array( 'error' => $e->getMessage() ) ); return null; } } /** * Setup automation jobs cron schedules */ public function setupAutomationSchedules(): void { try { if ( ! AutomationCronDemand::isWanted() ) { return; } JobScheduler::setupCronSchedules(); } catch ( \Throwable $e ) { Logger::error( 'cron', 'Automation schedule setup failed', array( 'error' => $e->getMessage() ) ); } } }