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

Not getting any logs and options table keeps storing records indefinitely at high speed

Open franmadj opened this issue 5 years ago • 3 comments

in /classes/class-background-process.php

require_once get_template_directory() . '/vendor/autoload.php';
class BackgroundProcess extends WP_Background_Process {
    protected $action = 'test';
    protected function task($item) {
        error_log($item);
        sleep(5);
        return false;
    }
    protected function complete() {
        parent::complete();
    }
}

In theme functions.php

add_action('init', 'BackgroundProcess');
function BackgroundProcess() {
    require_once get_template_directory() . '/classes/class-background-process.php';
    $background_process = new BackgroundProcess();
// Add items to the queue.
    $counter = 0;
    do {
        $counter ++;
        $background_process->push_to_queue($counter);
    } while ($counter < 10);

// Start the queue.
    $background_process->save()->dispatch();
}

franmadj avatar Nov 02 '19 19:11 franmadj

I'm also having this issue. cant seem to get the task to run. Was working fine for years up until very recently without any changes to my code. I've tried clearing the options from the database and also calling $background_process->cancel_process(); but nothing happens.. Really frustrating.. Its just stuck running ->handle_cron_healthcheck() and not processing.. Any ideas?

filtah avatar Jun 26 '20 10:06 filtah

ok, so I've been debugging and noticed that even with removing the jobs from the options table, the system thinks there is an active job in the queue:

Background process already running and it kills the proccess..

There is a transient that it checks to determine this. Manually running delete_site_transient( $this->identifier . 'process_lock' ); clears the transient, it's not easy to find the transient in the database. the identifier string seems to follow the pattern 'wp' . action_name . '_process_lock'

hopefully this will help someone else :)

filtah avatar Jun 26 '20 10:06 filtah

@filtah Thank you for notifying this. I'm also user of this plugin and found the fixes of above problem by modifying few lines in handle function of wp-background-process

// Update or delete current batch.
  if ( ! empty( $batch->data ) && count($batch->data)>0) {
    if(is_array($batch->data)) {
        $firstItem = reset($batch->data);
      if(!empty($firstItem)){
	   $this->update( $batch->key, $batch->data );
	} else {
           $this->delete( $batch->key );
	 }
     } else {
         $this->delete( $batch->key );
     }
 } else {
    $this->delete( $batch->key );
  }

kumarmanishc avatar May 31 '21 15:05 kumarmanishc

Closing ancient issue. :smile:

ianmjones avatar Apr 11 '23 18:04 ianmjones