From 349530321864fdf719be191e7ba1954964f07369 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 2 Apr 2020 15:36:02 +0200 Subject: [PATCH 1/3] allow retries on down sync, don't delete downsync errors. --- .../php/class-sync.php | 1 + .../php/media/class-upgrade.php | 18 +++++++++++++++++- .../php/sync/class-download-sync.php | 15 +++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php index a2bcd3745..6d740dcd7 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php @@ -48,6 +48,7 @@ class Sync implements Setup, Assets { 'transformation' => '_transformations', 'sync_error' => '_sync_error', 'cloudinary' => '_cloudinary_v2', + 'attempts' => '_sync_attempts', ); /** diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php index f4568904f..f64db8661 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php @@ -55,6 +55,20 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) { */ if ( ! empty( $meta['cloudinary'] ) && empty( $public_id ) ) { $cloudinary_id = $this->convert_cloudinary_version( $attachment_id ); + } elseif ( ! empty( $meta['cloudinary'] ) ) { + // Has public ID, but still has cloudinary, check pending status. + $is_pending = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); + $attempts = (int) $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['attempts'], true ); + $expire = time() - 5;// * 60; + if ( ( empty( $is_pending ) || $is_pending < $expire ) && 10 >= $attempts ) { + // Timeout. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['attempts'], $attempts + 1 ); + + return $this->convert_cloudinary_version( $attachment_id ); + } + $cloudinary_id = $public_id; + } else { + $cloudinary_id = $public_id; } } @@ -105,6 +119,8 @@ function ( $val ) use ( $media ) { $public_id = strstr( $public_id, '.' . $path['extension'], true ); // Save public ID. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + // Set download started data. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['pending'], time() ); // Setup a call for a background sync. $params = array( @@ -125,7 +141,7 @@ public function setup_hooks() { add_filter( 'cloudinary_id', array( $this, 'check_cloudinary_version' ), 9, 2 ); // Priority 9, to take preference over prep_on_demand_upload. // Add a redirection to the new plugin settings, from the old plugin. - if( is_admin() ) { + if ( is_admin() ) { add_action( 'admin_menu', function () { global $plugin_page; if ( ! empty( $plugin_page ) && false !== strpos( $plugin_page, 'cloudinary-image-management-and-manipulation-in-the-cloud-cdn' ) ) { diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index cda52be93..85f44884a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -7,6 +7,8 @@ namespace Cloudinary\Sync; +use Cloudinary\Sync; + /** * Class Download_Sync. * @@ -88,9 +90,15 @@ public function rest_can_upload_files( \WP_REST_Request $request ) { */ public function handle_failed_download( $attachment_id, $error ) { // @todo: Place a handler to catch the error for logging. - // Delete attachment temp. - wp_delete_attachment( $attachment_id, true ); + $is_pending = $this->plugin->components['media']->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); + if ( ! empty( $is_pending ) ) { + // Dont delete if it's a downsync. + delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); + } else { + // Delete attachment temp. + wp_delete_attachment( $attachment_id, true ); + } // Send error. wp_send_json_error( $error ); } @@ -181,6 +189,9 @@ public function rest_download_asset( \WP_REST_Request $request ) { 'data' => $attachment, ); + // Remove pending. + delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); + return rest_ensure_response( $response ); } } From fc6c099b559b59689c9711748aec5ae087ccc568 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 2 Apr 2020 15:48:05 +0200 Subject: [PATCH 2/3] set error and remove on success --- .../php/media/class-upgrade.php | 4 ++-- .../php/sync/class-download-sync.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php index f64db8661..7621769eb 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php @@ -59,11 +59,11 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) { // Has public ID, but still has cloudinary, check pending status. $is_pending = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); $attempts = (int) $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['attempts'], true ); - $expire = time() - 5;// * 60; - if ( ( empty( $is_pending ) || $is_pending < $expire ) && 10 >= $attempts ) { + if ( ( empty( $is_pending ) || $is_pending < time() - 5 * 60 ) && 10 > $attempts ) { // Timeout. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['attempts'], $attempts + 1 ); + // return proposed ID to allow front render. return $this->convert_cloudinary_version( $attachment_id ); } $cloudinary_id = $public_id; diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index 85f44884a..f46980910 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -94,7 +94,7 @@ public function handle_failed_download( $attachment_id, $error ) { $is_pending = $this->plugin->components['media']->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); if ( ! empty( $is_pending ) ) { // Dont delete if it's a downsync. - delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); + $this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], __( 'Could not download asset', 'cloudinary' ) ); } else { // Delete attachment temp. wp_delete_attachment( $attachment_id, true ); @@ -191,6 +191,7 @@ public function rest_download_asset( \WP_REST_Request $request ) { // Remove pending. delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); + delete_post_meta( $attachment_id, Sync::META_KEYS['sync_error'] ); return rest_ensure_response( $response ); } From e22329bfffa8b577e339babf2337c8ee6fbcae41 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 2 Apr 2020 15:50:27 +0200 Subject: [PATCH 3/3] use existing error --- .../php/sync/class-download-sync.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index f46980910..92de295ba 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -94,7 +94,7 @@ public function handle_failed_download( $attachment_id, $error ) { $is_pending = $this->plugin->components['media']->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); if ( ! empty( $is_pending ) ) { // Dont delete if it's a downsync. - $this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], __( 'Could not download asset', 'cloudinary' ) ); + $this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], $error ); } else { // Delete attachment temp. wp_delete_attachment( $attachment_id, true );