← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-rest-endpoints.php
+129
-13
13.5.2
→
16.3-beta
View file →
| @@ -57,8 +57,13 @@ | ||
| 57 | 57 | 'description' => __( 'Comment IDs to include in Full Sync', 'jetpack-sync' ), |
| 58 | 58 | 'type' => 'array', |
| 59 | 59 | 'required' => false, |
| 60 | 60 | ), |
| 61 | + 'context' => array( | |
| 62 | + 'description' => __( 'Context for the Full Sync', 'jetpack-sync' ), | |
| 63 | + 'type' => 'string', | |
| 64 | + 'required' => false, | |
| 65 | + ), | |
| 61 | 66 | ), |
| 62 | 67 | ) |
| 63 | 68 | ); |
| 64 | 69 | |
| @@ -71,9 +76,9 @@ | ||
| 71 | 76 | 'callback' => __CLASS__ . '::sync_status', |
| 72 | 77 | 'permission_callback' => __CLASS__ . '::verify_default_permissions', |
| 73 | 78 | 'args' => array( |
| 74 | 79 | 'fields' => array( |
| 75 | - 'description' => __( 'Comma seperated list of additional fields that should be included in status.', 'jetpack-sync' ), | |
| 80 | + 'description' => __( 'Comma-separated list of additional fields that should be included in status.', 'jetpack-sync' ), | |
| 76 | 81 | 'type' => 'string', |
| 77 | 82 | 'required' => false, |
| 78 | 83 | ), |
| 79 | 84 | ), |
| @@ -169,8 +174,45 @@ | ||
| 169 | 174 | array( |
| 170 | 175 | 'methods' => WP_REST_Server::EDITABLE, |
| 171 | 176 | 'callback' => __CLASS__ . '::checkout', |
| 172 | 177 | 'permission_callback' => __CLASS__ . '::verify_default_permissions', |
| 178 | + 'args' => array( | |
| 179 | + 'queue' => array( | |
| 180 | + 'description' => __( 'Name of Sync queue.', 'jetpack-sync' ), | |
| 181 | + 'type' => 'string', | |
| 182 | + 'required' => false, | |
| 183 | + ), | |
| 184 | + 'number_of_items' => array( | |
| 185 | + 'description' => __( 'Number of items to checkout from the queue.', 'jetpack-sync' ), | |
| 186 | + 'type' => 'integer', | |
| 187 | + 'required' => false, | |
| 188 | + ), | |
| 189 | + 'pop' => array( | |
| 190 | + 'description' => __( 'Pop items from the queue instead of checking out.', 'jetpack-sync' ), | |
| 191 | + 'type' => 'boolean', | |
| 192 | + 'required' => false, | |
| 193 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 194 | + ), | |
| 195 | + 'force' => array( | |
| 196 | + 'description' => __( 'Force unlock the queue before checkout.', 'jetpack-sync' ), | |
| 197 | + 'type' => 'boolean', | |
| 198 | + 'required' => false, | |
| 199 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 200 | + ), | |
| 201 | + 'encode' => array( | |
| 202 | + 'description' => __( 'Encode the items before sending.', 'jetpack-sync' ), | |
| 203 | + 'type' => 'boolean', | |
| 204 | + 'required' => false, | |
| 205 | + 'default' => true, | |
| 206 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 207 | + ), | |
| 208 | + 'use_memory_limit' => array( | |
| 209 | + 'description' => __( 'Use memory-based checkout instead of fixed item count.', 'jetpack-sync' ), | |
| 210 | + 'type' => 'boolean', | |
| 211 | + 'required' => false, | |
| 212 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 213 | + ), | |
| 214 | + ), | |
| 173 | 215 | ) |
| 174 | 216 | ); |
| 175 | 217 | |
| 176 | 218 | // Checkin Sync Objects. |
| @@ -287,9 +329,9 @@ | ||
| 287 | 329 | 'type' => 'string', |
| 288 | 330 | 'required' => false, |
| 289 | 331 | ), |
| 290 | 332 | 'only_range_edges' => array( |
| 291 | - 'description' => __( 'Should only range endges be returned', 'jetpack-sync' ), | |
| 333 | + 'description' => __( 'Should only range edges be returned', 'jetpack-sync' ), | |
| 292 | 334 | 'type' => 'boolean', |
| 293 | 335 | 'required' => false, |
| 294 | 336 | ), |
| 295 | 337 | 'detailed_drilldown' => array( |
| @@ -326,8 +368,19 @@ | ||
| 326 | 368 | 'callback' => __CLASS__ . '::reset_locks', |
| 327 | 369 | 'permission_callback' => __CLASS__ . '::verify_default_permissions', |
| 328 | 370 | ) |
| 329 | 371 | ); |
| 372 | + | |
| 373 | + // Clear Sync queue. | |
| 374 | + register_rest_route( | |
| 375 | + 'jetpack/v4', | |
| 376 | + '/sync/clear-queue', | |
| 377 | + array( | |
| 378 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 379 | + 'callback' => __CLASS__ . '::clear_queue', | |
| 380 | + 'permission_callback' => __CLASS__ . '::verify_default_permissions', | |
| 381 | + ) | |
| 382 | + ); | |
| 330 | 383 | } |
| 331 | 384 | |
| 332 | 385 | /** |
| 333 | 386 | * Trigger a Full Sync of specified modules. |
| @@ -362,11 +415,13 @@ | ||
| 362 | 415 | if ( empty( $modules ) ) { |
| 363 | 416 | $modules = null; |
| 364 | 417 | } |
| 365 | 418 | |
| 419 | + $context = $request->get_param( 'context' ); | |
| 420 | + | |
| 366 | 421 | return rest_ensure_response( |
| 367 | 422 | array( |
| 368 | - 'scheduled' => Actions::do_full_sync( $modules ), | |
| 423 | + 'scheduled' => Actions::do_full_sync( $modules, $context ), | |
| 369 | 424 | ) |
| 370 | 425 | ); |
| 371 | 426 | } |
| 372 | 427 | |
| @@ -462,23 +517,55 @@ | ||
| 462 | 517 | |
| 463 | 518 | /** |
| 464 | 519 | * Update Sync health. |
| 465 | 520 | * |
| 521 | + * IN_SYNC is only set if the incremental queue is within size and lag limits. | |
| 522 | + * | |
| 466 | 523 | * @since 1.23.1 |
| 467 | 524 | * |
| 468 | 525 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
| 469 | 526 | * |
| 470 | - * @return \WP_REST_Response | |
| 527 | + * @return \WP_REST_Response|WP_Error | |
| 471 | 528 | */ |
| 472 | 529 | public static function sync_health( $request ) { |
| 530 | + $requested_status = $request->get_param( 'status' ); | |
| 473 | 531 | |
| 474 | - switch ( $request->get_param( 'status' ) ) { | |
| 532 | + switch ( $requested_status ) { | |
| 475 | 533 | case Health::STATUS_IN_SYNC: |
| 534 | + // Only allow setting IN_SYNC if the incremental queue is healthy. | |
| 535 | + $sync_queue = Listener::get_instance()->get_sync_queue(); | |
| 536 | + $queue_size = $sync_queue->size(); | |
| 537 | + $queue_lag = $sync_queue->lag(); | |
| 538 | + $queue_healthy = Health::is_queue_healthy( | |
| 539 | + $queue_size, | |
| 540 | + $queue_lag, | |
| 541 | + Settings::get_setting( 'max_queue_size' ), | |
| 542 | + Settings::get_setting( 'max_queue_lag' ) | |
| 543 | + ); | |
| 544 | + if ( ! $queue_healthy ) { | |
| 545 | + Health::update_status( Health::STATUS_OUT_OF_SYNC ); | |
| 546 | + return rest_ensure_response( | |
| 547 | + array( | |
| 548 | + 'success' => Health::get_status(), | |
| 549 | + 'message' => 'Sync queue is not healthy (size and lag over limit). Status not set to in_sync.', | |
| 550 | + 'queue_size' => $queue_size, | |
| 551 | + 'queue_lag' => $queue_lag, | |
| 552 | + ) | |
| 553 | + ); | |
| 554 | + } | |
| 555 | + Health::update_status( $requested_status ); | |
| 556 | + break; | |
| 476 | 557 | case Health::STATUS_OUT_OF_SYNC: |
| 477 | - Health::update_status( $request->get_param( 'status' ) ); | |
| 558 | + Health::update_status( $requested_status ); | |
| 478 | 559 | break; |
| 479 | 560 | default: |
| 480 | - return new WP_Error( 'invalid_status', 'Invalid Sync Status Provided.' ); | |
| 561 | + return new WP_Error( | |
| 562 | + 'invalid_status', | |
| 563 | + 'Invalid Sync Status Provided.', | |
| 564 | + array( | |
| 565 | + 'status' => 400, | |
| 566 | + ) | |
| 567 | + ); | |
| 481 | 568 | } |
| 482 | 569 | |
| 483 | 570 | // re-fetch so we see what's really being stored. |
| 484 | 571 | return rest_ensure_response( |
| @@ -609,21 +696,29 @@ | ||
| 609 | 696 | if ( is_wp_error( $queue_name ) ) { |
| 610 | 697 | return $queue_name; |
| 611 | 698 | } |
| 612 | 699 | |
| 613 | - $number_of_items = $args['number_of_items']; | |
| 614 | - if ( $number_of_items < 1 || $number_of_items > 100 ) { | |
| 615 | - return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 ); | |
| 700 | + $use_memory_limit = ! empty( $args['use_memory_limit'] ); | |
| 701 | + | |
| 702 | + if ( $use_memory_limit && ! empty( $args['pop'] ) ) { | |
| 703 | + return new WP_Error( 'invalid_args', 'pop cannot be used with use_memory_limit', 400 ); | |
| 616 | 704 | } |
| 617 | 705 | |
| 706 | + if ( ! $use_memory_limit ) { | |
| 707 | + if ( empty( $args['number_of_items'] ) || $args['number_of_items'] < 1 || $args['number_of_items'] > 100 ) { | |
| 708 | + return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and up to 100', 400 ); | |
| 709 | + } | |
| 710 | + } | |
| 711 | + | |
| 618 | 712 | // REST Sender. |
| 619 | 713 | $sender = new REST_Sender(); |
| 620 | 714 | |
| 621 | 715 | if ( 'immediate' === $queue_name ) { |
| 622 | - return rest_ensure_response( $sender->immediate_full_sync_pull( $number_of_items ) ); | |
| 716 | + return rest_ensure_response( $sender->immediate_full_sync_pull() ); | |
| 623 | 717 | } |
| 624 | 718 | |
| 625 | - $response = $sender->queue_pull( $queue_name, $number_of_items, $args ); | |
| 719 | + $number_of_items = $use_memory_limit ? null : $args['number_of_items']; | |
| 720 | + $response = $sender->queue_pull( $queue_name, $number_of_items, $args ); | |
| 626 | 721 | // Disable sending while pulling. |
| 627 | 722 | if ( ! is_wp_error( $response ) ) { |
| 628 | 723 | set_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME, time(), Sender::TEMP_SYNC_DISABLE_TRANSIENT_EXPIRY ); |
| 629 | 724 | } elseif ( 'queue_size' === $response->get_error_code() ) { |
| @@ -763,9 +858,9 @@ | ||
| 763 | 858 | * |
| 764 | 859 | * @see Actions::init |
| 765 | 860 | * @see Sender::do_dedicated_sync_and_exit |
| 766 | 861 | * |
| 767 | - * @since $$next_version$$ | |
| 862 | + * @since 1.34.0 | |
| 768 | 863 | * |
| 769 | 864 | * @return \WP_REST_Response |
| 770 | 865 | */ |
| 771 | 866 | public static function spawn_sync() { |
| @@ -794,8 +889,29 @@ | ||
| 794 | 889 | * @return \WP_REST_Response |
| 795 | 890 | */ |
| 796 | 891 | public static function reset_locks() { |
| 797 | 892 | Actions::reset_sync_locks(); |
| 893 | + | |
| 894 | + return rest_ensure_response( | |
| 895 | + array( | |
| 896 | + 'success' => true, | |
| 897 | + ) | |
| 898 | + ); | |
| 899 | + } | |
| 900 | + | |
| 901 | + /** | |
| 902 | + * Clear the Sync queue. | |
| 903 | + * | |
| 904 | + * @since 4.30.0 | |
| 905 | + * | |
| 906 | + * @return \WP_REST_Response | |
| 907 | + */ | |
| 908 | + public static function clear_queue() { | |
| 909 | + $queue = new Queue( 'sync' ); | |
| 910 | + $queue->reset(); | |
| 911 | + | |
| 912 | + // Re-enable sending in case it was temporarily disabled during a pull. | |
| 913 | + delete_transient( Sender::TEMP_SYNC_DISABLE_TRANSIENT_NAME ); | |
| 798 | 914 | |
| 799 | 915 | return rest_ensure_response( |
| 800 | 916 | array( |
| 801 | 917 | 'success' => true, |