Skip to content

Commit

Permalink
Merge pull request #9 from hackerESQ/patch-1
Browse files Browse the repository at this point in the history
adds error catching to handle method
  • Loading branch information
stancl authored Mar 20, 2022
2 parents 15b38fb + d7f2f02 commit cd2c5c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/JobPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Closure;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Throwable;

class JobPipeline implements ShouldQueue
{
Expand Down Expand Up @@ -63,7 +65,17 @@ public function handle(): void
$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;
Expand Down
36 changes: 36 additions & 0 deletions tests/JobPipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ public function the_pipeline_can_be_canceled_by_returning_false_from_any_job()
// 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()
Expand Down Expand Up @@ -251,3 +267,23 @@ public function handle()
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());
}
}

0 comments on commit cd2c5c3

Please sign in to comment.