mirror of
https://github.com/archtechx/jobpipeline.git
synced 2025-12-12 17:14:04 +00:00
fix: just handle failures in closures correctly
This commit is contained in:
parent
ebeef52b4b
commit
009c814831
2 changed files with 15 additions and 35 deletions
|
|
@ -67,7 +67,7 @@ class JobPipeline implements ShouldQueue
|
|||
}
|
||||
|
||||
try {
|
||||
$result = app()->call($job, $this->passable);
|
||||
$result = app()->call($job);
|
||||
} catch (Throwable $exception) {
|
||||
if (is_array($job) && method_exists(get_class($job[0]), 'failed')) {
|
||||
call_user_func_array([$job[0], 'failed'], [$exception]);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Stancl\JobPipeline\Tests;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
|
@ -169,37 +170,27 @@ class JobPipelineTest extends TestCase
|
|||
}
|
||||
|
||||
/** @test */
|
||||
public function send_can_pass_parameters_to_method()
|
||||
public function failures_in_closures_will_throw_correctly()
|
||||
{
|
||||
$passes = true;
|
||||
|
||||
Event::listen(TestEvent::class, JobPipeline::make([
|
||||
[new FooJobWithMethod(), 'foo']
|
||||
])->send(function () {
|
||||
function () use (&$passes) {
|
||||
try {
|
||||
throw new Exception('foobar');
|
||||
} catch (Exception) {
|
||||
$passes = false;
|
||||
}
|
||||
}
|
||||
])->send(function (TestEvent $event) {
|
||||
return $this->valuestore;
|
||||
})->toListener());
|
||||
|
||||
$this->assertFalse($this->valuestore->has('foo'));
|
||||
|
||||
event(new TestEvent(new TestModel()));
|
||||
|
||||
$this->assertSame('bar', $this->valuestore->get('foo'));
|
||||
}
|
||||
sleep(1);
|
||||
|
||||
/** @test */
|
||||
public function send_can_pass_parameters_to_method_with_at_syntax()
|
||||
{
|
||||
app()->bind('FooJobWithMethod', fn () => new FooJobWithMethod());
|
||||
|
||||
Event::listen(TestEvent::class, JobPipeline::make([
|
||||
'FooJobWithMethod@foo'
|
||||
])->send(function () {
|
||||
return $this->valuestore;
|
||||
})->toListener());
|
||||
|
||||
$this->assertFalse($this->valuestore->has('foo'));
|
||||
|
||||
event(new TestEvent(new TestModel()));
|
||||
|
||||
$this->assertSame('bar', $this->valuestore->get('foo'));
|
||||
$this->assertFalse($passes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -321,14 +312,3 @@ class ExceptionJob
|
|||
$this->valuestore->put('exeception', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
class FooJobWithMethod
|
||||
{
|
||||
// variadic arguments must be used so the container doesn't try to inject any value
|
||||
public function foo(...$arguments)
|
||||
{
|
||||
[$valuestore] = $arguments;
|
||||
|
||||
$valuestore->put('foo', 'bar');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue