Laravel Vapor - test attaching a file from S3 in an email

This commit is contained in:
Dillon Ngo
2024-05-16 02:33:23 +08:00
parent 9265cd494a
commit 8355c81f20
4 changed files with 134 additions and 26 deletions
@@ -0,0 +1,59 @@
<?php
namespace App\Classes\Jobs\Commands\V2;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Models\Container;
use App\Models\Order;
use App\Models\User;
use App\Classes\Notifications\ShipmentDepartureEmail;
class TestAttachingS3FileAndSendEmailCommandJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
Log::info(Carbon::now() . ': Start job - Test to attach a file from S3 bucket and trigger an email send.');
$start = new Carbon();
$containers = Container::whereHas('transports', function ($transport){
return $transport->whereDate('dispatch_date', '<', Carbon::today())->whereDate('dispatch_date', '>', Carbon::now()->subdays(10));
})->get();
Log::info("[TESTING] total containers: " . count($containers));
$sent = false;
foreach ($containers as $container){
if($sent){
break;
}
foreach ($container->packingLists as $packingList){
if($sent){
break;
}
if(!($packingList->owner instanceof Order)) continue;
$user = $packingList->owner->companyModule->employees()->first();
$user = User::where('email', 'dillonngoweijoon@gmail.com')->first();
Log::info("[TESTING] user: " . json_encode($user));
Log::info("[TESTING] packingList: " . json_encode($packingList));
$user->notify(new ShipmentDepartureEmail($user, $packingList));
$sent = true;
}
}
$end = new Carbon();
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
Log::info(Carbon::now() . ': End job - Test to attach a file from S3 bucket and trigger an email send. ElapsedTime: ' . $elapsedTime . '.');
}
}
@@ -44,9 +44,12 @@ class ShipmentDepartureEmail extends AbstractEmail
$mailMessage = (new MailMessage) ->subject('Your packages are on the way to malaysia - Invoice pending payment for order no.'. $this->packingList->owner->reference);
$filesystemDriver = Storage::getDefaultDriver();
$filename = $invoiceDocument->first()->file->file_info->original->file;
Log::info('ShipmentDepartureEmail file name: '.$filename);
if($filesystemDriver === 's3'){
$fileContent = null;
$fileContent = Storage::disk('s3')->get('documents/' . $invoiceDocument->first()->file->file_info->original->file);
$fileContent = Storage::disk('s3')->get('documents/' . $filename);
if ($fileContent) {
$mailMessage->attachData($fileContent, [
'as' => 'invoice.pdf',
@@ -57,7 +60,7 @@ class ShipmentDepartureEmail extends AbstractEmail
else{
$filePath = '';
// $fileContent = Storage::disk('documents')->get($invoiceDocument->first()->file->file_info->original->file);
$filePath = storage_path('app/documents/' . $invoiceDocument->first()->file->file_info->original->file);
$filePath = storage_path('app/documents/' . $filename);
if ($filePath) {
$mailMessage->attach($filePath, [
'as' => 'invoice.pdf',
@@ -0,0 +1,44 @@
<?php
namespace App\Console\Commands\V2;
use App\Classes\Jobs\Commands\V2\TestAttachingS3FileAndSendEmailCommandJob;
use Illuminate\Console\Command;
class TestAttachingS3FileAndSendEmailCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test-attach-s3-file-in-email-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This is a test to attach a file from S3 bucket and trigger an email send';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
TestAttachingS3FileAndSendEmailCommandJob::dispatch();
}
}
+26 -24
View File
@@ -34,37 +34,39 @@ class Kernel extends ConsoleKernel
->everyFiveMinutes()
->withoutOverlapping();
$schedule->command('curl-vt-command')
->cron('0 8 * * *')
->withoutOverlapping();
//cief todo: disable commands to avoid unintentional interfering with production starts
// $schedule->command('curl-vt-command')
// ->cron('0 8 * * *')
// ->withoutOverlapping();
$schedule->command('curl-yd-order-list-command')
->cron('0 9-18/3 * * *')
->withoutOverlapping();
// $schedule->command('curl-yd-order-list-command')
// ->cron('0 9-18/3 * * *')
// ->withoutOverlapping();
$schedule->command('fix-packinglist-command')
->cron('30 9-18/3 * * *')
->withoutOverlapping();
// $schedule->command('fix-packinglist-command')
// ->cron('30 9-18/3 * * *')
// ->withoutOverlapping();
$schedule->command('fix-duplicate-container-reference-command')
->cron('0 1 * * *')
->withoutOverlapping();
// $schedule->command('fix-duplicate-container-reference-command')
// ->cron('0 1 * * *')
// ->withoutOverlapping();
$schedule->command('invoice-generate-command')
->hourly()
->withoutOverlapping();
// $schedule->command('invoice-generate-command')
// ->hourly()
// ->withoutOverlapping();
$schedule->command('billplz-failed-callback-fix-command')
->hourly()
->withoutOverlapping();
// $schedule->command('billplz-failed-callback-fix-command')
// ->hourly()
// ->withoutOverlapping();
$schedule->command('check-storage-invoices-group-transactions-command')
->dailyAt('0:01')
->withoutOverlapping();
// $schedule->command('check-storage-invoices-group-transactions-command')
// ->dailyAt('0:01')
// ->withoutOverlapping();
$schedule->command('permits-reminder-send-command')
->dailyAt('09:30')
->withoutOverlapping();
// $schedule->command('permits-reminder-send-command')
// ->dailyAt('09:30')
// ->withoutOverlapping();
//cief todo: disable commands to avoid unintentional interfering with production ends
}
//Commands Version 1: Before AWS
else