Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active March 24, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Shelob9/1d8756c30330d291009e52a51c59a40e to your computer and use it in GitHub Desktop.
Save Shelob9/1d8756c30330d291009e52a51c59a40e to your computer and use it in GitHub Desktop.
composer require a5hleyrich/wp-queue
class PostSaveJob extends \WP_Queue\Job
{
protected $postId;
public function __construct( $postId )
{
$this->postId = $postId;
}
public function handle()
{
$post = get_post($this->postId );
}
}
class PostSaveJob extends \WP_Queue\Job
{
protected $postId;
public function __construct( $postId )
{
$this->postId = $postId;
}
public function handle() {
$post = get_post( $this->postId );
if( ! in_array( get_post_type($post), [ 'post', 'page' ]) ){
return;
}
$controller = new \WP_REST_Posts_Controller( 'post' );
$route = 'wp/v2/posts/' . $this->postId;
$request = new \WP_REST_Request('GET', $route);
$request->set_param( 'id', $this->postId );
$response = $controller->get_item($request );
$name = sprintf( '%s/posts/%s.json', __DIR__, $post->post_name );
file_put_contents( $name, json_encode( $response ) );
return $name;
}
}
$fileName = ( new \calderawp\WordPressPlugin\Jobs\PostSaveJob( $postId ) )->handle();
class PostSaveJobTest extends TestCase {
public function testSaves(){
$postName = 'foo';
$postId = wp_insert_post( ['post_content' => 'fff', 'post_type' => 'post', 'post_status' => 'publish', 'post_name' => $postName ] );
$fileName = ( new \calderawp\WordPressPlugin\Jobs\PostSaveJob( $postId ) )->handle();
$this->assertTrue( file_exists( $fileName ) );
}
}
add_action( 'save_post', function( $postId ) {
wp_queue()->push( new PostSaveJob( $postId ) );
});
add_action( 'save_post', function( $postId ) {
wp_queue()->push( new PostSaveJob( $postId ), 600 );
});
add_action( 'plugins_loaded', function(){
wp_queue()->cron();
});
add_filter( 'wp_queue_default_connection', function() {
return 'sync';
} );
function workQueue( $jobs = 10, $attemptsPerJob = 3 ){
//Get a properly configured queue
$queue = wp_queue();
//Get the worker for the queue
$worker = $queue->worker($attemptsPerJob);
//Loop through jobs in queue until we've done the number passed in $jobs
// or no jobs left to run
$totalJobs = 0;
while( $worker->process() && $jobs <= $totalJobs ){
$totalJobs++;
}
//Work the queue and
return rest_ensure_response( [ 'totalJobs' => $totalJobs ] );
}
add_action( 'rest_api_init', function(){
register_rest_route( 'hi-roy', 'queue/run', [
'methods' => ['POST', 'GET' ],
'callback' => 'workQueue'
]);
});
@yipeecaiey
Copy link

yipeecaiey commented Jan 31, 2020

Working through the implementation of your article on https://torquemag.io/2019/01/using-the-wp-queue-to-copy-rest-api-data-to-files/ and I'm wondering...shouldn't line 10 of 7.php to be:
while( $worker->process() && $totalJobs < $jobs ){
instead of:
while( $worker->process() && $jobs <= $totalJobs ){

...swapping the order of comparison allows jobs to actually run
...changing the operator limits the number of jobs run to the $jobs rather than $jobs+1

@yipeecaiey
Copy link

I had to rewrite the workQueue function as the register_rest_route() method passes a WP_REST_Request to the callback function. This may have changed since the tutorial was written. The $jobs and $attemptsPerJob can be passed as parameters in the request.

I also had changed the original line 10 to be:
while( $jobs <= $totalJobs && $worker->process() ){
...this stops the while loop at the correct number of jobs to be processed rather than $jobs+1

I forked this gist and updated it there if there is any value in sharing/updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment