wp-background-processing
wp-background-processing copied to clipboard
cancel_process not working
Hi,
cancel_process
doesn't work as expected: it never stops processing queue items.
Am I missing something?
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.
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.
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 ?
Did you have any success?
Hi, no, I'm sorry, I never managed to get this to work.
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.
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.
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.