Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"autoload": {
"psr-4": {
"Soderlind\\RedisQueueDemo\\": "src/"
"Soderlind\\RedisQueue\\": "src/"
}
}
}
38 changes: 8 additions & 30 deletions redis-queue-demo.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
<?php

/**
* Plugin Name: Redis Queue Demo
* Plugin URI: https://github.com/soderlind/redis-queue-demo
* Description: A plugin demonstrating Redis-based job queue management including workers, job types, and a UI.
* Version: 1.2.0
* Plugin Name: Redis Queue (Legacy Loader)
* Plugin URI: https://github.com/soderlind/redis-queue
* Description: Temporary loader file. New main file is redis-queue.php. Will be removed after folder rename.
* Version: 2.0.0
* Requires at least: 6.7
* Requires PHP: 8.3
* Author: Per Soderlind
* Author URI: https://soderlind.no
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: redis-queue-demo
* Text Domain: redis-queue
* Domain Path: /languages
* Update URI: false
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

define( 'REDIS_QUEUE_DEMO_VERSION', '1.2.0' );
define( 'REDIS_QUEUE_DEMO_MIN_PHP', '8.3' );
define( 'REDIS_QUEUE_DEMO_PLUGIN_FILE', __FILE__ );
define( 'REDIS_QUEUE_DEMO_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'REDIS_QUEUE_DEMO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'REDIS_QUEUE_DEMO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// This file remains only to avoid immediate fatal if directory not yet renamed.
require_once __DIR__ . '/redis-queue.php';

// Load Composer autoload if present.
$autoload = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
}

// Bootstrap namespaced main class.
Soderlind\RedisQueueDemo\Core\Redis_Queue_Demo::get_instance();

function redis_queue_demo() {
return Soderlind\RedisQueueDemo\Core\Redis_Queue_Demo::get_instance();
}

function redis_queue_enqueue_job( $job_type, $payload = array(), $options = array() ) {
return redis_queue_demo()->enqueue_job( $job_type, $payload, $options );
}

function redis_queue_process_jobs( $queue = 'default', $max_jobs = null ) {
$instance = redis_queue_demo();
if ( ! $instance->get_queue_manager() || ! $instance->get_job_processor() ) {
return false;
}
$worker = new \Soderlind\RedisQueueDemo\Workers\Sync_Worker( $instance->get_queue_manager(), $instance->get_job_processor() );
return $worker->process_jobs( (array) $queue, $max_jobs );
}
// All logic handled by redis-queue.php.
75 changes: 75 additions & 0 deletions redis-queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Plugin Name: Redis Queue
* Plugin URI: https://github.com/soderlind/redis-queue
* Description: Redis-backed prioritized, delayed & retryable background jobs for WordPress (workers, REST API, admin UI).
* Version: 2.0.0
* Requires at least: 6.7
* Requires PHP: 8.3
* Author: Per Soderlind
* Author URI: https://soderlind.no
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: redis-queue
* Domain Path: /languages
* Update URI: false
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

define( 'REDIS_QUEUE_VERSION', '2.0.0' );
define( 'REDIS_QUEUE_MIN_PHP', '8.3' );
define( 'REDIS_QUEUE_PLUGIN_FILE', __FILE__ );
define( 'REDIS_QUEUE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'REDIS_QUEUE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'REDIS_QUEUE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Load Composer autoload if present.
$autoload = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
}

/**
* One-time migration of options from old prefix redis_queue_demo_ to redis_queue_.
*/
function redis_queue_migrate_options_v2() {
if ( get_option( 'redis_queue_migrated_options_v2' ) ) {
return;
}
$option_suffixes = [
'redis_host', 'redis_port', 'redis_password', 'redis_database', 'default_queue', 'max_jobs_per_run',
'worker_timeout', 'max_retries', 'retry_backoff', 'enable_logging', 'cleanup_completed_jobs', 'cleanup_after_days',
];
foreach ( $option_suffixes as $suffix ) {
$old_key = 'redis_queue_demo_' . $suffix;
$new_key = 'redis_queue_' . $suffix;
$old_val = get_option( $old_key, null );
if ( $old_val !== null && get_option( $new_key, null ) === null ) {
update_option( $new_key, $old_val );
}
}
update_option( 'redis_queue_migrated_options_v2', '1' );
}
add_action( 'plugins_loaded', 'redis_queue_migrate_options_v2', 1 );

// Bootstrap namespaced main class (new namespace & class name).
Soderlind\RedisQueue\Core\Redis_Queue::get_instance();

function redis_queue() {
return Soderlind\RedisQueue\Core\Redis_Queue::get_instance();
}

function redis_queue_enqueue_job( $job_type, $payload = array(), $options = array() ) {
return redis_queue()->enqueue_job( $job_type, $payload, $options );
}

function redis_queue_process_jobs( $queue = 'default', $max_jobs = null ) {
$instance = redis_queue();
if ( ! $instance->get_queue_manager() || ! $instance->get_job_processor() ) {
return false;
}
$worker = new \Soderlind\RedisQueue\Workers\Sync_Worker( $instance->get_queue_manager(), $instance->get_job_processor() );
return $worker->process_jobs( (array) $queue, $max_jobs );
}
26 changes: 13 additions & 13 deletions src/Core/Job_Processor.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace Soderlind\RedisQueueDemo\Core;
namespace Soderlind\RedisQueue\Core;

use Exception;
use Soderlind\RedisQueueDemo\Contracts\Queue_Job;
use Soderlind\RedisQueueDemo\Contracts\Job_Result;
use Soderlind\RedisQueueDemo\Contracts\Basic_Job_Result;
use Soderlind\RedisQueue\Contracts\Queue_Job;
use Soderlind\RedisQueue\Contracts\Job_Result;
use Soderlind\RedisQueue\Contracts\Basic_Job_Result;

/**
* Namespaced Job Processor.
Expand Down Expand Up @@ -46,7 +46,7 @@ public function process_job( $job_data ): Job_Result {
$this->handle_failed_job( $job_id, $job, $result, 1, null );
}
if ( function_exists( 'do_action' ) ) {
\do_action( 'redis_queue_demo_job_processed', $job_id, $job, $result );
\do_action( 'redis_queue_job_processed', $job_id, $job, $result );
}
return $result;
} catch (Exception $e) {
Expand All @@ -62,7 +62,7 @@ public function process_job( $job_data ): Job_Result {
$this->mark_job_failed( $job_id, $result );
}
if ( function_exists( 'do_action' ) ) {
\do_action( 'redis_queue_demo_job_failed', $job_id, $e, $job_data );
\do_action( 'redis_queue_job_failed', $job_id, $e, $job_data );
}
return $result;
} finally {
Expand All @@ -76,7 +76,7 @@ public function process_jobs( $queues = [ 'default' ], $max_jobs = 10 ): array {
$start_time = microtime( true );
$start_memory = memory_get_usage( true );
if ( function_exists( 'do_action' ) ) {
\do_action( 'redis_queue_demo_batch_start', $queues, $max_jobs );
\do_action( 'redis_queue_batch_start', $queues, $max_jobs );
}
while ( $processed < $max_jobs ) {
$job_data = $this->queue_manager->dequeue( $queues );
Expand All @@ -93,7 +93,7 @@ public function process_jobs( $queues = [ 'default' ], $max_jobs = 10 ): array {
$total_time = microtime( true ) - $start_time;
$total_memory = memory_get_peak_usage( true ) - $start_memory;
if ( function_exists( 'do_action' ) ) {
\do_action( 'redis_queue_demo_batch_complete', $results, $processed, $total_time, $total_memory );
\do_action( 'redis_queue_batch_complete', $results, $processed, $total_time, $total_memory );
}
return [ 'processed' => $processed, 'total_time' => $total_time, 'total_memory' => $total_memory, 'results' => $results ];
}
Expand Down Expand Up @@ -145,9 +145,9 @@ private function sanitize_job_data( array $job_data ): array {
if ( is_string( $job_type ) && $job_type !== '' ) {
// Canonical job type mapping only (legacy variants removed).
$map = [
'email' => 'Soderlind\\RedisQueueDemo\\Jobs\\Email_Job',
'image_processing' => 'Soderlind\\RedisQueueDemo\\Jobs\\Image_Processing_Job',
'api_sync' => 'Soderlind\\RedisQueueDemo\\Jobs\\API_Sync_Job',
'email' => 'Soderlind\\RedisQueue\\Jobs\\Email_Job',
'image_processing' => 'Soderlind\\RedisQueue\\Jobs\\Image_Processing_Job',
'api_sync' => 'Soderlind\\RedisQueue\\Jobs\\API_Sync_Job',
];
$key = $map[ strtolower( $job_type ) ] ?? null;
if ( $key ) {
Expand Down Expand Up @@ -180,7 +180,7 @@ private function get_job_class( $job_type ) {
'image_processing' => 'Soderlind\\RedisQueueDemo\\Jobs\\Image_Processing_Job',
'api_sync' => 'Soderlind\\RedisQueueDemo\\Jobs\\API_Sync_Job',
];
$job_classes = function_exists( 'apply_filters' ) ? \apply_filters( 'redis_queue_demo_job_classes', $base_map ) : $base_map;
$job_classes = function_exists( 'apply_filters' ) ? \apply_filters( 'redis_queue_job_classes', $base_map ) : $base_map;
if ( isset( $job_classes[ $job_type_normalized ] ) ) {
return $job_classes[ $job_type_normalized ];
}
Expand All @@ -199,7 +199,7 @@ private function handle_successful_job( $job_id, Job_Result $result ): void {
$table = $wpdb->prefix . 'redis_queue_jobs';
$wpdb->update( $table, [ 'status' => 'completed', 'result' => ( function_exists( 'wp_json_encode' ) ? \wp_json_encode( $result->to_array() ) : json_encode( $result->to_array() ) ), 'updated_at' => ( function_exists( 'current_time' ) ? \current_time( 'mysql' ) : date( 'Y-m-d H:i:s' ) ) ], [ 'job_id' => $job_id ], [ '%s', '%s', '%s' ], [ '%s' ] );
if ( function_exists( 'do_action' ) ) {
\do_action( 'redis_queue_demo_job_completed', $job_id, $result );
\do_action( 'redis_queue_job_completed', $job_id, $result );
}
}
private function handle_failed_job( $job_id, Queue_Job $job, Job_Result $result, $attempt, $exception = null ): void {
Expand Down
Loading