Laravel Vapor - New schedule tasks and commands for token expiration for new user and password reset (sync code from Shipping Portal)

This commit is contained in:
Dillon Ngo
2024-10-09 13:25:52 +08:00
parent f19e9bf0f8
commit a9dfb8abf7
6 changed files with 108 additions and 11 deletions
@@ -0,0 +1,38 @@
<?php
namespace App\Classes\Jobs\Commands\V2;
use App\Classes\Modules\Accounts\Services\ExpiresPasswordReset;
use App\Models\PasswordReset;
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;
class PasswordResetTokenExpirationV2CommandJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
Log::info(Carbon::now() . ': Start job - Password reset token expiration check.');
$start = new Carbon();
$attempts = PasswordReset::active()->oneDayOld()->get();
Log::info('PasswordReset Carbon now()->subHours(24): '. Carbon::now()->subHours(24));
Log::info('PasswordReset Attempts count: '.count($attempts));
foreach ($attempts as $attempt){
(new ExpiresPasswordReset())->execute($attempt);
Log::info('PasswordReset Expired: '.$attempt->id.', '.$attempt->created_at);
}
$end = new Carbon();
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
Log::info(Carbon::now() . ': End job - Password reset token expiration check. ElapsedTime: ' . $elapsedTime . '.');
}
}
@@ -76,11 +76,11 @@ class GeneratePasswordResetLogic extends AbstractControllerLogic
$attempt = $this->generatesPasswordReset->execute($user);
$this->passwordResetTokenExpiration::dispatch($attempt)->delay(now()->addHours(24));
//$this->passwordResetTokenExpiration::dispatch($attempt)->delay(now()->addHours(24)); //converted to schedule task
$this->sendResetPasswordEmail::dispatch($user, $attempt);
return $this->response(['email' => $object->getEmail()]);
}
}
}
}
@@ -50,7 +50,7 @@ class GenerateEmailVerificationAttemptProcessor
$attempt = $this->generatesEmailVerificationAttempt->execute($user);
// $this->emailVerificationAttemptExpiration::dispatch($attempt)->delay(now()->addHours(48)); //cief todo: changed to schedule task (DONE)
// $this->emailVerificationAttemptExpiration::dispatch($attempt)->delay(now()->addHours(48)); //converted to schedule task
$this->sendUserVerificationEmail::dispatch($user, $attempt);
@@ -0,0 +1,46 @@
<?php
namespace App\Console\Commands\V2;
use App\Classes\Jobs\Commands\V2\PasswordResetTokenExpirationV2CommandJob;
use Illuminate\Console\Command;
class PasswordResetTokenExpirationV2Command extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'password-reset-token-expriration-check-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Password reset token expiration check.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
PasswordResetTokenExpirationV2CommandJob::dispatch();
}
}
+11 -8
View File
@@ -33,14 +33,22 @@ class Kernel extends ConsoleKernel
//Commands Version 2: Laravel Vapor/AWS
$isEnabled = env('COMMANDS_V2_ENABLED', false);
if($isEnabled){
$schedule->command('dummy-command')
->everyFiveMinutes()
->withoutOverlapping();
// $schedule->command('dummy-command')
// ->everyFiveMinutes()
// ->withoutOverlapping();
$schedule->command('housekeeping-s3-files-command')
->dailyAt('01:00')
->withoutOverlapping();
$schedule->command('password-reset-token-expriration-check-command')
->everySixHours()
->withoutOverlapping();
$schedule->command('new-user-registration-expire-check-command')
->everySixHours()
->withoutOverlapping();
if(env('APP_ENV') === 'production'){
//cief todo: disable commands to avoid unintentional interfering with production starts
// $schedule->command('email-do-to-vt-command')
@@ -55,11 +63,6 @@ class Kernel extends ConsoleKernel
// ->hourly()
// ->withoutOverlapping();
// $schedule->command('new-user-registration-expire-check-command')
// //->dailyAt('23:55') //cief todo: original, to be reverted back to this
// ->everyFifteenMinutes()
// ->withoutOverlapping();
// $schedule->command('booking-expired-command')
// ->dailyAt('02:00')
// ->withoutOverlapping();
+10
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PasswordReset extends AbstractModel
@@ -22,4 +23,13 @@ class PasswordReset extends AbstractModel
public function user(): BelongsTo {
return $this->belongsTo(User::class, 'user_id', 'id');
}
/**
* @param $query
* @return mixed
*/
public function scopeOneDayOld($query)
{
return $query->where('created_at', '<=', Carbon::now()->subHours(24));
}
}