Skip to content

Commit 81759c8

Browse files
committed
Try catch.
1 parent 2c5324f commit 81759c8

File tree

6 files changed

+125
-12
lines changed

6 files changed

+125
-12
lines changed

src/Models/MailatorSchedule.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
use Carbon\Carbon;
1414
use Carbon\CarbonInterface;
1515
use Closure;
16+
use Exception;
1617
use Illuminate\Contracts\Mail\Mailable;
1718
use Illuminate\Database\Eloquent\Model;
1819
use Illuminate\Support\Arr;
1920
use Illuminate\Support\Facades\Validator;
2021
use Illuminate\Support\Str;
2122
use Opis\Closure\SerializableClosure;
23+
use Throwable;
2224

2325
/**
2426
* Class MailatorSchedule.
@@ -324,27 +326,33 @@ public function logs()
324326

325327
public function shouldSend(): bool
326328
{
327-
$this->load('logs');
328-
329-
return $this->configurationsPasses() && $this->whenPasses() && $this->eventsPasses();
329+
try {
330+
$this->load('logs');
331+
return $this->configurationsPasses() && $this->whenPasses() && $this->eventsPasses();
332+
} catch (Exception|Throwable) {
333+
return false;
334+
}
330335
}
331336

332337
public function execute(bool $now = false): void
333338
{
334339
$this->save();
335340

336-
if ($this->hasCustomAction()) {
337-
unserialize($this->action)->handle($this);
341+
try {
342+
if ($this->hasCustomAction()) {
343+
unserialize($this->action)->handle($this);
338344

339-
$this->markAsSent();
345+
$this->markAsSent();
340346

341-
static::garbageResolver()->handle($this);
342-
} else {
343-
if ($now) {
344-
dispatch_sync(new SendMailJob($this));
347+
static::garbageResolver()->handle($this);
345348
} else {
346-
dispatch(new SendMailJob($this));
349+
if ($now) {
350+
dispatch_sync(new SendMailJob($this));
351+
} else {
352+
dispatch(new SendMailJob($this));
353+
}
347354
}
355+
} catch (Exception|Throwable) {
348356
}
349357
}
350358

tests/Feature/AfterConstraintTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@ public function test_past_target_with_after_now_passed_after_constraint_hourly_b
5858

5959
$scheduler->save();
6060

61-
$this->travel(3)->hours();
61+
$this->travel(1)->hours();
62+
63+
$can = app(
64+
AfterConstraint::class
65+
)->canSend(
66+
$scheduler,
67+
$scheduler->logs
68+
);
69+
70+
self::assertTrue(
71+
$scheduler->fresh()->isFutureAction()
72+
);
73+
74+
self::assertTrue(
75+
$can
76+
);
77+
78+
$this->travel(1)->hours();
6279

6380
$can = app(
6481
AfterConstraint::class
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Feature\Console;
4+
5+
use Binarcode\LaravelMailator\Console\Commands\MailatorSchedulerCommand;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Binarcode\LaravelMailator\Tests\Fixtures\Actions\NoopAction;
8+
use Binarcode\LaravelMailator\Tests\Fixtures\Constraints\FailingConstraint;
9+
use Binarcode\LaravelMailator\Tests\Fixtures\Constraints\TrueConstraint;
10+
use Binarcode\LaravelMailator\Tests\Fixtures\CustomAction;
11+
use Binarcode\LaravelMailator\Tests\Fixtures\User;
12+
use Binarcode\LaravelMailator\Tests\TestCase;
13+
use Illuminate\Foundation\Testing\RefreshDatabase;
14+
15+
class MailatorScheduleCommandTest extends TestCase
16+
{
17+
use RefreshDatabase;
18+
19+
public function test_failing_during_run_dont_stop_execution(): void
20+
{
21+
MailatorSchedule::init('A')
22+
->constraint(new FailingConstraint)
23+
->actionClass(new NoopAction)
24+
->save();
25+
26+
MailatorSchedule::init('B')
27+
->constraint(new TrueConstraint)
28+
->actionClass(new CustomAction(
29+
$user = User::factory()->create([
30+
'email_verified_at' => null,
31+
])
32+
)
33+
)
34+
->save();
35+
36+
37+
$this->artisan(
38+
MailatorSchedulerCommand::class
39+
);
40+
41+
self::assertTrue($user->fresh()->hasVerifiedEmail());
42+
43+
}
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Fixtures\Actions;
4+
5+
use Binarcode\LaravelMailator\Actions\Action;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
8+
class NoopAction implements Action
9+
{
10+
public function handle(MailatorSchedule $schedule)
11+
{
12+
//
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Fixtures\Constraints;
4+
5+
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Illuminate\Support\Collection;
8+
9+
class FailingConstraint implements SendScheduleConstraint
10+
{
11+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
12+
{
13+
abort(403, 'Some failing.');
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Tests\Fixtures\Constraints;
4+
5+
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Illuminate\Support\Collection;
8+
9+
class TrueConstraint implements SendScheduleConstraint
10+
{
11+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
12+
{
13+
return true;
14+
}
15+
}

0 commit comments

Comments
 (0)