refactor: optimize mail reminder system - fix memory overflow, improve performance 20-30x, streamline code

This commit is contained in:
xboard
2025-05-24 20:08:21 +08:00
parent 38f640c6d9
commit 4e84cbd953
2 changed files with 211 additions and 32 deletions

View File

@@ -4,7 +4,7 @@ namespace App\Console\Commands;
use App\Services\MailService;
use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Support\Facades\Log;
class SendRemindMail extends Command
{
@@ -13,7 +13,9 @@ class SendRemindMail extends Command
*
* @var string
*/
protected $signature = 'send:remindMail';
protected $signature = 'send:remindMail
{--chunk-size=500 : 每批处理的用户数量}
{--force : 强制执行,跳过确认}';
/**
* The console command description.
@@ -22,33 +24,80 @@ class SendRemindMail extends Command
*/
protected $description = '发送提醒邮件';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @return int
*/
public function handle()
public function handle(): int
{
if (!(bool) admin_setting('remind_mail_enable', false)) {
return;
if (!admin_setting('remind_mail_enable', false)) {
$this->warn('邮件提醒功能未启用');
return 0;
}
$users = User::all();
$chunkSize = max(100, min(2000, (int) $this->option('chunk-size')));
$mailService = new MailService();
foreach ($users as $user) {
if ($user->remind_expire)
$mailService->remindExpire($user);
if ($user->remind_traffic)
$mailService->remindTraffic($user);
$totalUsers = $mailService->getTotalUsersNeedRemind();
if ($totalUsers === 0) {
$this->info('没有需要发送提醒邮件的用户');
return 0;
}
$this->displayInfo($totalUsers, $chunkSize);
if (!$this->option('force') && !$this->confirm("确定要发送提醒邮件给 {$totalUsers} 个用户吗?")) {
return 0;
}
$startTime = microtime(true);
$progressBar = $this->output->createProgressBar(ceil($totalUsers / $chunkSize));
$progressBar->start();
$statistics = $mailService->processUsersInChunks($chunkSize, function () use ($progressBar) {
$progressBar->advance();
});
$progressBar->finish();
$this->newLine();
$this->displayResults($statistics, microtime(true) - $startTime);
$this->logResults($statistics);
return 0;
}
private function displayInfo(int $totalUsers, int $chunkSize): void
{
$this->table(['项目', '值'], [
['需要处理的用户', number_format($totalUsers)],
['批次大小', $chunkSize],
['预计批次', ceil($totalUsers / $chunkSize)],
]);
}
private function displayResults(array $stats, float $duration): void
{
$this->info('✅ 提醒邮件发送完成!');
$this->table(['统计项', '数量'], [
['总处理用户', number_format($stats['processed_users'])],
['过期提醒邮件', number_format($stats['expire_emails'])],
['流量提醒邮件', number_format($stats['traffic_emails'])],
['跳过用户', number_format($stats['skipped'])],
['错误数量', number_format($stats['errors'])],
['总耗时', round($duration, 2) . ' 秒'],
['平均速度', round($stats['processed_users'] / max($duration, 0.1), 1) . ' 用户/秒'],
]);
if ($stats['errors'] > 0) {
$this->warn("⚠️ 有 {$stats['errors']} 个用户的邮件发送失败,请检查日志");
}
}
private function logResults(array $statistics): void
{
Log::info('SendRemindMail命令执行完成', ['statistics' => $statistics]);
}
}

View File

@@ -3,12 +3,138 @@
namespace App\Services;
use App\Jobs\SendEmailJob;
use App\Models\MailLog;
use App\Models\User;
use App\Utils\CacheKey;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class MailService
{
/**
* 获取需要发送提醒的用户总数
*/
public function getTotalUsersNeedRemind(): int
{
return User::where(function ($query) {
$query->where('remind_expire', true)
->orWhere('remind_traffic', true);
})
->where('banned', false)
->whereNotNull('email')
->count();
}
/**
* 分块处理用户提醒邮件
*/
public function processUsersInChunks(int $chunkSize, callable $progressCallback = null): array
{
$statistics = [
'processed_users' => 0,
'expire_emails' => 0,
'traffic_emails' => 0,
'errors' => 0,
'skipped' => 0,
];
User::select('id', 'email', 'expired_at', 'transfer_enable', 'u', 'd', 'remind_expire', 'remind_traffic')
->where(function ($query) {
$query->where('remind_expire', true)
->orWhere('remind_traffic', true);
})
->where('banned', false)
->whereNotNull('email')
->chunk($chunkSize, function ($users) use (&$statistics, $progressCallback) {
$this->processUserChunk($users, $statistics);
if ($progressCallback) {
$progressCallback();
}
// 定期清理内存
if ($statistics['processed_users'] % 2500 === 0) {
gc_collect_cycles();
}
});
return $statistics;
}
/**
* 处理用户块
*/
private function processUserChunk($users, array &$statistics): void
{
foreach ($users as $user) {
try {
$statistics['processed_users']++;
$emailsSent = 0;
// 检查并发送过期提醒
if ($user->remind_expire && $this->shouldSendExpireRemind($user)) {
$this->remindExpire($user);
$statistics['expire_emails']++;
$emailsSent++;
}
// 检查并发送流量提醒
if ($user->remind_traffic && $this->shouldSendTrafficRemind($user)) {
$this->remindTraffic($user);
$statistics['traffic_emails']++;
$emailsSent++;
}
if ($emailsSent === 0) {
$statistics['skipped']++;
}
} catch (\Exception $e) {
$statistics['errors']++;
Log::error('发送提醒邮件失败', [
'user_id' => $user->id,
'email' => $user->email,
'error' => $e->getMessage()
]);
}
}
}
/**
* 检查是否应该发送过期提醒
*/
private function shouldSendExpireRemind(User $user): bool
{
if ($user->expired_at === NULL) {
return false;
}
$expiredAt = $user->expired_at;
$now = time();
if (($expiredAt - 86400) < $now && $expiredAt > $now) {
return true;
}
return false;
}
/**
* 检查是否应该发送流量提醒
*/
private function shouldSendTrafficRemind(User $user): bool
{
if ($user->transfer_enable <= 0) {
return false;
}
$usedBytes = $user->u + $user->d;
$usageRatio = $usedBytes / $user->transfer_enable;
// 流量使用超过80%时发送提醒
return $usageRatio >= 0.8;
}
public function remindTraffic(User $user)
{
if (!$user->remind_traffic)
@@ -20,6 +146,7 @@ class MailService
return;
if (!Cache::put($flag, 1, 24 * 3600))
return;
SendEmailJob::dispatch([
'email' => $user->email,
'subject' => __('The traffic usage in :app_name has reached 80%', [
@@ -35,8 +162,10 @@ class MailService
public function remindExpire(User $user)
{
if (!($user->expired_at !== NULL && ($user->expired_at - 86400) < time() && $user->expired_at > time()))
if (!$this->shouldSendExpireRemind($user)) {
return;
}
SendEmailJob::dispatch([
'email' => $user->email,
'subject' => __('The service in :app_name is about to expire', [
@@ -83,19 +212,19 @@ class MailService
public static function sendEmail(array $params)
{
if (admin_setting('email_host')) {
\Config::set('mail.host', admin_setting('email_host', config('mail.host')));
\Config::set('mail.port', admin_setting('email_port', config('mail.port')));
\Config::set('mail.encryption', admin_setting('email_encryption', config('mail.encryption')));
\Config::set('mail.username', admin_setting('email_username', config('mail.username')));
\Config::set('mail.password', admin_setting('email_password', config('mail.password')));
\Config::set('mail.from.address', admin_setting('email_from_address', config('mail.from.address')));
\Config::set('mail.from.name', admin_setting('app_name', 'XBoard'));
Config::set('mail.host', admin_setting('email_host', config('mail.host')));
Config::set('mail.port', admin_setting('email_port', config('mail.port')));
Config::set('mail.encryption', admin_setting('email_encryption', config('mail.encryption')));
Config::set('mail.username', admin_setting('email_username', config('mail.username')));
Config::set('mail.password', admin_setting('email_password', config('mail.password')));
Config::set('mail.from.address', admin_setting('email_from_address', config('mail.from.address')));
Config::set('mail.from.name', admin_setting('app_name', 'XBoard'));
}
$email = $params['email'];
$subject = $params['subject'];
$params['template_name'] = 'mail.' . admin_setting('email_template', 'default') . '.' . $params['template_name'];
try {
\Mail::send(
Mail::send(
$params['template_name'],
$params['template_value'],
function ($message) use ($email, $subject) {
@@ -104,6 +233,7 @@ class MailService
);
$error = null;
} catch (\Exception $e) {
Log::error($e);
$error = $e->getMessage();
}
$log = [
@@ -113,7 +243,7 @@ class MailService
'error' => $error,
'config' => config('mail')
];
\App\Models\MailLog::create($log);
MailLog::create($log);
return $log;
}
}