← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-sender.php
+20
-13
13.5.2
→
16.3-beta
View file →
| @@ -225,9 +225,9 @@ | ||
| 225 | 225 | ! is_wp_error( $verified_user ) |
| 226 | 226 | && $verified_user |
| 227 | 227 | ) { |
| 228 | 228 | $old_user = wp_get_current_user(); |
| 229 | - $this->old_user = isset( $old_user->ID ) ? $old_user->ID : 0; | |
| 229 | + $this->old_user = $old_user->ID ?? 0; | |
| 230 | 230 | wp_set_current_user( $verified_user['user_id'] ); |
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | |
| @@ -357,9 +357,12 @@ | ||
| 357 | 357 | * |
| 358 | 358 | * @return boolean|WP_Error True if this sync sending was successful, error object otherwise. |
| 359 | 359 | */ |
| 360 | 360 | public function do_sync() { |
| 361 | - if ( ! Settings::is_dedicated_sync_enabled() ) { | |
| 361 | + // Sync directly during cron. We are doing this because otherwise | |
| 362 | + // the dedicated sync flow would be spawning HTTP requests during cron shutdown, | |
| 363 | + // which can be unreliable and cause sync lag for time-sensitive events like updates. | |
| 364 | + if ( ! Settings::is_dedicated_sync_enabled() || Settings::is_doing_cron() ) { | |
| 362 | 365 | $result = $this->do_sync_and_set_delays( $this->sync_queue ); |
| 363 | 366 | } else { |
| 364 | 367 | $result = Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 365 | 368 | } |
| @@ -417,14 +420,14 @@ | ||
| 417 | 420 | if ( session_status() === PHP_SESSION_ACTIVE ) { |
| 418 | 421 | session_write_close(); |
| 419 | 422 | } |
| 420 | 423 | |
| 424 | + // Actually try to send Sync events. | |
| 425 | + $result = $this->do_sync_and_set_delays( $this->sync_queue ); | |
| 426 | + | |
| 421 | 427 | // Output not used right now. Try to release dedicated sync lock |
| 422 | 428 | Dedicated_Sender::try_release_lock_spawn_request(); |
| 423 | 429 | |
| 424 | - // Actually try to send Sync events. | |
| 425 | - $result = $this->do_sync_and_set_delays( $this->sync_queue ); | |
| 426 | - | |
| 427 | 430 | // If no errors occurred, re-spawn a dedicated Sync request. |
| 428 | 431 | if ( true === $result ) { |
| 429 | 432 | Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 430 | 433 | } |
| @@ -429,9 +432,9 @@ | ||
| 429 | 432 | Dedicated_Sender::spawn_sync( $this->sync_queue ); |
| 430 | 433 | } |
| 431 | 434 | |
| 432 | 435 | if ( $do_real_exit ) { |
| 433 | - exit; | |
| 436 | + exit( 0 ); | |
| 434 | 437 | } |
| 435 | 438 | } |
| 436 | 439 | |
| 437 | 440 | /** |
| @@ -496,10 +499,12 @@ | ||
| 496 | 499 | } |
| 497 | 500 | if ( 'wpcom_error' === $sync_result->get_error_code() ) { |
| 498 | 501 | $this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id ); |
| 499 | 502 | } |
| 500 | - } elseif ( $exceeded_sync_wait_threshold ) { | |
| 501 | - // If we actually sent data and it took a while, wait before sending again. | |
| 503 | + } elseif ( $exceeded_sync_wait_threshold && ! Settings::is_doing_cron() ) { | |
| 504 | + // If a send was slow, briefly pause before the next one. | |
| 505 | + // Applies only to Dedicated/Normal Sync to avoid impacting user traffic; | |
| 506 | + // cron jobs are exempt. | |
| 502 | 507 | $this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id ); |
| 503 | 508 | } |
| 504 | 509 | |
| 505 | 510 | return $sync_result; |
| @@ -579,9 +584,9 @@ | ||
| 579 | 584 | * |
| 580 | 585 | * @access private |
| 581 | 586 | */ |
| 582 | 587 | private function fastcgi_finish_request() { |
| 583 | - if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) { | |
| 588 | + if ( function_exists( 'fastcgi_finish_request' ) ) { | |
| 584 | 589 | fastcgi_finish_request(); |
| 585 | 590 | } |
| 586 | 591 | } |
| 587 | 592 | |
| @@ -703,18 +708,19 @@ | ||
| 703 | 708 | * Immediately sends a single item without firing or enqueuing it |
| 704 | 709 | * |
| 705 | 710 | * @param string $action_name The action. |
| 706 | 711 | * @param array $data The data associated with the action. |
| 712 | + * @param string $key The key to use for the action. | |
| 707 | 713 | * |
| 708 | 714 | * @return array Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. |
| 709 | 715 | */ |
| 710 | - public function send_action( $action_name, $data = null ) { | |
| 716 | + public function send_action( $action_name, $data = null, $key = null ) { | |
| 711 | 717 | if ( ! Settings::is_sender_enabled( 'full_sync' ) ) { |
| 712 | 718 | return array(); |
| 713 | 719 | } |
| 714 | 720 | |
| 715 | 721 | // Compose the data to be sent. |
| 716 | - $action_to_send = $this->create_action_to_send( $action_name, $data ); | |
| 722 | + $action_to_send = $this->create_action_to_send( $action_name, $data, $key ); | |
| 717 | 723 | |
| 718 | 724 | list( $items_to_send, $skipped_items_ids, $items, $preprocess_duration ) = $this->get_items_to_send( $action_to_send, true ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 719 | 725 | Settings::set_is_sending( true ); |
| 720 | 726 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->get_codec()->name(), microtime( true ), 'immediate-send', 0, $preprocess_duration ); |
| @@ -740,13 +746,14 @@ | ||
| 740 | 746 | * @access private |
| 741 | 747 | * |
| 742 | 748 | * @param string $action_name The action. |
| 743 | 749 | * @param array $data The data associated with the action. |
| 750 | + * @param string $key The key to use for the action. | |
| 744 | 751 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
| 745 | 752 | */ |
| 746 | - private function create_action_to_send( $action_name, $data ) { | |
| 753 | + private function create_action_to_send( $action_name, $data, $key = null ) { | |
| 747 | 754 | return array( |
| 748 | - (string) microtime( true ) => array( | |
| 755 | + $key ?? (string) microtime( true ) => array( | |
| 749 | 756 | $action_name, |
| 750 | 757 | $data, |
| 751 | 758 | get_current_user_id(), |
| 752 | 759 | microtime( true ), |