Skip to content

Commit

Permalink
Add new method and transient to correctly cancel a process (#1)
Browse files Browse the repository at this point in the history
* add new method and transient to correctly cancel a process

* remove copied comment
  • Loading branch information
remyperona authored Oct 3, 2018
1 parent 1aead29 commit 9356c01
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions classes/wp-background-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ protected function is_process_running() {
return false;
}

/**
* Is process cancelled
*
* Check whether the current process is cancelled
* in a background process.
*/
protected function is_process_cancelled() {
if ( get_site_transient( $this->identifier . '_process_cancelled' ) ) {
return true;
}

return false;
}

/**
* Lock process
*
Expand Down Expand Up @@ -313,7 +327,7 @@ protected function handle() {
}

// Update or delete current batch.
if ( ! empty( $batch->data ) ) {
if ( ! empty( $batch->data ) && ! $this->is_process_cancelled() ) {
$this->update( $batch->key, $batch->data );
} else {
$this->delete( $batch->key );
Expand Down Expand Up @@ -401,6 +415,7 @@ protected function time_exceeded() {
protected function complete() {
// Unschedule the cron healthcheck.
$this->clear_scheduled_event();
delete_site_transient( $this->identifier . '_process_cancelled' );
}

/**
Expand Down Expand Up @@ -480,8 +495,10 @@ public function cancel_process() {
$batch = $this->get_batch();

$this->delete( $batch->key );

$this->unlock_process();
wp_clear_scheduled_hook( $this->cron_hook_identifier );

set_site_transient( $this->identifier . '_process_cancelled', 1 );
}

}
Expand Down

0 comments on commit 9356c01

Please sign in to comment.