Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JOBS-10957: Fix SQS error handling / general refactoring #43

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions src/ServiceBusSQSChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ringierimu\ServiceBusNotificationsChannel;

use Aws\Exception\AwsException;
use Aws\Sqs\SqsClient;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
Expand All @@ -11,12 +12,20 @@ class ServiceBusSQSChannel
{
protected SqsClient $sqs;

protected array $config;

protected bool $hasAttemptedRefresh = false;

public function __construct(array $config = [])
{
$this->config = $config ?: config('services.service_bus');
$this->initializeSqsClient();
}

protected function initializeSqsClient(): void
{
$this->sqs = new SqsClient([
'region' => Arr::get($this->config, 'sqs.region'),
'region' => Arr::get($this->config, 'sqs.region', 'eu-west-1'),
'version' => 'latest',
'credentials' => [
'key' => Arr::get($this->config, 'sqs.key'),
Expand All @@ -25,7 +34,7 @@ public function __construct(array $config = [])
]);
}

public function send($notifiable, Notification $notification)
public function send($notifiable, Notification $notification): void
{
/** @var ServiceBusEvent $event */
$event = $notification->toServiceBus($notifiable);
Expand All @@ -50,21 +59,68 @@ public function send($notifiable, Notification $notification)
return;
}

$message = $notification
->toServiceBus($notifiable)
->getParams();
if (!isset($params['from'], $params['events'][0])) {
Log::error('Invalid message structure', ['params' => $params]);
return;
}

$response = $this->sqs->sendMessage([
'QueueUrl' => Arr::get($this->config, 'sqs.queue_url'),
'MessageBody' => json_encode($message),
'MessageGroupId' => $message['from'],
]);
$queueUrl = Arr::get($this->config, 'sqs.queue_url');
$isFifoQueue = strpos($queueUrl, '.fifo') !== false;

$event = $message['events'][0];
$payloadSqs = [
'QueueUrl' => $queueUrl,
'MessageBody' => json_encode($params),
];

Log::info("{$event} sent to bus queue", [
'message_id' => $response->get('MessageId'),
'message' => $message,
]);
if ($isFifoQueue) {
$payloadSqs['MessageGroupId'] = $params['from'];
$payloadSqs['MessageDeduplicationId'] = md5(json_encode($params));
}

$this->sendMessageToSqs($payloadSqs, $eventType, $params, $dontReport);
}

protected function sendMessageToSqs(array $payloadSqs, string $eventType, array $params, array $dontReport): void
{
try {
$response = $this->sqs->sendMessage($payloadSqs);

$eventName = $params['events'][0];

if (!in_array($eventType, $dontReport)) {
Log::info("{$eventName} sent to bus queue", [
'message_id' => $response->get('MessageId'),
'params' => $params,
]);
}

$this->hasAttemptedRefresh = false;
} catch (AwsException $exception) {
$code = $exception->getAwsErrorCode();

if (in_array($code, ['ExpiredToken', 'UnrecognizedClientException', 'InvalidClientTokenId'])) {
Log::info("$code received. Refreshing credentials and retrying.", [
'event' => $eventType,
'params' => $params,
'aws_error_code' => $code,
'aws_error_message' => $exception->getAwsErrorMessage(),
'tags' => ['service-bus'],
]);

if (!$this->hasAttemptedRefresh) {
$this->hasAttemptedRefresh = true;

$this->initializeSqsClient();

$this->sendMessageToSqs($payloadSqs, $eventType, $params, $dontReport);
} else {
$this->hasAttemptedRefresh = false;

throw new \Exception('Authentication failed after retrying.', 0, $exception);
}
} else {
throw $exception;
}
}
}
}
Loading