| | |
| | | |
| | | ]; |
| | | |
| | | protected ?CacheManager $cache = null; |
| | | protected ?Cache $cache = null; |
| | | protected int $ttl = 300; |
| | | // Cache keys for different data types |
| | | private const CACHE_QUEUE_STATUS = 'status'; |
| | |
| | | { |
| | | global $wpdb; |
| | | $this->wpdb = $wpdb; |
| | | $this->cache = CacheManager::for('queue', DAY_IN_SECONDS); |
| | | $this->cache = Cache::for('queue', 'user',DAY_IN_SECONDS)->user(); |
| | | add_action('jvb_process_queue', [ $this, 'checkQueue' ]); |
| | | add_action('jvb_queue_maintenance', [$this, 'hourlyMaintenance']); |
| | | add_action('jvbEmailDailyMetricsReport', [$this, 'emailDailyMetricsReport']); |
| | |
| | | } |
| | | public function registerAdminAction():void |
| | | { |
| | | $admin = JVB()->admin(); |
| | | $admin = AdminPages::getInstance(); |
| | | $admin->registerAction( |
| | | 'Restart Stuck Operations', |
| | | 'restart-stuck-operations', |
| | |
| | | ]; |
| | | |
| | | } catch (Exception $e) { |
| | | JVB()->error()->log('queue', $e->getMessage(), $data, 'high'); |
| | | Errors::getInstance()->log('queue', $e->getMessage(), $data, 'high'); |
| | | return new WP_Error('queue_failed', $e->getMessage()); |
| | | } |
| | | } |
| | |
| | | $this->processOperation($operation); |
| | | |
| | | // Invalidate operation cache after processing |
| | | $this->cache->delete(self::CACHE_OPERATION_PREFIX . $operation->id); |
| | | $this->cache->delete(self::CACHE_USER_QUEUE_PREFIX . $operation->user_id); |
| | | $this->cache->forget(self::CACHE_OPERATION_PREFIX . $operation->id); |
| | | $this->cache->forget(self::CACHE_USER_QUEUE_PREFIX . $operation->user_id); |
| | | } |
| | | |
| | | // Batch invalidate caches at the end |
| | |
| | | */ |
| | | public function isUserQueueModified(int $user_id, int $since_timestamp): bool |
| | | { |
| | | return $this->cache::getTimestamp("user_{$user_id}") > $since_timestamp; |
| | | return $this->cache::lastModified("user_{$user_id}") > $since_timestamp; |
| | | } |
| | | protected function invalidateUserQueue(int $user_id): void |
| | | { |
| | |
| | | // 1. Updates HTTP timestamp for user_{$user_id} |
| | | // 2. Flushes user-specific caches |
| | | // 3. Triggers connected cache invalidation |
| | | CacheManager::invalidateAll("user_{$user_id}"); |
| | | Cache::for($user_id)->flush(); |
| | | } |
| | | |
| | | /** |
| | |
| | | $keys = $cacheKeys[$scope] ?? $cacheKeys['all']; |
| | | |
| | | foreach ($keys as $key) { |
| | | $this->cache->delete($key); |
| | | $this->cache->forget($key); |
| | | } |
| | | |
| | | $this->cache->touch(); |
| | |
| | | ); |
| | | |
| | | // Log this as a critical error |
| | | JVB()->error()->log( |
| | | Errors::getInstance()->log( |
| | | '[OperationQueue]:restartStuckOperations', |
| | | "Operation failed after maximum retries", |
| | | [ |
| | |
| | | ); |
| | | |
| | | // Log the backoff |
| | | JVB()->error()->log( |
| | | Errors::getInstance()->log( |
| | | '[OperationQueue]:restartStuckOperations', |
| | | "Operation requeued with exponential backoff", |
| | | [ |
| | |
| | | |
| | | $message .= "Please check the error logs for more details."; |
| | | |
| | | return JVB()->email()->sendEmail($admin_email, $subject, $message); |
| | | return Emails::getInstance()->sendEmail($admin_email, $subject, $message); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (!is_array($filterResult) || !isset($filterResult['success'])) { |
| | | // Log invalid filter response and quit early |
| | | $invalid_response = is_object($filterResult) ? get_class($filterResult) : gettype($filterResult); |
| | | JVB()->error()->log( |
| | | Errors::getInstance()->log( |
| | | '[OperationQueue]:processOperation', |
| | | "Invalid filter response format - expected array with 'success' key", |
| | | [ |
| | |
| | | |
| | | } catch (Exception $e) { |
| | | // Handle exception... |
| | | JVB()->error()->log( |
| | | Errors::getInstance()->log( |
| | | '[OperationQueue]:processOperation', |
| | | "Exception during operation processing: " . $e->getMessage(), |
| | | [ |
| | |
| | | protected function updateUserQueueTimestamp(int $user_id) |
| | | { |
| | | |
| | | CacheManager::updateTimestamp("user_{$user_id}"); |
| | | Cache::touch("user_{$user_id}"); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | $success_rate = round(($total_success / max(1, $total_ops)) * 100, 1); |
| | | |
| | | $message = JVB()->email()->h1('Daily Queue Performance Report'); |
| | | $message = Emails::getInstance()->h1('Daily Queue Performance Report'); |
| | | $message .= sprintf('<p>Report for <strong>%s</strong></p>', $yesterday); |
| | | |
| | | // Summary stats in grid |
| | | $stats = [ |
| | | JVB()->email()->stat($total_ops, 'Operations'), |
| | | JVB()->email()->stat($total_success, 'Successful', '✓'), |
| | | JVB()->email()->stat($total_failed, 'Failed', $total_failed > 0 ? '⚠' : ''), |
| | | JVB()->email()->stat($success_rate . '%', 'Success Rate') |
| | | Emails::getInstance()->stat($total_ops, 'Operations'), |
| | | Emails::getInstance()->stat($total_success, 'Successful', '✓'), |
| | | Emails::getInstance()->stat($total_failed, 'Failed', $total_failed > 0 ? '⚠' : ''), |
| | | Emails::getInstance()->stat($success_rate . '%', 'Success Rate') |
| | | ]; |
| | | $message .= JVB()->email()->grid($stats, 4); |
| | | $message .= Emails::getInstance()->grid($stats, 4); |
| | | |
| | | $message .= JVB()->email()->spacer(20); |
| | | $message .= Emails::getInstance()->spacer(20); |
| | | |
| | | // Alert if success rate is low |
| | | if ($success_rate < 90) { |
| | | $message .= JVB()->email()->alert( |
| | | $message .= Emails::getInstance()->alert( |
| | | sprintf('Success rate of %s%% is below the 90%% threshold', $success_rate), |
| | | 'warning' |
| | | ); |
| | | } |
| | | |
| | | $message .= JVB()->email()->h2('Details by Operation Type'); |
| | | $message .= Emails::getInstance()->h2('Details by Operation Type'); |
| | | |
| | | // Details for each operation type |
| | | foreach ($metrics as $metric) { |
| | | $details = []; |
| | | $details[] = ['label' => 'Total', 'value' => $metric->total_operations]; |
| | | $details[] = ['label' => 'Success', 'value' => JVB()->email()->badge($metric->successful_operations, 'success')]; |
| | | $details[] = ['label' => 'Failed', 'value' => $metric->failed_operations > 0 ? JVB()->email()->badge($metric->failed_operations, 'error') : '0']; |
| | | $details[] = ['label' => 'Success', 'value' => Emails::getInstance()->badge($metric->successful_operations, 'success')]; |
| | | $details[] = ['label' => 'Failed', 'value' => $metric->failed_operations > 0 ? Emails::getInstance()->badge($metric->failed_operations, 'error') : '0']; |
| | | |
| | | if ($metric->average_duration) { |
| | | $details[] = ['label' => 'Avg Duration', 'value' => round($metric->average_duration, 2) . 's']; |
| | |
| | | $details[] = ['label' => 'Peak Memory', 'value' => $memory_mb . ' MB']; |
| | | } |
| | | |
| | | $message .= JVB()->email()->card( |
| | | JVB()->email()->table($details), |
| | | $message .= Emails::getInstance()->card( |
| | | Emails::getInstance()->table($details), |
| | | esc_html($metric->type) |
| | | ); |
| | | } |
| | |
| | | // Current queue status |
| | | $pending_count = $this->getCurrentQueueSize(); |
| | | if ($pending_count > 0) { |
| | | $message .= JVB()->email()->spacer(20); |
| | | $message .= JVB()->email()->notice( |
| | | $message .= Emails::getInstance()->spacer(20); |
| | | $message .= Emails::getInstance()->notice( |
| | | sprintf('<strong>Current Queue:</strong> %d operations pending', $pending_count) |
| | | ); |
| | | } |
| | | |
| | | $message .= JVB()->email()->spacer(20); |
| | | $message .= JVB()->email()->button(admin_url('admin.php?page=jvb-queue'), 'View Queue Dashboard'); |
| | | $message .= Emails::getInstance()->spacer(20); |
| | | $message .= Emails::getInstance()->button(admin_url('admin.php?page=jvb-queue'), 'View Queue Dashboard'); |
| | | |
| | | JVB()->email()->sendEmail($admin_email, $subject, $message, 'QUEUE REPORT'); |
| | | Emails::getInstance()->sendEmail($admin_email, $subject, $message, 'QUEUE REPORT'); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return $value; |
| | | } catch (Exception $e) { |
| | | JVB()->error()->log( |
| | | Errors::getInstance()->log( |
| | | '[OperationQueue]:getOperationValue', |
| | | 'Error retrieving operation value: ' . $e->getMessage(), |
| | | [ |