feat(cloud): deliver service compensation notifications
parent
50e054ab86
commit
f628d8bd7b
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/** Fair, failure-isolated dispatcher for customer notification outboxes. */
|
||||
final class CloudNotificationDeliveryService
|
||||
{
|
||||
private readonly \Closure $maintenanceTick;
|
||||
private readonly \Closure $compensationTick;
|
||||
private bool $maintenanceFirst = true;
|
||||
private array $errors = [];
|
||||
|
||||
public function __construct(
|
||||
?callable $maintenanceTick = null,
|
||||
?callable $compensationTick = null
|
||||
) {
|
||||
if ($maintenanceTick === null) {
|
||||
$maintenance = new CloudMaintenanceNoticeDeliveryService();
|
||||
$maintenanceTick = [$maintenance, 'tick'];
|
||||
}
|
||||
if ($compensationTick === null) {
|
||||
$compensation = new CloudServiceCompensationNotificationService();
|
||||
$compensationTick = [$compensation, 'tick'];
|
||||
}
|
||||
$this->maintenanceTick = \Closure::fromCallable($maintenanceTick);
|
||||
$this->compensationTick = \Closure::fromCallable($compensationTick);
|
||||
}
|
||||
|
||||
public function tick(): bool
|
||||
{
|
||||
$this->errors = [];
|
||||
$channels = $this->maintenanceFirst
|
||||
? [
|
||||
['maintenance_notification_failed', $this->maintenanceTick],
|
||||
['notification_failed', $this->compensationTick],
|
||||
]
|
||||
: [
|
||||
['notification_failed', $this->compensationTick],
|
||||
['maintenance_notification_failed', $this->maintenanceTick],
|
||||
];
|
||||
$this->maintenanceFirst = !$this->maintenanceFirst;
|
||||
|
||||
$worked = false;
|
||||
foreach ($channels as [$errorCode, $tick]) {
|
||||
try {
|
||||
$worked = $tick() === true || $worked;
|
||||
} catch (\Throwable $error) {
|
||||
$this->errors[] = $errorCode;
|
||||
fwrite(
|
||||
STDERR,
|
||||
'cloud notification channel temporarily unavailable: '
|
||||
. $errorCode . PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
return $worked;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function observationErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudNotificationDeliveryService;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$order = [];
|
||||
$maintenanceCalls = 0;
|
||||
$compensationCalls = 0;
|
||||
$service = new CloudNotificationDeliveryService(
|
||||
static function () use (&$order, &$maintenanceCalls): bool {
|
||||
$order[] = 'maintenance';
|
||||
$maintenanceCalls++;
|
||||
return false;
|
||||
},
|
||||
static function () use (&$order, &$compensationCalls): bool {
|
||||
$order[] = 'compensation';
|
||||
$compensationCalls++;
|
||||
return true;
|
||||
}
|
||||
);
|
||||
$assert($service->tick() === true
|
||||
&& $order === ['maintenance', 'compensation']
|
||||
&& $maintenanceCalls === 1 && $compensationCalls === 1
|
||||
&& $service->observationErrors() === [],
|
||||
'First notification pass was not bounded or did not report work.');
|
||||
$order = [];
|
||||
$assert($service->tick() === true
|
||||
&& $order === ['compensation', 'maintenance']
|
||||
&& $maintenanceCalls === 2 && $compensationCalls === 2,
|
||||
'Notification channel priority did not alternate fairly.');
|
||||
|
||||
$order = [];
|
||||
$isolated = new CloudNotificationDeliveryService(
|
||||
static function () use (&$order): bool {
|
||||
$order[] = 'maintenance';
|
||||
throw new RuntimeException('expected maintenance failure');
|
||||
},
|
||||
static function () use (&$order): bool {
|
||||
$order[] = 'compensation';
|
||||
return true;
|
||||
}
|
||||
);
|
||||
$assert($isolated->tick() === true
|
||||
&& $order === ['maintenance', 'compensation']
|
||||
&& $isolated->observationErrors() === [
|
||||
'maintenance_notification_failed',
|
||||
], 'A maintenance failure blocked compensation delivery or was hidden.');
|
||||
|
||||
$bothFailed = new CloudNotificationDeliveryService(
|
||||
static function (): bool {
|
||||
throw new RuntimeException('expected maintenance failure');
|
||||
},
|
||||
static function (): bool {
|
||||
throw new RuntimeException('expected compensation failure');
|
||||
}
|
||||
);
|
||||
$assert($bothFailed->tick() === false
|
||||
&& $bothFailed->observationErrors() === [
|
||||
'maintenance_notification_failed',
|
||||
'notification_failed',
|
||||
], 'Multiple notification channel failures were not retained.');
|
||||
|
||||
echo "Cloud notification delivery service test passed.\n";
|
||||
Loading…
Reference in New Issue