Harden notification release regressions
This commit is contained in:
@@ -78,26 +78,26 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
NotificationChannel::Webhook => $this->sendWebhookRequest($notificationAction),
|
||||
};
|
||||
} catch (ConnectionException $exception) {
|
||||
$this->handleConnectionFailure($deliveryLog, $notificationAction->channel, $attemptCount, $attemptedAt, $exception);
|
||||
$this->handleConnectionFailure($deliveryLog, $notificationAction, $attemptCount, $attemptedAt, $exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->markSucceeded($deliveryLog, $response, $attemptCount, $attemptedAt);
|
||||
$this->markSucceeded($deliveryLog, $notificationAction, $response, $attemptCount, $attemptedAt);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->shouldRetry($response) && $this->hasAttemptsRemaining($attemptCount)) {
|
||||
$this->markRetryable($deliveryLog, $notificationAction->channel, $response, $attemptCount, $attemptedAt);
|
||||
$this->markRetryable($deliveryLog, $notificationAction, $response, $attemptCount, $attemptedAt);
|
||||
$this->release($this->backoffDelay($attemptCount));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->markFailed($deliveryLog, $notificationAction->channel, $response, $attemptCount, $attemptedAt);
|
||||
$this->fail(new \RuntimeException($this->responseFailureDescription($notificationAction->channel, $response)));
|
||||
$this->markFailed($deliveryLog, $notificationAction, $response, $attemptCount, $attemptedAt);
|
||||
$this->fail(new \RuntimeException($this->responseFailureDescription($notificationAction, $response)));
|
||||
}
|
||||
|
||||
public function failed(?Throwable $exception): void
|
||||
@@ -108,23 +108,29 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$notificationAction = NotificationAction::query()->find($this->notificationActionId);
|
||||
|
||||
$deliveryLog->update([
|
||||
'attempt_count' => max($deliveryLog->attempt_count, $this->attempts()),
|
||||
'delivery_status' => NotificationDeliveryStatus::Failed,
|
||||
'delivered_at' => null,
|
||||
'error_excerpt' => $deliveryLog->error_excerpt ?? $this->excerpt($exception?->getMessage()),
|
||||
'error_excerpt' => $deliveryLog->error_excerpt ?? $this->sanitizedExcerpt(
|
||||
$exception?->getMessage(),
|
||||
$notificationAction,
|
||||
),
|
||||
'last_attempted_at' => $deliveryLog->last_attempted_at ?? CarbonImmutable::now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function handleConnectionFailure(
|
||||
NotificationDeliveryLog $deliveryLog,
|
||||
NotificationChannel $channel,
|
||||
NotificationAction $notificationAction,
|
||||
int $attemptCount,
|
||||
CarbonImmutable $attemptedAt,
|
||||
ConnectionException $exception,
|
||||
): void {
|
||||
$errorExcerpt = $this->excerpt($exception->getMessage()) ?? $this->requestFailureDescription($channel);
|
||||
$errorExcerpt = $this->sanitizedExcerpt($exception->getMessage(), $notificationAction)
|
||||
?? $this->requestFailureDescription($notificationAction->channel);
|
||||
|
||||
if ($this->hasAttemptsRemaining($attemptCount)) {
|
||||
$deliveryLog->update([
|
||||
@@ -157,6 +163,7 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
|
||||
private function markSucceeded(
|
||||
NotificationDeliveryLog $deliveryLog,
|
||||
NotificationAction $notificationAction,
|
||||
Response $response,
|
||||
int $attemptCount,
|
||||
CarbonImmutable $attemptedAt,
|
||||
@@ -168,13 +175,13 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
'error_excerpt' => null,
|
||||
'http_status' => $response->status(),
|
||||
'last_attempted_at' => $attemptedAt,
|
||||
'response_excerpt' => $this->responseExcerpt($response),
|
||||
'response_excerpt' => $this->responseExcerpt($response, $notificationAction),
|
||||
]);
|
||||
}
|
||||
|
||||
private function markRetryable(
|
||||
NotificationDeliveryLog $deliveryLog,
|
||||
NotificationChannel $channel,
|
||||
NotificationAction $notificationAction,
|
||||
Response $response,
|
||||
int $attemptCount,
|
||||
CarbonImmutable $attemptedAt,
|
||||
@@ -183,16 +190,16 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
'attempt_count' => $attemptCount,
|
||||
'delivery_status' => NotificationDeliveryStatus::Retryable,
|
||||
'delivered_at' => null,
|
||||
'error_excerpt' => $this->responseFailureDescription($channel, $response),
|
||||
'error_excerpt' => $this->responseFailureDescription($notificationAction, $response),
|
||||
'http_status' => $response->status(),
|
||||
'last_attempted_at' => $attemptedAt,
|
||||
'response_excerpt' => $this->responseExcerpt($response),
|
||||
'response_excerpt' => $this->responseExcerpt($response, $notificationAction),
|
||||
]);
|
||||
}
|
||||
|
||||
private function markFailed(
|
||||
NotificationDeliveryLog $deliveryLog,
|
||||
NotificationChannel $channel,
|
||||
NotificationAction $notificationAction,
|
||||
Response $response,
|
||||
int $attemptCount,
|
||||
CarbonImmutable $attemptedAt,
|
||||
@@ -201,10 +208,10 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
'attempt_count' => $attemptCount,
|
||||
'delivery_status' => NotificationDeliveryStatus::Failed,
|
||||
'delivered_at' => null,
|
||||
'error_excerpt' => $this->responseFailureDescription($channel, $response),
|
||||
'error_excerpt' => $this->responseFailureDescription($notificationAction, $response),
|
||||
'http_status' => $response->status(),
|
||||
'last_attempted_at' => $attemptedAt,
|
||||
'response_excerpt' => $this->responseExcerpt($response),
|
||||
'response_excerpt' => $this->responseExcerpt($response, $notificationAction),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -297,15 +304,16 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
return $backoff[$index];
|
||||
}
|
||||
|
||||
private function responseExcerpt(Response $response): ?string
|
||||
private function responseExcerpt(Response $response, NotificationAction $notificationAction): ?string
|
||||
{
|
||||
return $this->excerpt($response->body()) ?? $this->excerpt($response->reason());
|
||||
return $this->sanitizedExcerpt($response->body(), $notificationAction)
|
||||
?? $this->sanitizedExcerpt($response->reason(), $notificationAction);
|
||||
}
|
||||
|
||||
private function responseFailureDescription(NotificationChannel $channel, Response $response): string
|
||||
private function responseFailureDescription(NotificationAction $notificationAction, Response $response): string
|
||||
{
|
||||
$reason = $this->excerpt($response->reason());
|
||||
$subject = $this->deliverySubject($channel);
|
||||
$reason = $this->sanitizedExcerpt($response->reason(), $notificationAction);
|
||||
$subject = $this->deliverySubject($notificationAction->channel);
|
||||
|
||||
if ($reason === null) {
|
||||
return "{$subject} returned HTTP {$response->status()}.";
|
||||
@@ -364,4 +372,24 @@ class DeliverNotificationAction implements ShouldQueue
|
||||
|
||||
return $excerpt === '' ? null : $excerpt;
|
||||
}
|
||||
|
||||
private function sanitizedExcerpt(?string $value, ?NotificationAction $notificationAction): ?string
|
||||
{
|
||||
if (
|
||||
$value !== null
|
||||
&& $notificationAction !== null
|
||||
&& filled($notificationAction->delivery_url)
|
||||
) {
|
||||
$value = str_replace(
|
||||
[
|
||||
$notificationAction->delivery_url,
|
||||
str_replace('/', '\/', $notificationAction->delivery_url),
|
||||
],
|
||||
'[redacted]',
|
||||
$value,
|
||||
);
|
||||
}
|
||||
|
||||
return $this->excerpt($value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user