Skip to content

Commit 8a70cfb

Browse files
committed
add queue validate to keep queue up to date.
1 parent 637eaf8 commit 8a70cfb

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-sync-queue.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,20 @@ public function get_queue_status() {
233233
}
234234

235235
/**
236-
* Build the upload sync queue.
236+
* Validate the queue is up to date and populate with unsynced assets.
237237
*/
238-
public function build_queue() {
238+
public function validate_queue() {
239239

240+
$queue = $this->get_queue();
241+
if ( ! empty( $queue['processing'] ) ) {
242+
foreach ( $queue['processing'] as $attachment_id ) {
243+
if ( $this->plugin->get_component( 'sync' )->is_synced( $attachment_id ) ) {
244+
$this->mark( $attachment_id, 'done' );
245+
}
246+
}
247+
// Get queue to get new version with marked processing.
248+
$queue = $this->get_queue();
249+
}
240250
$args = array(
241251
'post_type' => 'attachment',
242252
'post_mime_type' => array( 'image', 'video' ),
@@ -260,6 +270,28 @@ public function build_queue() {
260270

261271
$attachments = new \WP_Query( $args );
262272
$ids = $attachments->get_posts();
273+
// Reset Threads.
274+
foreach ( $this->threads as $thread ) {
275+
$queue[ $thread ] = array();
276+
$queue['run_status'][ $thread ] = array();
277+
}
278+
// Add items to pending queue.
279+
if ( ! empty( $ids ) ) {
280+
$chunk_size = ceil( count( $ids ) / count( $this->threads ) );
281+
$chunks = array_chunk( $ids, $chunk_size );
282+
foreach ( $chunks as $index => $chunk ) {
283+
$queue[ $this->threads[ $index ] ] = $chunk;
284+
}
285+
}
286+
287+
$this->set_queue( $queue );
288+
}
289+
290+
/**
291+
* Build the upload sync queue.
292+
*/
293+
public function build_queue() {
294+
263295
// Transform attachments.
264296
$return = array(
265297
'done' => array(),
@@ -272,16 +304,8 @@ public function build_queue() {
272304
$return['run_status'][ $thread ] = array();
273305
}
274306

275-
// Add items to pending queue.
276-
if ( ! empty( $ids ) ) {
277-
$chunk_size = ceil( count( $ids ) / count( $this->threads ) );
278-
$chunks = array_chunk( $ids, $chunk_size );
279-
foreach ( $chunks as $index => $chunk ) {
280-
$return[ $this->threads[ $index ] ] = $chunk;
281-
}
282-
}
283-
284307
$this->set_queue( $return );
308+
$this->validate_queue();
285309

286310
return $return;
287311
}

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/sync-media-footer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
if ( ! $this->plugin->components['sync']->managers['queue']->is_running() ) {
1010
// Rebuild on load.
1111
$this->plugin->components['sync']->managers['queue']->build_queue();
12+
} else {
13+
// Check and confirm queue is in fact valid.
14+
$this->plugin->components['sync']->managers['queue']->validate_queue();
1215
}
1316

17+
1418
?>
1519
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Changes', 'cloudinary' ); ?></button>
16-
<hr />
20+
<hr/>
1721
<div class="sync settings-tab-section">
1822
<h2><?php esc_html_e( 'Bulk-Sync WordPress Media with Cloudinary (optional)', 'cloudinary' ); ?></h2>
1923

@@ -29,7 +33,7 @@
2933
</button>
3034
<span id="progress-wrapper" class="sync-media-progress">
3135
<span class="progress-text">
32-
<img src="<?php echo esc_url( CLDN_URL . 'css/loading.svg'); ?>" alt="<?php esc_attr_e( 'Syncing…', 'cloudinary' ); ?>">
36+
<img src="<?php echo esc_url( CLDN_URL . 'css/loading.svg' ); ?>" alt="<?php esc_attr_e( 'Syncing…', 'cloudinary' ); ?>">
3337
<?php esc_html_e( 'Syncing…', 'cloudinary' ); ?>
3438
</span>
3539
</span>

0 commit comments

Comments
 (0)