Adjustments for GMail 429 handling

This commit is contained in:
David Bomba 2024-05-26 10:18:11 +10:00
parent 5123c9be6c
commit 9c7f1e888f
4 changed files with 44 additions and 18 deletions

View File

@ -64,23 +64,23 @@ class GmailTransport extends AbstractTransport
$body->setRaw($this->base64_encode($bcc_list.$message->toString())); $body->setRaw($this->base64_encode($bcc_list.$message->toString()));
try { // try {
$service->users_messages->send('me', $body, []); $service->users_messages->send('me', $body, []);
} catch(\Google\Service\Exception $e) { // } catch(\Google\Service\Exception $e) {
/* Need to slow down */ // /* Need to slow down */
if ($e->getCode() == '429') { // if ($e->getCode() == '429') {
nlog("429 google - retrying "); // nlog("429 google - retrying ");
sleep(rand(3,8)); // sleep(rand(3,8));
try { // try {
$service->users_messages->send('me', $body, []); // $service->users_messages->send('me', $body, []);
} catch(\Google\Service\Exception $e) { // } catch(\Google\Service\Exception $e) {
} // }
} // }
} // }
} }
private function base64_encode($data) private function base64_encode($data)

View File

@ -164,7 +164,20 @@ class NinjaMailerJob implements ShouldQueue
$this->cleanUpMailers(); $this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first()); $this->logMailError($e->getMessage(), $this->company->clients()->first());
return; return;
} catch (\Exception | \Google\Service\Exception $e) { }
catch(\Google\Service\Exception $e){
if ($e->getCode() == '429') {
$message = "Google rate limiting triggered, we are queueing based on GMail requirements.";
$this->logMailError($message, $this->company->clients()->first());
sleep(rand(1, 2));
$this->release(900);
}
}
catch (\Exception $e) {
nlog("Mailer failed with {$e->getMessage()}"); nlog("Mailer failed with {$e->getMessage()}");
$message = $e->getMessage(); $message = $e->getMessage();
@ -221,8 +234,7 @@ class NinjaMailerJob implements ShouldQueue
} }
/* Releasing immediately does not add in the backoff */ /* Releasing immediately does not add in the backoff */
sleep(rand(5, 10)); sleep(rand(2, 3));
$this->release($this->backoff()[$this->attempts() - 1]); $this->release($this->backoff()[$this->attempts() - 1]);
} }

View File

@ -302,7 +302,21 @@ class Email implements ShouldQueue
$this->cleanUpMailers(); $this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first()); $this->logMailError($e->getMessage(), $this->company->clients()->first());
return; return;
} catch (\Exception | \RuntimeException | \Google\Service\Exception $e) { }
catch(\Google\Service\Exception $e){
if ($e->getCode() == '429') {
$message = "Google rate limiting triggered, we are queueing based on GMail requirements.";
$this->logMailError($message, $this->company->clients()->first());
sleep(rand(1, 2));
$this->release(900);
$message = null;
}
}
catch (\Exception | \RuntimeException $e) {
nlog("Mailer failed with {$e->getMessage()}"); nlog("Mailer failed with {$e->getMessage()}");
$message = $e->getMessage(); $message = $e->getMessage();

View File

@ -166,9 +166,9 @@ class EmailDefaults
private function setBody(): self private function setBody(): self
{ {
if (strlen($this->email->email_object->body) > 3) { if (strlen($this->email->email_object->body ?? '') > 3) {
// A Custom Message has been set in the email screen. // A Custom Message has been set in the email screen.
} elseif (strlen($this->email->email_object->settings?->{$this->email->email_object->email_template_body}) > 3) { } elseif (strlen($this->email->email_object->settings?->{$this->email->email_object->email_template_body} ?? '') > 3) {
// A body has been saved in the settings. // A body has been saved in the settings.
$this->email->email_object->body = $this->email->email_object->settings?->{$this->email->email_object->email_template_body}; $this->email->email_object->body = $this->email->email_object->settings?->{$this->email->email_object->email_template_body};
} else { } else {