diff --git a/assets/admin.js b/assets/admin.js index d467d8b..a7074d4 100644 --- a/assets/admin.js +++ b/assets/admin.js @@ -1,9 +1,28 @@ -/* Redis Queue Demo Admin JavaScript */ +/** + * Redis Queue Admin JavaScript + * + * Handles all admin interface interactions including: + * - Dashboard statistics and monitoring + * - Job management (view, cancel, purge) + * - Test job submission + * - Redis connection testing + * - Worker triggering + * - Diagnostics and debugging + * + * @package RedisQueue + * @since 2.0.0 + */ (function($) { 'use strict'; - // Main admin object + /** + * Main admin object containing all admin interface functionality. + */ var RedisQueueAdmin = { + /** + * Initialize the admin interface. + * Sets up event bindings and initializes page-specific features. + */ init: function() { this.bindEvents(); this.initDashboard(); @@ -11,6 +30,10 @@ this.initSettings(); }, + /** + * Bind event handlers to UI elements. + * Uses event delegation for dynamically added elements. + */ bindEvents: function() { // Dashboard events $(document).on('click', '#trigger-worker', this.triggerWorker); @@ -18,6 +41,7 @@ $(document).on('click', '#run-diagnostics', this.runDiagnostics); $(document).on('click', '#debug-test', this.runDebugTest); $(document).on('click', '#reset-stuck-jobs', this.resetStuckJobs); + // Purge events $(document).on('click', '.purge-buttons button', this.purgeJobs); @@ -32,6 +56,10 @@ $(document).on('click', '#test-redis-connection', this.testConnection); }, + /** + * Initialize dashboard functionality. + * Sets up auto-refresh for job statistics. + */ initDashboard: function() { // Auto-refresh stats every 30 seconds if on dashboard if ($('#queued-jobs').length) { @@ -40,6 +68,10 @@ } }, + /** + * Initialize test job forms. + * Binds submit handlers to prevent default form submission. + */ initTestForms: function() { // Initialize test forms $('.test-job-form').each(function() { @@ -51,6 +83,10 @@ }); }, + /** + * Initialize settings page features. + * Auto-tests Redis connection on page load. + */ initSettings: function() { // Initialize settings page if ($('#test-redis-connection').length) { @@ -59,12 +95,19 @@ } }, + /** + * Trigger the worker to process queued jobs. + * Sends AJAX request to process jobs and displays results. + * + * @param {Event} e Click event + */ triggerWorker: function(e) { e.preventDefault(); var $button = $(this); var originalText = $button.text(); + // Disable button during processing $button.prop('disabled', true).text(redisQueueAdmin.strings.processing); $.ajax({ @@ -79,7 +122,7 @@ RedisQueueAdmin.showNotice(redisQueueAdmin.strings.workerTriggered, 'success'); RedisQueueAdmin.refreshStats(); - // Show processing details + // Show processing details in console if (response.data) { var details = 'Processed: ' + (response.data.processed || 0) + ' jobs\n' + 'Success: ' + (response.data.successful || 0) + '\n' + @@ -94,11 +137,18 @@ RedisQueueAdmin.showNotice(redisQueueAdmin.strings.error, 'error'); }, complete: function() { + // Re-enable button $button.prop('disabled', false).text(originalText); } }); }, + /** + * Refresh dashboard statistics. + * Updates job counts (queued, processing, completed, failed). + * + * @param {Event} [e] Optional click event + */ refreshStats: function(e) { if (e) e.preventDefault(); @@ -111,6 +161,7 @@ }, success: function(response) { if (response.success && response.data) { + // Update job count displays $('#queued-jobs').text(response.data.queued || 0); $('#processing-jobs').text(response.data.processing || 0); $('#completed-jobs').text(response.data.completed || 0); @@ -123,6 +174,12 @@ }); }, + /** + * Run Redis diagnostics. + * Tests connection, read/write operations, and displays Redis state. + * + * @param {Event} e Click event + */ runDiagnostics: function(e) { if (e) e.preventDefault(); @@ -151,9 +208,13 @@ html += '
  • Test Read: ' + (diagnostics.test_read ? 'Success' : 'Failed') + '
  • '; html += '
  • Queue Prefix: ' + diagnostics.queue_prefix + '
  • '; html += '
  • Redis Keys Found: ' + (diagnostics.redis_keys ? diagnostics.redis_keys.length : 0) + '
  • '; + + // Display Redis keys if found if (diagnostics.redis_keys && diagnostics.redis_keys.length > 0) { html += '
  • Keys: ' + diagnostics.redis_keys.join(', ') + '
  • '; } + + // Display any errors if (diagnostics.error) { html += '
  • Error: ' + diagnostics.error + '
  • '; } @@ -170,6 +231,12 @@ }); }, + /** + * Run comprehensive debug test. + * Performs detailed analysis of plugin configuration and state. + * + * @param {Event} e Click event + */ runDebugTest: function(e) { if (e) e.preventDefault(); @@ -245,6 +312,12 @@ }); }, + /** + * Reset stuck jobs. + * Resets jobs that are stuck in "processing" state back to "queued". + * + * @param {Event} e Click event + */ resetStuckJobs: function(e) { if (e) e.preventDefault(); @@ -266,7 +339,7 @@ if (response.success && response.data) { $result.html('

    ' + response.data.message + '

    '); - // Refresh stats if available + // Refresh stats to show updated counts if (typeof RedisQueueAdmin.refreshStats === 'function') { RedisQueueAdmin.refreshStats(); } @@ -281,6 +354,12 @@ }); }, + /** + * Purge jobs from the database. + * Supports purging completed, failed, older, or all jobs. + * + * @param {Event} e Click event + */ purgeJobs: function(e) { e.preventDefault(); var $button = $(this); @@ -289,6 +368,7 @@ if (!scope) return; + // Build appropriate confirmation message based on scope var confirmMsg = 'Are you sure you want to purge ' + scope + ' jobs?'; if (scope === 'all') { confirmMsg = 'DANGER: This will delete ALL jobs. Continue?'; @@ -314,6 +394,7 @@ success: function(response) { if (response.success && response.data) { $result.html('

    ' + response.data.message + '

    '); + // Refresh stats to reflect purged jobs if (typeof RedisQueueAdmin.refreshStats === 'function') { RedisQueueAdmin.refreshStats(); } @@ -330,12 +411,18 @@ }); }, + /** + * View detailed information about a specific job. + * Opens a modal with job details loaded via REST API. + * + * @param {Event} e Click event + */ viewJob: function(e) { e.preventDefault(); var jobId = $(this).data('job-id'); - // Create modal or use WordPress media modal + // Create and display modal var modal = $('
    ' + '