-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Amqp self heal on acknowledge failures (#370)
* Self healing on acknowledge failures
- Loading branch information
Showing
15 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ecotone\Amqp; | ||
|
||
use Ecotone\Enqueue\CachedConnectionFactory; | ||
use Ecotone\Enqueue\EnqueueAcknowledgementCallback; | ||
use Ecotone\Enqueue\ReconnectableConnectionFactory; | ||
use Ecotone\Messaging\Endpoint\AcknowledgementCallback; | ||
use Ecotone\Messaging\Handler\Logger\LoggingGateway; | ||
|
||
final class AmqpAcknowledgeCallbackWraper implements AcknowledgementCallback | ||
{ | ||
public function __construct(private EnqueueAcknowledgementCallback $acknowledgementCallback, private CachedConnectionFactory $connectionFactory, private LoggingGateway $loggingGateway) | ||
{ | ||
|
||
} | ||
|
||
public function isAutoAck(): bool | ||
{ | ||
return $this->acknowledgementCallback->isAutoAck(); | ||
} | ||
|
||
public function disableAutoAck(): void | ||
{ | ||
$this->acknowledgementCallback->disableAutoAck(); | ||
} | ||
|
||
public function accept(): void | ||
{ | ||
try { | ||
$this->acknowledgementCallback->accept(); | ||
}catch (\Exception $exception) { | ||
$this->loggingGateway->info("Failed to acknowledge message, disconnecting AMQP Connection in order to self-heal. Failure happen due to: " . $exception->getMessage(), exception: $exception); | ||
|
||
$this->connectionFactory->reconnect(); | ||
} | ||
} | ||
|
||
public function reject(): void | ||
{ | ||
try { | ||
$this->acknowledgementCallback->reject(); | ||
}catch (\Exception $exception) { | ||
$this->loggingGateway->info("Failed to reject message, disconnecting AMQP Connection in order to self-heal. Failure happen due to: " . $exception->getMessage(), exception: $exception); | ||
|
||
$this->connectionFactory->reconnect(); | ||
} | ||
} | ||
|
||
public function requeue(): void | ||
{ | ||
try { | ||
$this->acknowledgementCallback->requeue(); | ||
}catch (\Exception $exception) { | ||
$this->loggingGateway->info("Failed to requeue message, disconnecting AMQP Connection in order to self-heal. Failure happen due to: " . $exception->getMessage(), exception: $exception); | ||
|
||
$this->connectionFactory->reconnect(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/Amqp/tests/Fixture/DistributedCommandBus/Receiver/Event/TicketCreated.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\Amqp\Fixture\DistributedCommandBus\Receiver\Event; | ||
|
||
final class TicketCreated | ||
{ | ||
public function __construct(private string $ticket) | ||
{ | ||
} | ||
|
||
public function getTicket(): string | ||
{ | ||
return $this->ticket; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...mqp/tests/Fixture/DistributedCommandBus/ReceiverEventHandler/TicketNotificationConfig.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\Amqp\Fixture\DistributedCommandBus\ReceiverEventHandler; | ||
|
||
use Ecotone\Amqp\AmqpBackedMessageChannelBuilder; | ||
use Ecotone\Messaging\Attribute\ServiceContext; | ||
|
||
final class TicketNotificationConfig | ||
{ | ||
#[ServiceContext] | ||
public function channel() | ||
{ | ||
return AmqpBackedMessageChannelBuilder::create('async'); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...sts/Fixture/DistributedCommandBus/ReceiverEventHandler/TicketNotificationEventHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\Amqp\Fixture\DistributedCommandBus\ReceiverEventHandler; | ||
|
||
use Ecotone\Messaging\Attribute\Asynchronous; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\QueryHandler; | ||
use Test\Ecotone\Amqp\Fixture\DistributedCommandBus\Receiver\Event\TicketCreated; | ||
|
||
final class TicketNotificationEventHandler | ||
{ | ||
public const GET_TICKETS_NOTIFICATION_COUNT = 'getTicketsNotificationCount'; | ||
|
||
private array $ticketNotifications = []; | ||
|
||
public function __construct(private array $delays = []) | ||
{ | ||
|
||
} | ||
|
||
#[Asynchronous('async')] | ||
#[EventHandler(endpointId: 'notify')] | ||
public function notify(TicketCreated $event): void | ||
{ | ||
$delay = array_shift($this->delays); | ||
if ($delay) { | ||
sleep($delay); | ||
} | ||
|
||
$this->ticketNotifications[] = $event->getTicket(); | ||
} | ||
|
||
#[QueryHandler(self::GET_TICKETS_NOTIFICATION_COUNT)] | ||
public function getTicketsNotifications(): int | ||
{ | ||
return count($this->ticketNotifications); | ||
} | ||
} |
Oops, something went wrong.