wp-background-processing
wp-background-processing copied to clipboard
Add persistent data during background process tasks to be used later on completion
Similar to #9 , I need to add persistent data during tasks in the background process, so by the time the background process has completed, I can then do something with the persistent data.
Could this not be accomplished with the following pattern?
class WP_Example_Process extends WP_Background_Process {
/**
* @var string
*/
protected $action = 'example_process';
protected $persisted_data;
/**
* Task
*
* Override this method to perform any actions required on each
* queue item. Return the modified item for further processing
* in the next pass through. Or, return false to remove the
* item from the queue.
*
* @param mixed $item Queue item to iterate over
*
* @return mixed
*/
protected function task( $item ) {
// Actions to perform
$this->persisted_data = true;
return false;
}
/**
* Complete
*
* Override if applicable, but ensure that the below actions are
* performed, or, call parent::complete().
*/
protected function complete() {
parent::complete();
echo ($this->persisted_data) ? "it persisted" : "hmmmm didn't persist :/";
// Show notice to user or perform some other arbitrary task...
}
}
I have the same problem.
@dskvr the problem is, if the queue if split into more dispatches due to memory/time limit, all property of the WP_Background_Process instance are lost.
If you set the persistent data at each item you might still have it at complete(), but if you set it at construction for example, you'll lose it at the 2nd dispatch if more than one is needed.
Closing as duplicate of #9.