Skip to content

Commit 855f290

Browse files
authored
Merge pull request #1004 from cloudinary/feature/cleanup-cron
Cleanup Cloudinary cron on deactivation and full cleanup
2 parents e171306 + 2b29fa6 commit 855f290

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

php/class-cron.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,12 @@ public function cleanup_failed_cron() {
448448
$this->locker->delete_lock_file( $this->init_time );
449449
}
450450

451+
/**
452+
* Allow cleanup of the locker.
453+
*
454+
* @return void
455+
*/
456+
public function cleanup_locker() {
457+
$this->locker->delete_lock_file();
458+
}
451459
}

php/class-deactivation.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ protected function cleanup() {
479479
$this->cleanup_post_type();
480480
$this->drop_tables();
481481
$this->cleanup_options();
482+
$this->cleanup_cron();
483+
$this->cleanup_legacy_cron();
482484

483485
// If we got this far, let's remove the cron event.
484486
wp_clear_scheduled_hook( 'cloudinary_cleanup_event' );
@@ -600,4 +602,51 @@ protected function cleanup_options() {
600602
delete_transient( $key );
601603
}
602604
}
605+
606+
/**
607+
* Remove all cron-related tasks.
608+
*
609+
* @return void
610+
*/
611+
protected function cleanup_cron() {
612+
// Get the Cron instance.
613+
$cron_instance = Cron::get_instance();
614+
$schedule = $cron_instance->get_schedule();
615+
616+
// Unregister all registered schedules.
617+
if ( ! empty( $schedule ) ) {
618+
foreach ( array_keys( $schedule ) as $schedule_name ) {
619+
$cron_instance->unregister_schedule( $schedule_name );
620+
}
621+
}
622+
623+
// Remove any lock files or objects used by the Locker instance.
624+
$cron_instance->cleanup_locker();
625+
626+
// Delete the cron schedule option saved in database.
627+
delete_option( Cron::CRON_META_KEY );
628+
}
629+
630+
/**
631+
* Cleanup legacy cron jobs.
632+
*
633+
* @return void
634+
*/
635+
protected function cleanup_legacy_cron() {
636+
wp_clear_scheduled_hook( 'cloudinary_status' );
637+
638+
$jobs = array(
639+
'cloudinary_rest_api_connectivity',
640+
'cloudinary_resume_queue',
641+
'cloudinary_resume_upgrade',
642+
'cloudinary_sync_items',
643+
);
644+
645+
foreach ( $jobs as $job ) {
646+
$time = wp_next_scheduled( $job );
647+
if ( false !== $time ) {
648+
wp_clear_scheduled_hook( $job );
649+
}
650+
}
651+
}
603652
}

0 commit comments

Comments
 (0)