mirror of
https://github.com/archtechx/jobpipeline.git
synced 2025-12-14 10:44:04 +00:00
Merge pull request #9 from hackerESQ/patch-1
adds error catching to handle method
This commit is contained in:
commit
cd2c5c39ad
2 changed files with 49 additions and 1 deletions
|
|
@ -6,6 +6,8 @@ namespace Stancl\JobPipeline;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class JobPipeline implements ShouldQueue
|
class JobPipeline implements ShouldQueue
|
||||||
{
|
{
|
||||||
|
|
@ -63,7 +65,17 @@ class JobPipeline implements ShouldQueue
|
||||||
$job = [new $job(...$this->passable), 'handle'];
|
$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) {
|
if ($result === false) {
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,22 @@ 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 */
|
||||||
|
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 */
|
/** @test */
|
||||||
public function closures_can_be_used_as_jobs()
|
public function closures_can_be_used_as_jobs()
|
||||||
|
|
@ -251,3 +267,23 @@ class FalseJob
|
||||||
return false;
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue