-
Notifications
You must be signed in to change notification settings - Fork 58
feat(integrations): contact cron for push and pull #4608
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
Open
miguelpeixe
wants to merge
14
commits into
trunk
Choose a base branch
from
feat/integrations-contact-cron
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d7ec7d1
feat(integrations): contact cron for push and pull
miguelpeixe 23fc5da
feat: introduce contact pull retries
miguelpeixe ab68986
chore: remove Sync extension
miguelpeixe 5d03ba9
fix: simplify cron run control
miguelpeixe 068d4c6
fix: per-user throttling
miguelpeixe 9bb150b
fix: simplify sync pull method and remove retry
miguelpeixe a0bf7bc
chore: remove deprecated const
miguelpeixe 7fb56e0
chore: update tests
miguelpeixe 6630a11
feat: simplify queue handling to rely on retries
miguelpeixe a776191
chore: remove unused import
miguelpeixe 578167d
fix: clarify synchronous pull logging for current user
miguelpeixe 8ce6978
fix: address PR review feedback
miguelpeixe d80df92
fix: skip no-field integrations in pull_all, clear queues in test setUp
miguelpeixe 9d0b690
Merge branch 'trunk' into feat/integrations-contact-cron
miguelpeixe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
includes/reader-activation/integrations/class-contact-cron.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| <?php | ||
| /** | ||
| * Contact Cron orchestration class | ||
| * | ||
| * Handles recurring pull and push of contact data via WP-Cron. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack\Reader_Activation\Integrations; | ||
|
|
||
| use Newspack\Reader_Activation\Contact_Sync; | ||
| use Newspack\Logger; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * Contact Cron Class. | ||
| * | ||
| * Manages recurring contact data synchronization: queues users for | ||
| * pull (from integrations) and push (to integrations), processes | ||
| * them in batch via WP-Cron. | ||
| */ | ||
| class Contact_Cron { | ||
| /** | ||
| * Cron interval in seconds (5 minutes). | ||
| * | ||
| * @var int | ||
| */ | ||
| const CRON_INTERVAL = 300; | ||
|
|
||
| /** | ||
| * User meta key for last enqueue timestamp. | ||
| * | ||
| * @var string | ||
| */ | ||
| const LAST_ENQUEUE_META = 'newspack_contact_cron_last_enqueue'; | ||
|
|
||
| /** | ||
| * WP-Cron hook for batch processing. | ||
| * | ||
| * @var string | ||
| */ | ||
| const CRON_HOOK = 'newspack_contact_cron_batch'; | ||
|
|
||
| /** | ||
| * WP option key for the pull queue. | ||
| * | ||
| * @var string | ||
| */ | ||
| const PULL_QUEUE_OPTION = 'newspack_pull_contact_data_queue'; | ||
|
|
||
| /** | ||
| * WP option key for the push queue. | ||
| * | ||
| * @var string | ||
| */ | ||
| const PUSH_QUEUE_OPTION = 'newspack_push_contact_data_queue'; | ||
|
|
||
| /** | ||
| * WP-Cron schedule name. | ||
| * | ||
| * @var string | ||
| */ | ||
| const CRON_SCHEDULE = 'newspack_contact_cron_interval'; | ||
|
|
||
| /** | ||
| * Logger header for Contact Cron messages. | ||
| * | ||
| * @var string | ||
| */ | ||
| const LOGGER_HEADER = 'NEWSPACK-CONTACT-CRON'; | ||
|
|
||
| /** | ||
| * Initialize hooks. | ||
| */ | ||
| public static function init() { | ||
| add_filter( 'cron_schedules', [ __CLASS__, 'add_cron_schedule' ] ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected | ||
| add_action( 'init', [ __CLASS__, 'maybe_enqueue_contact' ], 20 ); | ||
| add_action( 'init', [ __CLASS__, 'schedule_cron' ] ); | ||
| add_action( self::CRON_HOOK, [ __CLASS__, 'handle_batch' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Register custom cron schedule. | ||
| * | ||
| * @param array $schedules Existing cron schedules. | ||
| * @return array Modified schedules. | ||
| */ | ||
| public static function add_cron_schedule( $schedules ) { | ||
| $schedules[ self::CRON_SCHEDULE ] = [ | ||
| 'interval' => self::CRON_INTERVAL, | ||
| 'display' => __( 'Newspack Contact Cron Interval', 'newspack-plugin' ), | ||
| ]; | ||
| return $schedules; | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue contact data for pull and push for the current logged-in user. | ||
| * | ||
| * If the last pull is stale (> 24 h), the pull runs synchronously. | ||
| */ | ||
| public static function maybe_enqueue_contact() { | ||
| if ( ! is_user_logged_in() ) { | ||
| return; | ||
| } | ||
|
|
||
| $user_id = get_current_user_id(); | ||
| $last_enqueue = (int) get_user_meta( $user_id, self::LAST_ENQUEUE_META, true ); | ||
|
|
||
| if ( ( time() - $last_enqueue ) < self::CRON_INTERVAL ) { | ||
| return; | ||
| } | ||
| update_user_meta( $user_id, self::LAST_ENQUEUE_META, time() ); | ||
|
|
||
| self::enqueue_for_push( $user_id ); | ||
|
|
||
| if ( Contact_Pull::is_stale( $last_enqueue ) ) { | ||
| $result = Contact_Pull::pull_sync(); | ||
| if ( is_wp_error( $result ) ) { | ||
| self::enqueue_for_pull( $user_id ); | ||
| } | ||
| } else { | ||
| self::enqueue_for_pull( $user_id ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Add a user to the pull queue. | ||
| * | ||
| * @param int $user_id WordPress user ID. | ||
| */ | ||
| public static function enqueue_for_pull( $user_id ) { | ||
| self::enqueue( self::PULL_QUEUE_OPTION, $user_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Add a user to the push queue. | ||
| * | ||
| * @param int $user_id WordPress user ID. | ||
| */ | ||
| public static function enqueue_for_push( $user_id ) { | ||
| self::enqueue( self::PUSH_QUEUE_OPTION, $user_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Add a user ID to a queue option. | ||
| * | ||
| * @param string $option The option key. | ||
| * @param int $user_id WordPress user ID. | ||
| */ | ||
| private static function enqueue( $option, $user_id ) { | ||
| $queue = get_option( $option, [] ); | ||
| if ( ! in_array( $user_id, $queue, true ) ) { | ||
| $queue[] = $user_id; | ||
| update_option( $option, $queue, false ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensure the recurring cron event is scheduled. | ||
| * | ||
| * Respects NEWSPACK_CRON_DISABLE to allow selective disabling. | ||
| */ | ||
| public static function schedule_cron() { | ||
| register_deactivation_hook( NEWSPACK_PLUGIN_FILE, [ __CLASS__, 'deactivate_cron' ] ); | ||
|
|
||
| if ( defined( 'NEWSPACK_CRON_DISABLE' ) && is_array( NEWSPACK_CRON_DISABLE ) && in_array( self::CRON_HOOK, NEWSPACK_CRON_DISABLE, true ) ) { | ||
| self::deactivate_cron(); | ||
| } elseif ( ! wp_next_scheduled( self::CRON_HOOK ) ) { | ||
| wp_schedule_event( time(), self::CRON_SCHEDULE, self::CRON_HOOK ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deactivate the cron event. | ||
| */ | ||
| public static function deactivate_cron() { | ||
| wp_clear_scheduled_hook( self::CRON_HOOK ); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the recurring cron event. | ||
| * | ||
| * Processes both pull and push queues. | ||
| */ | ||
| public static function handle_batch() { | ||
| self::handle_batch_pull(); | ||
| self::handle_batch_push(); | ||
| } | ||
|
|
||
| /** | ||
| * Process the pull queue. | ||
| * | ||
| * Reads the queue, pulls all active integrations for each user, | ||
| * and removes successfully processed users. | ||
| */ | ||
| private static function handle_batch_pull() { | ||
| $queue = get_option( self::PULL_QUEUE_OPTION, [] ); | ||
| if ( empty( $queue ) ) { | ||
| return; | ||
| } | ||
| delete_option( self::PULL_QUEUE_OPTION ); | ||
|
|
||
| Logger::log( 'Batch pull started for ' . count( $queue ) . ' user(s).', self::LOGGER_HEADER ); | ||
|
|
||
| foreach ( $queue as $user_id ) { | ||
| if ( ! get_userdata( $user_id ) ) { | ||
| Logger::log( 'Batch pull skipping non-existent user ' . $user_id . '.', self::LOGGER_HEADER ); | ||
| continue; | ||
| } | ||
| if ( Contact_Pull::has_pending_retries( $user_id ) ) { | ||
| Logger::log( 'Batch pull skipping user ' . $user_id . ': pending pull retries.', self::LOGGER_HEADER ); | ||
| continue; | ||
| } | ||
miguelpeixe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $result = Contact_Pull::pull_all( $user_id ); | ||
| if ( is_wp_error( $result ) ) { | ||
| Logger::error( 'Batch pull failed for user ' . $user_id . ': ' . $result->get_error_message(), self::LOGGER_HEADER ); | ||
| } | ||
miguelpeixe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Logger::log( 'Batch pull completed.', self::LOGGER_HEADER ); | ||
| } | ||
|
|
||
| /** | ||
| * Process the push queue. | ||
| * | ||
| * Reads the queue, fetches fresh contact data for each user, | ||
| * and pushes to all active integrations. | ||
| */ | ||
| private static function handle_batch_push() { | ||
| $queue = get_option( self::PUSH_QUEUE_OPTION, [] ); | ||
| if ( empty( $queue ) ) { | ||
| return; | ||
| } | ||
| delete_option( self::PUSH_QUEUE_OPTION ); | ||
|
|
||
| Logger::log( 'Batch push started for ' . count( $queue ) . ' user(s).', self::LOGGER_HEADER ); | ||
|
|
||
| foreach ( $queue as $user_id ) { | ||
| if ( ! get_userdata( $user_id ) ) { | ||
| Logger::log( 'Batch push skipping non-existent user ' . $user_id . '.', self::LOGGER_HEADER ); | ||
| continue; | ||
| } | ||
| if ( Contact_Sync::has_pending_retries( $user_id ) ) { | ||
| Logger::log( 'Batch push skipping user ' . $user_id . ': pending sync retries.', self::LOGGER_HEADER ); | ||
| continue; | ||
| } | ||
| $result = Contact_Sync::sync_contact( $user_id, 'Recurring sync routine' ); | ||
| if ( is_wp_error( $result ) ) { | ||
| Logger::error( 'Batch push failed for user ' . $user_id . ': ' . $result->get_error_message(), self::LOGGER_HEADER ); | ||
| } | ||
| } | ||
|
|
||
miguelpeixe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Logger::log( 'Batch push completed.', self::LOGGER_HEADER ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.