From 80947a4889fa348f0d319695a9bd8b3ca5e1f430 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Thu, 17 Mar 2022 19:10:30 -0500 Subject: [PATCH 1/3] adds error catching to handle method --- src/JobPipeline.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/JobPipeline.php b/src/JobPipeline.php index 0ceaa17..bf53218 100644 --- a/src/JobPipeline.php +++ b/src/JobPipeline.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\JobPipeline; use Closure; +use Throwable; use Illuminate\Contracts\Queue\ShouldQueue; class JobPipeline implements ShouldQueue @@ -63,7 +64,18 @@ class JobPipeline implements ShouldQueue $job = [new $job(...$this->passable), 'handle']; } - $result = app()->call($job); + try { + $result = app()->call($job); + } catch(Throwable $exception) { + if ( method_exists(get_class($job[0]), 'failed') ) { + + call_user_func_array([$job[0], 'failed'], [$exception]); + } else { + Log::error($exception); + } + + break; + } if ($result === false) { break; From d636ab5fe31eca3c8d008f4085b210fbfe325cf7 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Fri, 18 Mar 2022 13:44:18 -0500 Subject: [PATCH 2/3] fix style --- src/JobPipeline.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/JobPipeline.php b/src/JobPipeline.php index bf53218..526b87c 100644 --- a/src/JobPipeline.php +++ b/src/JobPipeline.php @@ -5,8 +5,9 @@ declare(strict_types=1); namespace Stancl\JobPipeline; use Closure; -use Throwable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Support\Facades\Log; +use Throwable; class JobPipeline implements ShouldQueue { @@ -66,9 +67,8 @@ class JobPipeline implements ShouldQueue try { $result = app()->call($job); - } catch(Throwable $exception) { - if ( method_exists(get_class($job[0]), 'failed') ) { - + } catch (Throwable $exception) { + if (method_exists(get_class($job[0]), 'failed')) { call_user_func_array([$job[0], 'failed'], [$exception]); } else { Log::error($exception); From d7f2f027f7610e636e2a2feee793f9fae46681a8 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Sun, 20 Mar 2022 14:10:10 -0500 Subject: [PATCH 3/3] adds tests --- tests/JobPipelineTest.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/JobPipelineTest.php b/tests/JobPipelineTest.php index 0327660..6331770 100644 --- a/tests/JobPipelineTest.php +++ b/tests/JobPipelineTest.php @@ -133,6 +133,22 @@ 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() + { + Event::listen(TestEvent::class, JobPipeline::make([ + ExceptionJob::class, + ])->send(function () { + return $this->valuestore; + })->toListener()); + + event(new TestEvent(new TestModel())); + + sleep(1); + + $this->assertEquals($this->valuestore->get('exeception'), 'pipeline exception'); + } /** @test */ public function closures_can_be_used_as_jobs() @@ -251,3 +267,23 @@ class FalseJob return false; } } + +class ExceptionJob +{ + protected $valuestore; + + public function __construct(Valuestore $valuestore) + { + $this->valuestore = $valuestore; + } + + public function handle() + { + throw new \Exception('pipeline exception', 1); + } + + public function failed(\Throwable $e) + { + $this->valuestore->put('exeception', $e->getMessage()); + } +}