Clean up for handling postmark email failures

This commit is contained in:
David Bomba 2024-09-26 14:35:33 +10:00
parent 5bb049df34
commit 2ceaf880b8
2 changed files with 63 additions and 56 deletions

View File

@ -11,31 +11,32 @@
namespace App\Jobs\Mail; namespace App\Jobs\Mail;
use App\DataMapper\Analytics\EmailFailure; use App\Models\User;
use App\DataMapper\Analytics\EmailSuccess; use App\Utils\Ninja;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\User; use App\Libraries\MultiDB;
use App\Utils\Ninja; use App\Models\ClientContact;
use App\Utils\Traits\MakesHash;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash;
use App\Libraries\Google\Google;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use Postmark\Models\PostmarkException;
use Turbo124\Beacon\Facades\LightLogs;
use Illuminate\Queue\InteractsWithQueue;
use GuzzleHttp\Exception\ClientException;
use App\DataMapper\Analytics\EmailFailure;
use App\DataMapper\Analytics\EmailSuccess;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use Illuminate\Queue\SerializesModels; use App\Events\Payment\PaymentWasEmailedAndFailed;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
use Turbo124\Beacon\Facades\LightLogs;
/*Multi Mailer implemented*/ /*Multi Mailer implemented*/
@ -242,6 +243,21 @@ class NinjaMailerJob implements ShouldQueue
} }
/**
* Post mark buries the proper message in a guzzle response
* this merges a text string with a json object
* need to harvest the ->Message property using the following
*/
if ($e instanceof PostmarkException) { //postmark specific failure
$this->fail();
$this->entityEmailFailed($e->getMessage());
$this->cleanUpMailers();
return;
}
//only report once, not on all tries //only report once, not on all tries
if ($this->attempts() == $this->tries) { if ($this->attempts() == $this->tries) {
/* If there is an entity attached to the message send a failure mailer */ /* If there is an entity attached to the message send a failure mailer */
@ -249,10 +265,8 @@ class NinjaMailerJob implements ShouldQueue
$this->entityEmailFailed($message); $this->entityEmailFailed($message);
} }
/* Don't send postmark failures to Sentry */ app('sentry')->captureException($e);
if (Ninja::isHosted() && (!$e instanceof ClientException)) {
app('sentry')->captureException($e);
}
} }
/* Releasing immediately does not add in the backoff */ /* Releasing immediately does not add in the backoff */

View File

@ -11,38 +11,39 @@
namespace App\Services\Email; namespace App\Services\Email;
use App\DataMapper\Analytics\EmailFailure; use Log;
use App\DataMapper\Analytics\EmailSuccess; use App\Models\User;
use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Utils\Ninja;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Mail\Engine\PaymentEmailEngine;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\Vendor;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\User;
use App\Models\Vendor;
use App\Models\VendorContact;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\Ninja; use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\VendorContact;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\VendorHtmlEngine; use App\Utils\VendorHtmlEngine;
use App\Libraries\Google\Google;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use Postmark\Models\PostmarkException;
use Turbo124\Beacon\Facades\LightLogs;
use App\Mail\Engine\PaymentEmailEngine;
use Illuminate\Queue\InteractsWithQueue;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable; use App\DataMapper\Analytics\EmailFailure;
use App\DataMapper\Analytics\EmailSuccess;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Mail\Mailable; use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use Illuminate\Queue\InteractsWithQueue; use App\Events\Payment\PaymentWasEmailedAndFailed;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
use Log;
use Turbo124\Beacon\Facades\LightLogs;
class Email implements ShouldQueue class Email implements ShouldQueue
{ {
@ -375,16 +376,10 @@ class Email implements ShouldQueue
* this merges a text string with a json object * this merges a text string with a json object
* need to harvest the ->Message property using the following * need to harvest the ->Message property using the following
*/ */
if ($e instanceof ClientException) { //postmark specific failure if ($e instanceof PostmarkException) { //postmark specific failure
$response = $e->getResponse();
$message_body = json_decode($response->getBody()->getContents());
if ($message_body && property_exists($message_body, 'Message')) {
$message = $message_body->Message;
}
$this->fail(); $this->fail();
$this->entityEmailFailed($message); $this->entityEmailFailed($e->getMessage());
$this->cleanUpMailers(); $this->cleanUpMailers();
return; return;
@ -395,10 +390,8 @@ class Email implements ShouldQueue
/* If the is an entity attached to the message send a failure mailer */ /* If the is an entity attached to the message send a failure mailer */
$this->entityEmailFailed($message); $this->entityEmailFailed($message);
/* Don't send postmark failures to Sentry */ app('sentry')->captureException($e);
if (Ninja::isHosted() && (!$e instanceof ClientException)) {
app('sentry')->captureException($e);
}
} }
$this->tearDown(); $this->tearDown();
@ -985,7 +978,7 @@ class Email implements ShouldQueue
* @param string $message * @param string $message
* @return void * @return void
*/ */
private function entityEmailFailed($message): void private function entityEmailFailed(string $message = ''): void
{ {
$class = get_class($this->email_object->entity); $class = get_class($this->email_object->entity);