From a9654f00c8fbc9934289d761843ddd071025013f Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 28 Aug 2026 17:20:28 +0000
Subject: [PATCH] =PHPStorm Code Inspection: fixed class reference errors (from getting rid of JVB() and replacing with direct class::getInstance; as well as general code sniffing, etc

---
 inc/managers/queue/Processor.php |   75 +++++++++++++++++--------------------
 1 files changed, 34 insertions(+), 41 deletions(-)

diff --git a/inc/managers/queue/Processor.php b/inc/managers/queue/Processor.php
index ccc4ddb..21e6df4 100644
--- a/inc/managers/queue/Processor.php
+++ b/inc/managers/queue/Processor.php
@@ -1,5 +1,7 @@
 <?php
 namespace JVBase\managers\queue;
+use Throwable;
+
 if (!defined('ABSPATH')) {
 	exit;
 }
@@ -14,48 +16,39 @@
 
 	public function run(): void
 	{
-		if (get_transient(BASE.'queue_running')) {
-			return;
-		}
-		set_transient(BASE.'queue_running', true, 60);
 		if (!$this->hasAdequateResources()) {
 			error_log('[Processor] Insufficient resources to start processing');
 			return;
 		}
 
-		$ops = $this->storage->fetchRunnable();
-		if (empty($ops)) {
-			return;
-		}
-		foreach ($ops as $op) {
-			if ($op->state === 'completed') {
-				return;
+		$op = null;
+		$this->storage->withTransaction(function() use (&$op) {
+			$candidates = $this->storage->fetchRunnable();
+			foreach ($candidates as $candidate) {
+				if ($candidate->state === 'completed') continue;
+				if (!$this->dependenciesSatisfied($candidate)) continue;
+				if ($this->storage->markProcessing($candidate->id)) {
+					$op = $candidate;
+					break;
+				}
 			}
-			if (!$this->dependenciesSatisfied($op)) {
-				continue;
-			}
-			if (!$this->storage->markProcessing($op->id)) {
-				continue;
-			}
-			$this->processOne($op);
-			usleep(10000);
-		}
+		});
+
+		if (!$op) return;
+
+		$this->processOne($op);
+		usleep(10000);
 
 		$this->storage->invalidateQueueCache();
 	}
 
+
 	private function processOne(Operation $op): void
 	{
-		if (get_transient(BASE.$op->id)) {
-			return;
-		}
-		set_transient(BASE.$op->id, true, 500);
 		$progress = new Progress($op);
-
 		$executor = $this->registry->getExecutor($op->type) ?? $this->defaultExecutor;
 		$op->startedAt = current_time('mysql');
 		$op->state = 'processing';
-
 		$this->storage->saveProgress($op);
 
 		try {
@@ -71,7 +64,7 @@
 			$op->result = $result->result;
 			$op->completedAt = current_time('mysql');
 
-		} catch (\Throwable $e) {
+		} catch (Throwable $e) {
 			error_log("[Processor] Exception caught: " . $e->getMessage());
 			$this->handleFailure($op, $e);
 		}
@@ -147,7 +140,7 @@
 					}
 				}
 
-			} catch (\Throwable $e) {
+			} catch (Throwable $e) {
 				error_log("[Processor] Chunk {$index} failed: " . $e->getMessage());
 
 				// Record failed items from this chunk
@@ -208,7 +201,7 @@
 		return $chunks;
 	}
 
-	private function handleFailure(Operation $op, \Throwable $e): void
+	private function handleFailure(Operation $op, Throwable $e): void
 	{
 		$hash = md5($e->getMessage());
 
@@ -227,17 +220,17 @@
 
 		$op->errorMessage = $e->getMessage();
 
-		JVB()->error()->log(
-			'[Queue]:processOne',
-			$e->getMessage(),
-			[
-				'operation_id' => $op->id,
-				'type'         => $op->type,
-				'user_id'      => $op->userId,
-				'retries'      => $op->retries,
-			],
-			$op->outcome === 'failed_permanent' ? 'critical' : 'warning'
-		);
+//		JVB()->error()->log(
+//			'[Queue]:processOne',
+//			$e->getMessage(),
+//			[
+//				'operation_id' => $op->id,
+//				'type'         => $op->type,
+//				'user_id'      => $op->userId,
+//				'retries'      => $op->retries,
+//			],
+//			$op->outcome === 'failed_permanent' ? 'critical' : 'warning'
+//		);
 	}
 
 	private function calculateBackoff(int $attempt): string
@@ -288,7 +281,7 @@
 		};
 	}
 
-	private function hasAdequateResources(): bool
+	public function hasAdequateResources(): bool
 	{
 		// Stricter thresholds for starting (50% memory, 60s minimum time)
 		$memoryLimit = $this->getMemoryLimitBytes();

--
Gitblit v1.10.0