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

wp-background-process do not dispatch

Open erdguez opened this issue 7 years ago • 18 comments

Hi Ashley,

Im trying yo use your lib to sync data from WP to external system but I could not make it work...im only trying to write to logs...

require_once plugin_dir_path( FILE ) . 'classes/wp-async-request.php'; require_once plugin_dir_path( FILE ) . 'classes/wp-background-process.php';

class EE_sfSDK extends WP_Background_Process { protected $action = 'attendees_background_process'; protected function task( $registration ) { //$this->really_long_running_task(); error_log("This is the task RUNNING of the WP_Background_Process"); return false; } protected function complete() { parent::complete(); // Show notice to user or perform some other arbitrary task... error_log("This is the task COMPLETED WP_Background_Process"); }

the elements are push correctly inside the queue:

foreach ($registrations as $registration) { if ($registration instanceof EE_Registration) { error_log("Event: ext_sync_to_salesforce push_to_queue!!"); $sfSDK->push_to_queue($registration->ID()); } } $sfSDK->save()->dispatch();

the WP table options have a new row:

wp_attendees_background_process_batch_1f8a9447c3ecec7570a4de37af

with the values...

a:7:{i:0;i:2664;i:1;i:2692;i:2;i:2693;i:3;i:2714;i:4;i:2729;i:5;i:2769;i:6;i:2770;}

but looks like the server is never dispatching the BG process....

there is any configuration missing to make it work?

thanks

erdguez avatar Jul 12 '17 19:07 erdguez

Same problem here

pablo-sg-pacheco avatar Jul 22 '17 20:07 pablo-sg-pacheco

My problem was initiating the class later.

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

pablo-sg-pacheco avatar Jul 22 '17 23:07 pablo-sg-pacheco

are u creating the instance and using it at the same place on 'plugins_loaded'? can you share a code snippet of your solution?

erdguez avatar Jul 24 '17 19:07 erdguez

I'm developing a plugin. The first hook to initialize the plugin is "plugins_loaded".

On that moment I instantiate my class that extends the \WP_Background_Process one, I keep a variable for that.

When I need to use it I simply get its already loaded instance and that's it.

To dispatch it I'm doing like this

$bkg_process->push_to_queue( $id1 );
$bkg_process->push_to_queue( $id2 );
$bkg_process->push_to_queue( $id3 );
$bkg_process->save()->dispatch();

pablo-sg-pacheco avatar Jul 24 '17 20:07 pablo-sg-pacheco

are you using "$bkg_process" like a global var?

Im also using it inside a plugin but i need to use the instance in a callback of a function...Im wondering the best way to declare it on 'plugins_loaded' and using it on other part of the code.

erdguez avatar Jul 24 '17 20:07 erdguez

I'm using the singleton design pattern for my plugin. So wherever I am i can get its instance like Plugin::get_instance()

Inside the plugin's main class I created a public variable to keep the background_process class. So in my plugin's constructor I initialize it like $this->bkg_process = MyBkgProcess();

And when I want to use it for real I just call Plugin::get_instance()->bkg_process->push_to_queue() for example.

Does it help you?

pablo-sg-pacheco avatar Jul 24 '17 20:07 pablo-sg-pacheco

yes, thanks! i will try this out.

erdguez avatar Jul 24 '17 20:07 erdguez

;)

Tell me later if you got any lucky and if it worked for you

pablo-sg-pacheco avatar Jul 24 '17 20:07 pablo-sg-pacheco

can you share me full exmple thank @pablo-sg-pacheco

nhoctanker avatar Nov 27 '17 10:11 nhoctanker

Sorry, my plugin is a bit of a mess and I think I'm not able to get only this background processing part and paste it here.

But hopefully I can try to help you with your code

pablo-sg-pacheco avatar Nov 27 '17 12:11 pablo-sg-pacheco

@pablo-sg-pacheco Thanks for the tip on spawning the instance earlier, spent a couple days scratching my head at that one

himynameschris avatar Feb 01 '18 22:02 himynameschris

For those like me still struggling to conciliate OOP with WordPress development, here's how I managed to solve this problem without having a plugin class.

NOTE: My need was to launch a long running process as a cron job scheduled at a fixed time.

  • First, at the beginning of the main plugin file, I import the classes and declare a global variable that will hold the process istance (hat tip to @erdguez).
require_once plugin_dir_path( __FILE__ ) . 'classes/allin-process.php';
global $bg_process;
  • Since my class Allin_Process extends WP_Background_Process, at the beginning of allin-process.php I included the classes from this repo:
require_once plugin_dir_path( __FILE__ ) . 'wp-async-request.php';
require_once plugin_dir_path( __FILE__ ) . 'wp-background-process.php';
  • To ensure that my process class is instantiated at the right time (hat tip to @pablo-sg-pacheco) I hook plugins_loaded to the process istantiation by making use of my global variable:
function allin_plugins_loaded() {
    global $bg_process;
    $bg_process = new Allin_Process();
}
add_action( 'plugins_loaded', 'allin_plugins_loaded' );
  • Set up my cron, nothing new here:
add_action( 'allin_cron_hook', 'allin_run_process' );
if ( !wp_next_scheduled( 'allin_cron_hook' ) && get_option('allin_cron_enabled')) {
    wp_schedule_event( strtotime('01:30:00'), 'daily', 'allin_cron_hook' );
} 
  • In my process function I can finally use this library with no issues:
function allin_run_process() {
        $rows = allin_get_data(); /* I'm a custom function, don't mind me */
        global $bg_process;

        foreach ($rows as $row) {
            $bg_process->push_to_queue($row);
        }

        $bg_process->save()->dispatch();
    }
}

And after roughly 10 hours or scalp scratching, I can finally see my items processed. YAY! Hope this helps someone and thanks to the other participants for the precious leads! -Gigi

GigiSan avatar Jul 18 '18 08:07 GigiSan

@GigiSan your example saved me a lot of scalp scratching.

Thanks.

bliteknight avatar Aug 11 '18 04:08 bliteknight

Another note, should you need to create a background process that needs to run when no one is on your site i.e. these settings are in your wp_config.php

define( 'DISABLE_WP_CRON', true );
define('ALTERNATE_WP_CRON', true);

I had to add this call in the complete() function to keep the jobs going

$hostURL = sprintf("%s://%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http'
    ,$_SERVER['SERVER_NAME'])."/wp-cron.php?doing_wp_cron";
echo file_get_contents($hostURL);
parent::complete();

In my case it stopped working once I left the page and would only continue when I came back. Calling wp-cron.php causes it to continue processing any pending jobs.

bliteknight avatar Aug 11 '18 04:08 bliteknight

Sorry to ask here, I have decide use this plugin for crawl data from other website, it should be split to two background task. For example, I wanna crawl a lot of post and they have pagination, so I have background task for get page 1 content, and then call small background task to crawl data from each post of the list. After page 1 done, the parent background task will move to page 2. Is it good ? Can Wp background process call sub background process and how to? Thanks

animexxx avatar Aug 13 '18 10:08 animexxx

@bliteknight Glad it helped you out! We had the wp-cron timing issue sorted up by our WordPress provider, which has a feature to regularly ping the site so it never goes unvisited for more than a few minutes.

GigiSan avatar Aug 13 '18 10:08 GigiSan

My problem was initiating the class later.

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

Same here but it's not always possible to instantiate a class on plugins_loaded. Let's say I have a background process and inside each task, I'm creating new batch of the background process. In this case, what will happen is I'll run the same child tasks many times on each task() call in the upper-level process.

r0b1n1sl4m avatar Dec 22 '19 21:12 r0b1n1sl4m

Thanks @GigiSan , this saved me a lot of hours. I'd already spent 2 hours head scratching before I found your solution.

reynoldspaul avatar Oct 20 '20 17:10 reynoldspaul

Closing ancient issue. :smile:

ianmjones avatar Apr 11 '23 16:04 ianmjones