Skip to content

Commit

Permalink
Laravel Inbox Pattern - ShipSaaS
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsandaru committed Aug 20, 2023
1 parent 5438278 commit 20b5902
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Core/Lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function on(LifecycleEventEnum $event, callable $handler): void
$this->listeners[$event->value][] = $handler;
}

/**
* @note this will run the closing/closed callbacks, so be highly aware
*/
public function forceClose(): void
{
$this->signalHandler();
}

public function initLifecycle(Signals $signal): void
{
if ($this->isInitialized) {
Expand Down
4 changes: 3 additions & 1 deletion src/Handlers/InboxMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public function process(int $limit = 10): int
$processed++;
} catch (Throwable $e) {
// something really bad happens, we need to stop the process
Log::info('Failed to process inbox message', [
Log::error('Failed to process inbox message', [
'error' => [
'msg' => $e->getMessage(),
'traces' => $e->getTrace(),
]
]);

$this->lifecycle->forceClose();

throw $e;
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Commands/InboxWorkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace ShipSaasInboxProcess\Tests\Integration\Commands;

use Error;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use ShipSaasInboxProcess\InboxProcessSetup;
use ShipSaasInboxProcess\Tests\TestCase;
use Throwable;

class InboxWorkCommandTest extends TestCase
{
Expand Down Expand Up @@ -108,6 +111,30 @@ public function testCommandShouldUnlockTopicAfterStopped()
'topic' => 'testlock',
]);
}

public function testCommandThrowsErrorWhenFailedToProcessAMessage()
{
InboxProcessSetup::addProcessor('with_err_msg', function () {
throw new Error('Cannot process');
});
Log::expects('error')->atLeast()->once();

appendInboxMessage('with_err_msg', '1', []);

$exception = null;
try {
$this->artisan('inbox:work with_err_msg --stop-on-empty');
} catch (Throwable $e) {
$exception = $e;
}

$this->assertNotNull($exception);
$this->assertInstanceOf(Error::class, $exception);

$this->assertDatabaseMissing('running_inboxes', [
'topic' => 'with_err_msg',
]);
}
}

class InvoicePaymentSucceedEvent
Expand Down

0 comments on commit 20b5902

Please sign in to comment.