wp-background-processing icon indicating copy to clipboard operation
wp-background-processing copied to clipboard

cancel_process not working

Open countfak opened this issue 6 years ago • 6 comments

Hi,

cancel_process doesn't work as expected: it never stops processing queue items.

Am I missing something?

countfak avatar Oct 02 '18 17:10 countfak

I am running into the same issue, wondering if I have to save the batch key and somehow use that to delete the queue. I suspect it's because I am triggering the queue via ajax calls.

oddpixel avatar Oct 22 '18 17:10 oddpixel

I believe the problem is that although cancel_process deletes the current batch from the database and cancels the hook, if another process is simultaneously working on the batch then when it finishes it writes the batch into the database again and call dispatch, which starts the hook up again. I haven't looked into how to fix the code, but if like me you just need to kill a large batch running amok in the background, try this: shut down your server, delete the correct 'wp_your_process_name_batch_%' entry from the wp_options table, and restart the server.

jjd314 avatar Jan 01 '19 17:01 jjd314

I'm having the same problem. I want to be able to manually stop the background process. I've created this function but it does not working.

function me_stop_moving_files(){
    global $wpdb;
    $sql = "SELECT `option_name` AS `name`, `option_value` AS `value`
            FROM  $wpdb->options
            WHERE `option_name` LIKE %s
            ORDER BY `option_name`";


    $wild = '%';
    $find = 'wp_example_process';
    $like = $wild . $wpdb->esc_like( $find ) . $wild;
    $results = $wpdb->get_results( $wpdb->prepare($sql,$like) );

    foreach ( $results as $result ){
        delete_option($result->name);
    }

    $this->process_all->cancel_process();
    wp_clear_scheduled_hook('wp_example_process_cron');

    die('completed');
}

Did you have any success ?

crerem avatar May 02 '19 12:05 crerem

Did you have any success?

Hi, no, I'm sorry, I never managed to get this to work.

countfak avatar May 02 '19 13:05 countfak

I found a way, I've added a piece of code in WP_Background_Process, inside the protected function handle() (Inside the do ...while) : if ( get_option('stop_import') ) { $this->cancel_process(); } Of course when you want to stop the process you just need to set the option. So it looks like that:

do 
{
	if ( get_option('stop_import') )
	{
		$this->cancel_process();
	}

	$batch = $this->get_batch();

	foreach ( $batch->data as $key => $value )
	{
		$task = $this->task( $value );

		if ( false !== $task )
		{
			$batch->data[ $key ] = $task;
		} 
		else
		{
			unset( $batch->data[ $key ] );
		}

		if ( $this->time_exceeded() || $this->memory_exceeded() )
		{
			// Batch limits reached.
			break;
		}
	}

	// Update or delete current batch.
	if ( ! empty( $batch->data ) )
	{
		$this->update( $batch->key, $batch->data );
	}
	else
	{
		$this->delete( $batch->key );
	}
} 
while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() )

I hope it will help someone =)

Edit: it still finishes to execute the current batch before cancelling itself.

Gobi-one avatar Aug 30 '19 15:08 Gobi-one

I can also add that you can stop the iteration in the foreach by adding some horrible code: `foreach ( $batch->data as $key => $value ) {

if ( file_get_contents ( '/test.txt' ) )
{
	$this->cancel_process();
	break;
}
    ...

` OFC you need to write 1 or 0 in your file when you want to stop (here test.txt). But as you can see, it implies reading the content of the file each and every time... I really dislike that awful solution, but without adding special libraries to PHP, you can't have a global variable accessible in that piece of code (cause it's in another thread ??). I'm just going to use the other solution, that exec all the batch before stopping.

Gobi-one avatar Aug 31 '19 16:08 Gobi-one

We have some improvements coming to this library, I believe one of them will effectively fix this issue by introducing a better method of cancelling a background process.

We'll see if there is a way to improve the cancel_process() function at the same time, which has a slightly different use case to what we'll be adding.

ianmjones avatar Apr 11 '23 19:04 ianmjones