Skip to content

Commit

Permalink
Queue UI (#458)
Browse files Browse the repository at this point in the history
* Continued work on queue UI=

* Wrapping up the core of the queue UI

* Working table for queue UI

* Adding counts to the queue

* Switch to use wp_count_posts for performance

* Style the row table

* Adding single view for queue job

* Adding queue UI with fixes for retrying
  • Loading branch information
srtfisher authored Nov 29, 2023
1 parent 50b55a2 commit c4b945b
Show file tree
Hide file tree
Showing 13 changed files with 761 additions and 6 deletions.
29 changes: 26 additions & 3 deletions config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| Define the queue provider used in the application.
|
*/
'default' => environment( 'QUEUE_CONNECTION', 'wordpress' ),
'default' => environment( 'QUEUE_CONNECTION', 'wordpress' ),

/*
|--------------------------------------------------------------------------
Expand All @@ -25,7 +25,30 @@
| The amount of items handled in one run of the queue.
|
*/
'batch_size' => environment( 'QUEUE_BATCH_SIZE', 100 ),
'batch_size' => environment( 'QUEUE_BATCH_SIZE', 5 ),

/*
|--------------------------------------------------------------------------
| Maximum number of concurrent batches
|--------------------------------------------------------------------------
|
| The maximum number of batches that can be run concurrently. For example,
| if 1000 queue jobs are dispatched and this is set to 5 with a batch size
| of 100, then 5 batches of 100 will be run concurrently and take two runs
| of the queue to complete.
|
*/
'max_concurrent_batches' => environment( 'QUEUE_MAX_CONCURRENT_BATCHES', 1 ),

/*
|--------------------------------------------------------------------------
| Enable the Queue Admin Interface
|--------------------------------------------------------------------------
|
| Enable the queue admin interface to display queue jobs.
|
*/
'enable_admin' => environment( 'QUEUE_ENABLE_ADMIN', true ),

/*
|--------------------------------------------------------------------------
Expand All @@ -35,7 +58,7 @@
| Control the configuration for the queue providers.
|
*/
'wordpress' => [
'wordpress' => [
// Delay between queue runs in seconds.
'delay' => environment( 'QUEUE_DELAY', 0 ),
],
Expand Down
1 change: 1 addition & 0 deletions src/mantle/database/query/class-post-query-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Post_Query_Builder extends Builder {
'post_author' => 'author',
'post_name' => 'name',
'slug' => 'name',
'status' => 'post_status',
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/mantle/queue/class-queue-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function register() {
fn ( $app ) => new Dispatcher( $app ),
);

// Register queue console commands.
$this->add_command( Run_Command::class );

// Register the queue service providers.
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/queue/class-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function( Queue_Worker_Job $job ) use ( $provider ) {

$this->events->dispatch( new Job_Failed( $provider, $job, $e ) );
} finally {
// TODO: Revisit this and don't delete the job. unlock it and let it be retried.
// TODO: Don't delete after completion.
if ( ! $job->has_failed() ) {
$job->delete();
} elseif ( $job->can_retry() ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Queue_Job_Admin_Page class file.
*
* @package Mantle
*/

namespace Mantle\Queue\Providers\WordPress\Admin;

use Mantle\Queue\Providers\WordPress\Post_Status;
use Mantle\Queue\Providers\WordPress\Queue_Job;
use Mantle\Queue\Providers\WordPress\Queue_Worker_Job;

/**
* Renders the queue admin page screen.
*
* @todo Refactor to use Blade and Mantle templating.
*/
class Queue_Job_Admin_Page {

/**
* Render the admin page.
*/
public function render(): void {
$job_id = ! empty( $_GET['job'] ) ? absint( $_GET['job'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

match ( true ) {
! empty( $_GET['action'] ) && $job_id => $this->render_action( $job_id ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
! empty( $job_id ) => $this->render_single_job( $job_id ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
default => $this->render_table(),
};
}

/**
* Render a single job view.
*
* @param int $job_id The job ID.
*/
protected function render_single_job( int $job_id ): void {
$job = Queue_Job::find( $job_id );

if ( empty( $job ) ) {
wp_die( esc_html__( 'Invalid job ID.', 'mantle' ) );
}

include __DIR__ . '/template/single.php';
}

/**
* Handle an action (retry/delete).
*
* @param int $job_id The job ID.
*/
protected function render_action( int $job_id ): void {
if (
empty( $_GET['_wpnonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'queue-job-action-' . $job_id )
) {
wp_die( 'Invalid nonce.' );
}

$action = sanitize_text_field( wp_unslash( $_GET['action'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$job = Queue_Job::find( $job_id );
$message = '';

if ( empty( $job ) ) {
wp_die( esc_html__( 'Invalid job ID.', 'mantle' ) );
}

if ( 'retry' === $action ) {
if ( Post_Status::FAILED->value !== $job->status ) {
wp_die( esc_html__( 'Job is not in a failed state.', 'mantle' ) );
}

( new Queue_Worker_Job( $job ) )->retry();

$message = esc_html__( 'Job has been scheduled to be retried.', 'mantle' );
} elseif ( 'delete' === $action ) {
$job->delete( true );

$message = esc_html__( 'Job has been deleted.', 'mantle' );
}

if ( ! empty( $message ) ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html( $message )
);
}

$this->render_table();
}

/**
* Render the queue table.
*/
protected function render_table(): void {
$table = new Queue_Jobs_Table();

$table->prepare_items();

include __DIR__ . '/template/table.php';
}
}
Loading

0 comments on commit c4b945b

Please sign in to comment.