mirror of
https://github.com/archtechx/jobpipeline.git
synced 2025-12-12 05:24:03 +00:00
Possibility to change the default configurations for the queue jobs. (#13)
* Add $queue attribute
This code enables me to change the queue name that the JobPipeline will be pushed to.
Now I can do something like this:
```php
JobPipeline::make([
Jobs\CreateDatabase::class,
Jobs\MigrateDatabase::class,
// Jobs\SeedDatabase::class,
// Your own jobs to prepare the tenant.
// Provision API keys, create S3 buckets, anything you want!
])->send(function (Events\TenantCreated $event)
{
return $event->tenant;
})->onQueue('another-queue')
->shouldBeQueued()
```
* Add .idea directory to gitignore
* The following methods where added: onConnection, onQueue, delay, tries, shouldBeQueuedOn. The signature of shouldBeQueued was changed too.
* reuse shouldBeQueuedOn
* updating docs
* added timeout
* docs updated
* .phpunit.cache/ added to gitignore
* tests for shouldQueueOn method
* leave only shouldBeQueued method
* update README
* remove unnecessary property declarations
* Delete jobpipelinetest.json
* add jobpipelinetest.json as a empty file
* update readme
* simplify tests
* improve code
* improve readme
---------
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
bfc53f6dd6
commit
d7a9e6796e
4 changed files with 58 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,3 +2,5 @@
|
||||||
composer.lock
|
composer.lock
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
.php-cs-fixer.cache
|
.php-cs-fixer.cache
|
||||||
|
.idea/
|
||||||
|
.phpunit.cache/
|
||||||
|
|
|
||||||
20
README.md
20
README.md
|
|
@ -66,6 +66,26 @@ JobPipeline::make([
|
||||||
})->shouldBeQueued(true)
|
})->shouldBeQueued(true)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you wish to push the job to a different queue, you can pass a string as the second parameter:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
|
||||||
|
|
||||||
|
JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
SeedDatabase::class,
|
||||||
|
])->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:
|
Finally, convert the pipeline to a listener and bind it to an event:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,10 @@ class JobPipeline implements ShouldQueue
|
||||||
public $passable;
|
public $passable;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $shouldBeQueued;
|
public bool $shouldBeQueued;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
public string $queue;
|
||||||
|
|
||||||
public function __construct($jobs, callable $send = null, bool $shouldBeQueued = null)
|
public function __construct($jobs, callable $send = null, bool $shouldBeQueued = null)
|
||||||
{
|
{
|
||||||
|
|
@ -50,10 +53,14 @@ class JobPipeline implements ShouldQueue
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shouldBeQueued(bool $shouldBeQueued = true)
|
public function shouldBeQueued(bool $shouldBeQueued = true, string $queue = null)
|
||||||
{
|
{
|
||||||
$this->shouldBeQueued = $shouldBeQueued;
|
$this->shouldBeQueued = $shouldBeQueued;
|
||||||
|
|
||||||
|
if ($queue) {
|
||||||
|
$this->queue = $queue;
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,32 @@ class JobPipelineTest extends TestCase
|
||||||
$this->assertSame('bar', $this->valuestore->get('foo'));
|
$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 */
|
/** @test */
|
||||||
public function job_pipeline_executes_jobs_and_passes_the_object_sequentially()
|
public function job_pipeline_executes_jobs_and_passes_the_object_sequentially()
|
||||||
{
|
{
|
||||||
|
|
@ -133,7 +159,7 @@ class JobPipelineTest extends TestCase
|
||||||
// Foo job is not excuted
|
// Foo job is not excuted
|
||||||
$this->assertFalse($this->valuestore->has('foo'));
|
$this->assertFalse($this->valuestore->has('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function the_pipeline_can_execute_failed_method_on_job()
|
public function the_pipeline_can_execute_failed_method_on_job()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue