diff --git a/.gitignore b/.gitignore index 7b37592..27f6517 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ composer.lock .phpunit.result.cache .php-cs-fixer.cache +.idea/ +.phpunit.cache/ diff --git a/README.md b/README.md index f40284c..c3dfc17 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,26 @@ JobPipeline::make([ })->shouldBeQueued(true) ``` +If you wish to push the job to a different queue, you can pass a string as the second parameter: + +```php +send(function (TenantCreated $event) { + return $event->tenant; +})->shouldBeQueued(true, 'another-queue'); +``` + +This can be simplified by calling `shouldBeQueued(queue: 'another-queue')` since the first parameter defaults to `true`. + Finally, convert the pipeline to a listener and bind it to an event: ```php diff --git a/src/JobPipeline.php b/src/JobPipeline.php index a3f7bc3..de597ea 100644 --- a/src/JobPipeline.php +++ b/src/JobPipeline.php @@ -25,7 +25,10 @@ class JobPipeline implements ShouldQueue public $passable; /** @var bool */ - public $shouldBeQueued; + public bool $shouldBeQueued; + + /** @var string */ + public string $queue; public function __construct($jobs, callable $send = null, bool $shouldBeQueued = null) { @@ -50,10 +53,14 @@ class JobPipeline implements ShouldQueue return $this; } - public function shouldBeQueued(bool $shouldBeQueued = true) + public function shouldBeQueued(bool $shouldBeQueued = true, string $queue = null) { $this->shouldBeQueued = $shouldBeQueued; + if ($queue) { + $this->queue = $queue; + } + return $this; } diff --git a/tests/JobPipelineTest.php b/tests/JobPipelineTest.php index 6331770..bf8d1a5 100644 --- a/tests/JobPipelineTest.php +++ b/tests/JobPipelineTest.php @@ -80,6 +80,32 @@ class JobPipelineTest extends TestCase $this->assertSame('bar', $this->valuestore->get('foo')); } + /** @test */ + public function job_pipelines_can_use_a_specific_queue() + { + Event::listen(TestEvent::class, JobPipeline::make([ + FooJob::class, + ])->send(function () { + return $this->valuestore; + })->shouldBeQueued(queue: 'another')->toListener()); + + $this->assertFalse($this->valuestore->has('foo')); + event(new TestEvent(new TestModel())); + + $this->artisan('queue:work --once --queue=default'); + + sleep(1); + + // Job hasn't run since it was pushed to the 'another' queue + $this->assertNull($this->valuestore->get('foo')); + + $this->artisan('queue:work --once --queue=another'); + + sleep(1); + + $this->assertSame('bar', $this->valuestore->get('foo')); + } + /** @test */ public function job_pipeline_executes_jobs_and_passes_the_object_sequentially() { @@ -133,7 +159,7 @@ class JobPipelineTest extends TestCase // Foo job is not excuted $this->assertFalse($this->valuestore->has('foo')); } - + /** @test */ public function the_pipeline_can_execute_failed_method_on_job() {