deliciousbrains / wp-background-processing

WordPress background processing class
GNU General Public License v2.0
1.06k stars 162 forks source link

background processing is not working with plugin activation #63

Closed shoroar closed 1 year ago

shoroar commented 5 years ago

Hello, I am using plugin boilerplate of DevinVinson/WordPress-Plugin-Boilerplate. I want to use the background processing when the plugin is activated. my code is

class Plugin_Name_Activator {

 private $background_process;
public function __construct() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-background-processing.php';
    $this->background_process = new Plugin_Name_Background_Processing();
   }

public function activate() {

    $names = array(
                'Grant Buel',
                'Bryon Pennywell',
                'Jarred Mccuiston',
            );

            foreach ( $names as $name ) {
                $this->background_process->push_to_queue( $name );
            }

            $this->background_process->save()->dispatch();

}

the WP table options have a new row:

wp_background_process_batch_1f8a9447c3ecec7570a4de37af

But

Plugin_Name_Background_Processing() task($item) is never called. What is the problem????

when When I instantiate the class on 'plugins_loaded' action, it works just fine.

I want to work the background processing when activate the plugin, not with 'plugins_loaded' action

rafailsar commented 4 years ago

did you get it working ?

OlafEichstaedt commented 4 years ago

Could the problem be that there is no background process to work on the queue once WP has finished the request to activate the plugin? If I am not mistaken, you have to instantiate the background process on every WP load (i.e. that is why it works when you instantiate with 'plugins_loaded').

dispatch() will 'only' launch a new asynchronous request to WP. To serve this request, the server will launch a new instance of WP that can be used to do the work on the queue. If I am not mistaken, this new instance will not run the code to activate the plugin, so with your design, there is no background process to do the processing.

If you instantiate the background process on 'plugins_loaded', each WP instance has the background process and the processing can start. I believe this is actually intended behavior since the background process may die or time out and processing may only resume once WP_cron spins up a new instance of WP.

ianmjones commented 1 year ago

Closing ancient issue. :smile:

(duplicate of #21)