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

[2.x] Manual retry #20

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions database/migrations/7_add_tries_to_logs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use MailCarrier\Enums\LogStatus;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->unsignedSmallInteger('tries')->default(0);
$table->timestamp('last_try_at')->nullable();
});

DB::table('logs')->whereNot('status', LogStatus::Pending->value)->update([
'last_try_at' => DB::raw('created_at'),
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->dropColumn('tries');
$table->dropColumn('last_try_at');
});
}
};
29 changes: 29 additions & 0 deletions database/migrations/8_add_tags_metadata_to_logs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->json('tags')->nullable();
$table->json('metadata')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('logs', function (Blueprint $table) {
$table->dropColumn('tags');
$table->dropColumn('metadata');
});
}
};
2 changes: 2 additions & 0 deletions src/Actions/Logs/CreateFromGenericMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function run(GenericMailDto $genericMailDto): Log
hash: $genericMailDto->template->getHash(),
),
'variables' => $genericMailDto->variables,
'tags' => $genericMailDto->tags,
'metadata' => $genericMailDto->metadata,
]);

$log->attachments()->createMany(
Expand Down
17 changes: 14 additions & 3 deletions src/Actions/SendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use MailCarrier\Exceptions\TemplateRenderException;
use MailCarrier\Facades\MailCarrier;
use MailCarrier\Jobs\SendMailJob;
use MailCarrier\Models\Log;
use MailCarrier\Models\Template;

class SendMail extends Action
Expand All @@ -30,15 +31,21 @@ class SendMail extends Action

protected bool $shouldLog = true;

protected ?Log $log = null;

/**
* Send or enqueue the email.
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function run(SendMailDto $params): void
public function run(SendMailDto $params, ?Log $log = null): void
{
$this->params = $params;
if ($params->recipients && !is_null($log)) {
throw new \LogicException('A Log can be passed only with a single recipient.');
}

$this->log = $log;
$this->params = $params;
$this->template = (new Templates\FindBySlug)->run($params->template);
$this->recipients = $params->recipients ?: [
new RecipientDto(
Expand Down Expand Up @@ -131,7 +138,11 @@ protected function send(RecipientDto $recipient): void
error: $exception?->getMessage(),
);

$log = !$this->shouldLog ? null : (new Logs\CreateFromGenericMail)->run($genericMailDto);
if ($this->log) {
$log = $this->log;
} else {
$log = !$this->shouldLog ? null : (new Logs\CreateFromGenericMail)->run($genericMailDto);
}

if ($exception) {
$exception->setLog($log);
Expand Down
1 change: 1 addition & 0 deletions src/Facades/MailCarrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @method static string|null download(string $resource, ?string $disk = null)
* @method static int getFileSize(string $resource, ?string $disk = null)
* @method static string humanBytes(int $bytes)
* @method static array<int, int> getEmailRetriesBackoff()
*
* @see \MailCarrier\MailCarrierManager
*/
Expand Down
20 changes: 11 additions & 9 deletions src/Jobs/SendMailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MailCarrier\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -12,6 +13,7 @@
use MailCarrier\Dto\GenericMailDto;
use MailCarrier\Enums\LogStatus;
use MailCarrier\Exceptions\SendingFailedException;
use MailCarrier\Facades\MailCarrier;
use MailCarrier\Mail\GenericMail;
use MailCarrier\Models\Log;

Expand Down Expand Up @@ -43,6 +45,11 @@ public function __construct(
*/
public function handle(): void
{
// Prevent sending already sent logs, e.g. from manual retry
if ($this->log?->status === LogStatus::Sent) {
return;
}

$error = null;

try {
Expand All @@ -55,11 +62,13 @@ public function handle(): void
(new Logs\Update)->run($this->log, [
'status' => $error ? LogStatus::Failed : LogStatus::Sent,
'error' => $error,
'tries' => $this->log->tries + 1,
'last_try_at' => Carbon::now(),
]);
}

if ($error) {
throw (new SendingFailedException($error))->setLog($this->log);
throw (new SendingFailedException($error))->setLog($this->log->refresh());
}
}

Expand All @@ -83,13 +92,6 @@ protected function send(): void
*/
public function backoff(): array
{
return [
5, // 5sec
30, // 30sec
60, // 1min
60 * 5, // 5min
60 * 30, // 30min
60 * 60, // 1h
];
return MailCarrier::getEmailRetriesBackoff();
}
}
17 changes: 17 additions & 0 deletions src/MailCarrierManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,21 @@ public function humanBytes(int $bytes): string

return round($bytes, 2) . ' ' . $units[$i];
}

/**
* Get the retries (in seconds) and labels for a failing email.
*
* @return array<int, int>
*/
public function getEmailRetriesBackoff(): array
{
return [
5, // 5sec
30, // 30sec
60, // 1min
60 * 5, // 5min
60 * 30, // 30min
60 * 60, // 1h
];
}
}
2 changes: 2 additions & 0 deletions src/MailCarrierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function configurePackage(Package $package): void
'4_create_logs_table',
'5_create_attachments_table',
'6_transform_logs_cc_bcc_array',
'7_add_tries_to_logs_table',
'8_add_tags_metadata_to_logs_table',
])
->runsMigrations();
}
Expand Down
9 changes: 9 additions & 0 deletions src/Models/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
* @property \MailCarrier\Dto\LogTemplateDto $template_frozen
* @property array<string, mixed>|null $variables
* @property string|null $error
* @property int $tries
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon|null $last_try_at
* @property array|null $tags
* @property array|null $metadata
* @property-read \MailCarrier\Models\Template|null $template
* @property-read \Illuminate\Database\Eloquent\Collection<int, \MailCarrier\Models\Attachment> $attachments
*/
Expand Down Expand Up @@ -66,6 +70,8 @@ class Log extends Model
'template_frozen',
'variables',
'error',
'tries',
'last_try_at',
];

/**
Expand All @@ -87,6 +93,9 @@ class Log extends Model
'bcc' => CollectionOfContacts::class,
'template_frozen' => LogTemplateDto::class,
'variables' => 'array',
'tags' => 'array',
'metadata' => 'json',
'last_try_at' => 'datetime',
];

/**
Expand Down
83 changes: 81 additions & 2 deletions src/Resources/LogResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace MailCarrier\Resources;

use Carbon\CarbonInterface;
use Filament\Forms\Components\FileUpload;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Support\Colors\Color;
use Filament\Support\Enums\Alignment;
use Filament\Tables;
use Filament\Tables\Actions\Action as TablesAction;
Expand All @@ -11,8 +15,11 @@
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use MailCarrier\Actions\Logs\GetTriggers;
use MailCarrier\Actions\SendMail;
use MailCarrier\Dto\LogTemplateDto;
use MailCarrier\Dto\SendMailDto;
use MailCarrier\Enums\LogStatus;
use MailCarrier\Facades\MailCarrier;
use MailCarrier\Models\Log;
use MailCarrier\Models\Template;
use MailCarrier\Resources\LogResource\Pages;
Expand Down Expand Up @@ -76,6 +83,25 @@ public static function table(Tables\Table $table): Tables\Table
->modalFooterActionsAlignment(Alignment::Center)
),

Tables\Columns\TextColumn::make('tries')
->badge()
->tooltip(function (Log $record) {
if ($record->status !== LogStatus::Failed || is_null($record->last_try_at)) {
return null;
}

// We add "1" to retries count because the first try is not counted as "retry"
if ($record->tries >= count(MailCarrier::getEmailRetriesBackoff()) + 1) {
return 'No retry left.';
}

return 'Retrying in ' . $record->last_try_at
->addSeconds(
MailCarrier::getEmailRetriesBackoff()[max(0, $record->tries - 1)]
)
->diffForHumans(syntax: CarbonInterface::DIFF_ABSOLUTE);
}),

Tables\Columns\TextColumn::make('created_at')
->label('Sent at')
->since()
Expand Down Expand Up @@ -136,7 +162,7 @@ protected static function getTableActions(): array
]))
->modalSubmitAction(false)
->modalCancelActionLabel('Close')
->modalFooterActionsAlignment(Alignment::Center),
->modalFooterActionsAlignment(Alignment::Right),

Tables\Actions\Action::make('preview')
->icon('heroicon-o-eye')
Expand All @@ -146,7 +172,24 @@ protected static function getTableActions(): array
->modalWidth('7xl')
->modalSubmitAction(false)
->modalCancelActionLabel('Close')
->modalFooterActionsAlignment(Alignment::Center),
->modalFooterActionsAlignment(Alignment::Right),

Tables\Actions\Action::make('manual_retry')
->icon('heroicon-o-arrow-path')
->color(Color::Orange)
->form([
FileUpload::make('attachments')
->multiple()
->preserveFilenames()
->storeFiles(false),
])
->modalWidth('2xl')
->modalIcon('heroicon-o-arrow-path')
->modalDescription('Are you sure you want to manually retry to send this email?')
->modalSubmitActionLabel('Retry')
->modalFooterActionsAlignment(Alignment::Right)
->action(fn (?Log $record, array $data) => $record ? static::retryEmail($record, $data) : null)
->visible(fn (?Log $record) => $record?->status === LogStatus::Failed),
];
}

Expand Down Expand Up @@ -176,4 +219,40 @@ protected static function getTemplateValue(LogTemplateDto $templateDto, ?Templat
($subtitle ? '<p class="text-xs mt-1 text-slate-300">' . $subtitle . '</p>' : '')
);
}

protected static function retryEmail(Log $log, array $data): void
{
try {
SendMail::resolve()->run(
new SendMailDto([
'template' => $log->template->slug,
'subject' => $log->subject,
'sender' => $log->sender,
'recipient' => $log->recipient,
'cc' => $log->cc->all(),
'bcc' => $log->bcc->all(),
'variables' => $log->variables,
'trigger' => $log->trigger,
'tags' => $log->tags ?: [],
'metadata' => $log->metadata ?: [],
'attachments' => $data['attachments'] ?? [],
]),
$log
);
} catch (\Throwable $e) {
Notification::make()
->title('Error while sending email')
->body($e->getMessage())
->danger()
->send();

return;
}

Notification::make()
->icon('heroicon-o-paper-airplane')
->title('Email sent correctly')
->success()
->send();
}
}
Loading