From ea49e13332c47d8523736a7029024c82abf600f3 Mon Sep 17 00:00:00 2001 From: Stefano Giraldo Date: Mon, 16 Jun 2025 10:03:19 +0200 Subject: [PATCH] feat: manage specific queue connection in config --- config/one-time-operations.php | 4 ++++ phpunit.xml | 1 + src/Commands/OneTimeOperationsProcessCommand.php | 4 +++- src/OneTimeOperationManager.php | 5 +++++ tests/Feature/OneTimeOperationCase.php | 4 ++++ tests/Feature/OneTimeOperationCommandTest.php | 11 ++++++----- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/config/one-time-operations.php b/config/one-time-operations.php index 492a611..858d108 100644 --- a/config/one-time-operations.php +++ b/config/one-time-operations.php @@ -11,4 +11,8 @@ // Database Connection Name - Change the model connection, support for Multitenancy // Only change when you want to deviate from your system default repository 'connection' => null, + + // Queue Connection Name + // Only change when you want to use a specific queue connection different from default + 'queue_connection' => env('ONE_TIME_OPERATIONS_QUEUE_CONNECTION', env('QUEUE_CONNECTION', 'sync')), ]; diff --git a/phpunit.xml b/phpunit.xml index c8b2167..5dbabc2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,5 +16,6 @@ + diff --git a/src/Commands/OneTimeOperationsProcessCommand.php b/src/Commands/OneTimeOperationsProcessCommand.php index ee26589..d022cfc 100644 --- a/src/Commands/OneTimeOperationsProcessCommand.php +++ b/src/Commands/OneTimeOperationsProcessCommand.php @@ -161,7 +161,9 @@ protected function storeOperation(OneTimeOperationFile $operationFile): void protected function dispatchOperationJob(OneTimeOperationFile $operationFile) { if ($this->isAsyncMode($operationFile)) { - OneTimeOperationProcessJob::dispatch($operationFile->getOperationName())->onQueue($this->getQueue($operationFile)); + OneTimeOperationProcessJob::dispatch($operationFile->getOperationName()) + ->onQueue($this->getQueue($operationFile)) + ->onConnection(OneTimeOperationManager::getConnectionName()); return; } diff --git a/src/OneTimeOperationManager.php b/src/OneTimeOperationManager.php index 106eac4..d0241cc 100644 --- a/src/OneTimeOperationManager.php +++ b/src/OneTimeOperationManager.php @@ -108,6 +108,11 @@ public static function getDirectoryName(): string return Config::get('one-time-operations.directory'); } + public static function getConnectionName(): string + { + return Config::get('one-time-operations.queue_connection'); + } + public static function getDirectoryPath(): string { return App::basePath(Str::of(self::getDirectoryName())->rtrim('/')).DIRECTORY_SEPARATOR; diff --git a/tests/Feature/OneTimeOperationCase.php b/tests/Feature/OneTimeOperationCase.php index 5a515fe..33276ec 100644 --- a/tests/Feature/OneTimeOperationCase.php +++ b/tests/Feature/OneTimeOperationCase.php @@ -3,6 +3,7 @@ namespace TimoKoerber\LaravelOneTimeOperations\Tests\Feature; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Queue; use Orchestra\Testbench\TestCase; @@ -24,6 +25,9 @@ protected function setUp(): void Queue::fake(); Carbon::setTestNow(self::TEST_DATETIME); + + Config::set('queue.default', 'default'); + } protected function getPackageProviders($app): array diff --git a/tests/Feature/OneTimeOperationCommandTest.php b/tests/Feature/OneTimeOperationCommandTest.php index e634704..6d9b8b3 100644 --- a/tests/Feature/OneTimeOperationCommandTest.php +++ b/tests/Feature/OneTimeOperationCommandTest.php @@ -3,6 +3,7 @@ namespace TimoKoerber\LaravelOneTimeOperations\Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Queue; use Illuminate\Support\Str; @@ -113,7 +114,7 @@ public function test_the_whole_command_process() // operation was exectued - database entry and job was created $this->assertEquals(1, Operation::count()); Queue::assertPushed(OneTimeOperationProcessJob::class, function (OneTimeOperationProcessJob $job) { - return $job->connection === null; // async + return $job->connection === Config::get('one-time-operations.queue_connection'); // async }); // entry was created successfully @@ -221,7 +222,7 @@ public function test_sync_processing_with_file_attribute() // Job was executed asynchronously Queue::assertPushed(OneTimeOperationProcessJob::class, function (OneTimeOperationProcessJob $job) { return $job->operationName === '2015_10_21_072800_foo_bar_operation' - && $job->connection === null // async + && $job->connection === Config::get('one-time-operations.queue_connection') // async && $job->queue === 'default'; // default queue }); @@ -237,7 +238,7 @@ public function test_sync_processing_with_file_attribute() // Job was executed asynchronously on queue "foobar" Queue::assertPushed(OneTimeOperationProcessJob::class, function (OneTimeOperationProcessJob $job) { return $job->operationName === '2015_10_21_072800_foo_bar_operation' - && $job->connection === null // async + && $job->connection === Config::get('one-time-operations.queue_connection') // async && $job->queue === 'foobar'; // default queue }); } @@ -258,7 +259,7 @@ public function test_processing_with_queue() // Job was executed synchronously Queue::assertPushed(OneTimeOperationProcessJob::class, function (OneTimeOperationProcessJob $job) { return $job->operationName === '2015_10_21_072800_foo_bar_operation' - && $job->connection === null // async + && $job->connection === Config::get('one-time-operations.queue_connection') // async && $job->queue === 'narfpuit'; // queue narfpuit }); @@ -270,7 +271,7 @@ public function test_processing_with_queue() // Job was executed asynchronously on queue "foobar" Queue::assertPushed(OneTimeOperationProcessJob::class, function (OneTimeOperationProcessJob $job) { return $job->operationName === '2015_10_21_072800_foo_bar_operation' - && $job->connection === null // async + && $job->connection === Config::get('one-time-operations.queue_connection') // async && $job->queue === 'foobar'; // queue foobar }); }