From 5a54c0dc1a47c981cab2ba726f591c620dbf7195 Mon Sep 17 00:00:00 2001 From: DatPM Date: Fri, 11 Jul 2025 09:58:32 +0700 Subject: [PATCH 1/2] Skips auth context restore for sync queues Avoids unnecessary auth context restoration and payload modifications when using synchronous queues. This prevents potential issues and overhead in scenarios where jobs are executed immediately without serialization. Specifically: - It prevents adding extra payload for sync queues when creating a queue payload. - It skips restoring auth context when handling a job from a sync queue. --- src/LaravelAuthQueueServiceProvider.php | 7 +++++++ src/Middlewares/RestoreAuthenticatedContextMiddleware.php | 6 ++++++ tests/AuthContextQueueListenerTest.php | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/LaravelAuthQueueServiceProvider.php b/src/LaravelAuthQueueServiceProvider.php index 053ecf1..adb7406 100644 --- a/src/LaravelAuthQueueServiceProvider.php +++ b/src/LaravelAuthQueueServiceProvider.php @@ -3,17 +3,24 @@ namespace DatPM\LaravelAuthQueue; use Illuminate\Queue\Queue; +use Illuminate\Queue\SyncQueue; use Illuminate\Support\Facades\Auth; use Illuminate\Support\ServiceProvider; use DatPM\LaravelAuthQueue\Guards\KernelGuard; use Illuminate\Contracts\Database\ModelIdentifier; use DatPM\LaravelAuthQueue\Traits\WasAuthenticated; +use Illuminate\Support\Facades\Queue as QueueManager; class LaravelAuthQueueServiceProvider extends ServiceProvider { public function boot() { Queue::createPayloadUsing(function ($connectionName, $queue, $payload) { + // Ignore adding extra payload for Sync Queue + if (QueueManager::connection($connectionName) instanceof SyncQueue) { + return []; + } + // Skip attaching authUser when the job does not use WasAuthenticated Trait if (! in_array(WasAuthenticated::class, class_uses_recursive($payload['displayName']))) { return []; diff --git a/src/Middlewares/RestoreAuthenticatedContextMiddleware.php b/src/Middlewares/RestoreAuthenticatedContextMiddleware.php index adddc9c..a2cec7c 100644 --- a/src/Middlewares/RestoreAuthenticatedContextMiddleware.php +++ b/src/Middlewares/RestoreAuthenticatedContextMiddleware.php @@ -2,6 +2,7 @@ namespace DatPM\LaravelAuthQueue\Middlewares; +use Illuminate\Queue\Jobs\SyncJob; use Illuminate\Support\Facades\Auth; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Auth\Authenticatable; @@ -12,6 +13,11 @@ class RestoreAuthenticatedContextMiddleware public function handle($command, callable $next) { + // If the job is being handled in sync, skip restore logic + if ($command->job instanceof SyncJob) { + return $next($command); + } + Auth::shouldUse('kernel'); $guard = auth(); diff --git a/tests/AuthContextQueueListenerTest.php b/tests/AuthContextQueueListenerTest.php index f8f4f07..b36e0d8 100644 --- a/tests/AuthContextQueueListenerTest.php +++ b/tests/AuthContextQueueListenerTest.php @@ -56,7 +56,7 @@ }); it('preserves auth context when Listener is executed', function () { - Queue::setDefaultDriver('database'); + Queue::setDefaultDriver('sync'); /** @var \Mockery\Mock $loggerSpy */ $loggerSpy = Mockery::spy('logger'); From 3586ce691013ef69f6002c6742a298d3bf04174a Mon Sep 17 00:00:00 2001 From: DatPM Date: Mon, 21 Jul 2025 22:43:48 +0700 Subject: [PATCH 2/2] Revert Wrong Change --- tests/AuthContextQueueListenerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AuthContextQueueListenerTest.php b/tests/AuthContextQueueListenerTest.php index b36e0d8..f8f4f07 100644 --- a/tests/AuthContextQueueListenerTest.php +++ b/tests/AuthContextQueueListenerTest.php @@ -56,7 +56,7 @@ }); it('preserves auth context when Listener is executed', function () { - Queue::setDefaultDriver('sync'); + Queue::setDefaultDriver('database'); /** @var \Mockery\Mock $loggerSpy */ $loggerSpy = Mockery::spy('logger');