1
0
Fork 0
mirror of https://github.com/archtechx/jobpipeline.git synced 2025-12-12 15:34:04 +00:00

Allow canceling pipelines by returning false from handle()

This commit is contained in:
Samuel Štancl 2020-11-12 19:33:31 +01:00
parent 8fcac74f86
commit 86873515ae
4 changed files with 46 additions and 1 deletions

View file

View file

@ -57,7 +57,11 @@ class JobPipeline implements ShouldQueue
public function handle(): void public function handle(): void
{ {
foreach ($this->jobs as $job) { foreach ($this->jobs as $job) {
app()->call([new $job(...$this->passable), 'handle']); $result = app()->call([new $job(...$this->passable), 'handle']);
if ($result === false) {
break;
}
} }
} }

View file

@ -75,6 +75,8 @@ class JobPipelineTest extends TestCase
event(new TestEvent(new TestModel())); event(new TestEvent(new TestModel()));
$this->artisan('queue:work --once'); $this->artisan('queue:work --once');
sleep(1);
$this->assertSame('bar', $this->valuestore->get('foo')); $this->assertSame('bar', $this->valuestore->get('foo'));
} }
@ -110,6 +112,27 @@ class JobPipelineTest extends TestCase
$this->assertSame(['a', 'b'], app('test_args')); $this->assertSame(['a', 'b'], app('test_args'));
} }
/** @test */
public function the_pipeline_can_be_canceled_by_returning_false_from_any_job()
{
Event::listen(TestEvent::class, JobPipeline::make([
FalseJob::class,
FooJob::class,
])->send(function () {
return $this->valuestore;
})->shouldBeQueued(true)->toListener());
event(new TestEvent(new TestModel()));
$this->artisan('queue:work --once');
sleep(1);
$this->assertTrue($this->valuestore->get('false_job_executed'));
// Foo job is not excuted
$this->assertFalse($this->valuestore->has('foo'));
}
} }
class FooJob class FooJob
@ -193,3 +216,20 @@ class JobWithMultipleArguments
app()->instance('test_args', [$this->first, $this->second]); app()->instance('test_args', [$this->first, $this->second]);
} }
} }
class FalseJob
{
protected $valuestore;
public function __construct(Valuestore $valuestore)
{
$this->valuestore = $valuestore;
}
public function handle()
{
$this->valuestore->put('false_job_executed', true);
return false;
}
}

View file

@ -0,0 +1 @@
{"false_job_executed":true}