Skip to content

Commit f4fa5da

Browse files
authored
Merge pull request #9 from soderlind/copilot/add-source-comments-php-js
Add comprehensive source comments to PHP and JavaScript files
2 parents c658ad0 + b486d0a commit f4fa5da

File tree

10 files changed

+896
-41
lines changed

10 files changed

+896
-41
lines changed

assets/admin.js

Lines changed: 162 additions & 17 deletions
Large diffs are not rendered by default.

redis-queue.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
* Domain Path: /languages
1515
* Update URI: false
1616
*/
17+
// Exit if accessed directly.
1718
if ( ! defined( 'ABSPATH' ) ) {
1819
exit;
1920
}
2021

22+
// Plugin version and requirements.
2123
define( 'REDIS_QUEUE_VERSION', '2.0.0' );
2224
define( 'REDIS_QUEUE_MIN_PHP', '8.3' );
25+
26+
// Plugin file paths and URLs.
2327
define( 'REDIS_QUEUE_PLUGIN_FILE', __FILE__ );
2428
define( 'REDIS_QUEUE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
2529
define( 'REDIS_QUEUE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
@@ -33,31 +37,52 @@
3337

3438
/**
3539
* One-time migration of options from old prefix redis_queue_demo_ to redis_queue_.
40+
*
41+
* Migrates plugin options from the old naming convention (redis_queue_demo_*)
42+
* to the new naming convention (redis_queue_*). This runs once during plugin
43+
* upgrade from version 1.x to 2.0.0.
3644
*/
3745
function redis_queue_migrate_options_v2() {
46+
// Check if migration has already been performed.
3847
if ( get_option( 'redis_queue_migrated_options_v2' ) ) {
3948
return;
4049
}
50+
51+
// Define all option keys that need to be migrated.
4152
$option_suffixes = [
4253
'redis_host', 'redis_port', 'redis_password', 'redis_database', 'default_queue', 'max_jobs_per_run',
4354
'worker_timeout', 'max_retries', 'retry_backoff', 'enable_logging', 'cleanup_completed_jobs', 'cleanup_after_days',
4455
];
56+
57+
// Migrate each option from old to new key name.
4558
foreach ( $option_suffixes as $suffix ) {
4659
$old_key = 'redis_queue_demo_' . $suffix;
4760
$new_key = 'redis_queue_' . $suffix;
4861
$old_val = get_option( $old_key, null );
62+
63+
// Only migrate if old value exists and new value doesn't exist.
4964
if ( $old_val !== null && get_option( $new_key, null ) === null ) {
5065
update_option( $new_key, $old_val );
5166
}
5267
}
68+
69+
// Mark migration as complete.
5370
update_option( 'redis_queue_migrated_options_v2', '1' );
5471
}
5572
add_action( 'plugins_loaded', 'redis_queue_migrate_options_v2', 1 );
5673

57-
// Bootstrap namespaced main class (new namespace & class name).
74+
/**
75+
* Bootstrap the main plugin class.
76+
*
77+
* Initializes the Redis Queue plugin if all dependencies are loaded.
78+
* If dependencies are missing (e.g., Composer autoloader not run),
79+
* displays an admin notice to inform the user.
80+
*/
5881
if ( class_exists( 'Soderlind\RedisQueue\Core\Redis_Queue' ) ) {
82+
// Initialize the plugin singleton instance.
5983
Soderlind\RedisQueue\Core\Redis_Queue::get_instance();
6084
} else {
85+
// Show error notice if dependencies are missing.
6186
add_action( 'admin_notices', function() {
6287
if ( current_user_can( 'activate_plugins' ) ) {
6388
echo '<div class="notice notice-error"><p>';
@@ -68,13 +93,32 @@ function redis_queue_migrate_options_v2() {
6893
} );
6994
}
7095

96+
/**
97+
* Get the main plugin instance.
98+
*
99+
* Provides global access to the Redis Queue plugin singleton instance.
100+
* This is the recommended way to access the plugin's public API.
101+
*
102+
* @return \Soderlind\RedisQueue\Core\Redis_Queue|null Plugin instance or null if not loaded.
103+
*/
71104
function redis_queue() {
72105
if ( class_exists( 'Soderlind\RedisQueue\Core\Redis_Queue' ) ) {
73106
return Soderlind\RedisQueue\Core\Redis_Queue::get_instance();
74107
}
75108
return null;
76109
}
77110

111+
/**
112+
* Enqueue a background job.
113+
*
114+
* Convenience function to add a job to the Redis queue. This is a wrapper
115+
* around the main plugin's enqueue_job method.
116+
*
117+
* @param string $job_type The type of job (email, image_processing, api_sync, or custom).
118+
* @param array $payload Job-specific data to be processed.
119+
* @param array $options Optional settings: priority, queue, delay.
120+
* @return string|false Job ID on success, false on failure.
121+
*/
78122
function redis_queue_enqueue_job( $job_type, $payload = array(), $options = array() ) {
79123
$instance = redis_queue();
80124
if ( ! $instance ) {
@@ -83,11 +127,23 @@ function redis_queue_enqueue_job( $job_type, $payload = array(), $options = arra
83127
return $instance->enqueue_job( $job_type, $payload, $options );
84128
}
85129

130+
/**
131+
* Process queued jobs synchronously.
132+
*
133+
* Creates a synchronous worker and processes jobs from the specified queue(s).
134+
* This can be called from WP-CLI, cron jobs, or custom worker scripts.
135+
*
136+
* @param string|array $queue Queue name(s) to process. Default 'default'.
137+
* @param int|null $max_jobs Maximum number of jobs to process. Default from settings.
138+
* @return array|false Processing results array or false on failure.
139+
*/
86140
function redis_queue_process_jobs( $queue = 'default', $max_jobs = null ) {
87141
$instance = redis_queue();
88142
if ( ! $instance || ! $instance->get_queue_manager() || ! $instance->get_job_processor() ) {
89143
return false;
90144
}
145+
146+
// Create a synchronous worker and process jobs.
91147
$worker = new \Soderlind\RedisQueue\Workers\Sync_Worker( $instance->get_queue_manager(), $instance->get_job_processor() );
92148
return $worker->process_jobs( (array) $queue, $max_jobs );
93149
}

src/Contracts/Job_Result.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,79 @@
11
<?php
22
namespace Soderlind\RedisQueue\Contracts;
33

4+
/**
5+
* Job Result Interface.
6+
* Defines the contract for job execution results.
7+
*
8+
* Results track success/failure status, data, errors, and performance metrics.
9+
* They must be serializable for storage in the database.
10+
*/
411
interface Job_Result {
12+
/**
13+
* Check if job execution was successful.
14+
*
15+
* @return bool True if successful, false if failed.
16+
*/
517
public function is_successful();
18+
19+
/**
20+
* Get result data.
21+
* Data returned by successful job execution.
22+
*
23+
* @return mixed Result data.
24+
*/
625
public function get_data();
26+
27+
/**
28+
* Get error message.
29+
* Returns error message if job failed.
30+
*
31+
* @return string|null Error message or null if successful.
32+
*/
733
public function get_error_message();
34+
35+
/**
36+
* Get error code.
37+
* Returns error code if job failed.
38+
*
39+
* @return int|null Error code or null if successful.
40+
*/
841
public function get_error_code();
42+
43+
/**
44+
* Get result metadata.
45+
* Additional contextual information about the result.
46+
*
47+
* @return array Metadata.
48+
*/
949
public function get_metadata();
50+
51+
/**
52+
* Get job execution time in seconds.
53+
*
54+
* @return float|null Execution time or null if not measured.
55+
*/
1056
public function get_execution_time();
57+
58+
/**
59+
* Get peak memory usage in bytes.
60+
*
61+
* @return int|null Memory usage or null if not measured.
62+
*/
1163
public function get_memory_usage();
64+
65+
/**
66+
* Convert result to array for serialization.
67+
*
68+
* @return array Result data as array.
69+
*/
1270
public function to_array();
71+
72+
/**
73+
* Create result from array data.
74+
*
75+
* @param array $data Result data array.
76+
* @return self Result instance.
77+
*/
1378
public static function from_array( $data );
1479
}

src/Contracts/Queue_Job.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,103 @@
11
<?php
22
namespace Soderlind\RedisQueue\Contracts;
33

4+
/**
5+
* Queue Job Interface.
6+
* Defines the contract that all queue jobs must implement.
7+
*
8+
* Jobs must be serializable for storage in Redis and the database.
9+
* They must also handle execution, failures, and retry logic.
10+
*/
411
interface Queue_Job {
12+
/**
13+
* Get job type identifier.
14+
*
15+
* @return string Job type.
16+
*/
517
public function get_job_type();
18+
19+
/**
20+
* Get job payload data.
21+
*
22+
* @return array Job payload.
23+
*/
624
public function get_payload();
25+
26+
/**
27+
* Get job priority.
28+
* Lower values indicate higher priority.
29+
*
30+
* @return int Priority value.
31+
*/
732
public function get_priority();
33+
34+
/**
35+
* Get maximum retry attempts.
36+
*
37+
* @return int Max retry attempts.
38+
*/
839
public function get_retry_attempts();
40+
41+
/**
42+
* Get execution timeout in seconds.
43+
*
44+
* @return int Timeout in seconds.
45+
*/
946
public function get_timeout();
47+
48+
/**
49+
* Get queue name for this job.
50+
*
51+
* @return string Queue name.
52+
*/
1053
public function get_queue_name();
11-
public function execute(); // Must return Job_Result
54+
55+
/**
56+
* Execute the job.
57+
* Must return a Job_Result indicating success or failure.
58+
*
59+
* @return Job_Result Job execution result.
60+
*/
61+
public function execute();
62+
63+
/**
64+
* Handle job failure.
65+
* Called when job fails to allow custom error handling and logging.
66+
*
67+
* @param mixed $exception Exception or failure reason.
68+
* @param int $attempt Current attempt number.
69+
*/
1270
public function handle_failure( $exception, $attempt );
71+
72+
/**
73+
* Determine if job should be retried after failure.
74+
*
75+
* @param mixed $exception Exception or failure reason.
76+
* @param int $attempt Current attempt number.
77+
* @return bool True if job should be retried.
78+
*/
1379
public function should_retry( $exception, $attempt );
80+
81+
/**
82+
* Get delay before retry in seconds.
83+
*
84+
* @param int $attempt Attempt number.
85+
* @return int Delay in seconds.
86+
*/
1487
public function get_retry_delay( $attempt );
88+
89+
/**
90+
* Serialize job for storage.
91+
*
92+
* @return array Serialized job data.
93+
*/
1594
public function serialize();
95+
96+
/**
97+
* Deserialize job from stored data.
98+
*
99+
* @param array $data Serialized job data.
100+
* @return self Job instance.
101+
*/
16102
public static function deserialize( $data );
17103
}

0 commit comments

Comments
 (0)