fix(api): 修复邮件队列超时并补齐调度进程

延长 SendEmailJob 超时并改为超时直接失败,补充重试退避、
失败日志与收件人脱敏,避免 send_email 队列批量超时重试。

新增 MAIL_TIMEOUT 与 QUEUE_RETRY_AFTER 配置,并抽出邮件运行时
配置与 HTML 内容服务,确保 Horizon 常驻进程使用最新邮件配置。

为 Docker、supervisor 与 compose 样例补齐 scheduler 进程,并在
节点管理端开启墙检测托管时立即触发一次检测,保证定时任务持续生效。
This commit is contained in:
yinjianm
2026-04-28 13:32:58 +08:00
parent 329d52f89f
commit a4e78b864a
36 changed files with 1359 additions and 107 deletions
+54 -4
View File
@@ -8,6 +8,9 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use RuntimeException;
use Throwable;
class SendEmailJob implements ShouldQueue
{
@@ -15,7 +18,8 @@ class SendEmailJob implements ShouldQueue
protected $params;
public $tries = 3;
public $timeout = 10;
public $timeout = 60;
public $failOnTimeout = true;
/**
* Create a new job instance.
@@ -33,7 +37,7 @@ class SendEmailJob implements ShouldQueue
*
* @return void
*/
public function handle()
public function handle(): void
{
$params = $this->params;
if (
@@ -46,9 +50,55 @@ class SendEmailJob implements ShouldQueue
$params['template_value']['content'] = (string) $params['template_value']['content'] . "\r\n\r\n[Send-Time: {$timestamp}]";
}
$mailLog = MailService::sendEmail($params);
$mailLog = $this->sendEmail($params);
if ($mailLog['error']) {
$this->release(60);
throw new RuntimeException('Failed to send email: ' . $mailLog['error']);
}
}
/**
* Return retry delays for transient mail delivery failures.
*/
public function backoff(): array
{
return [60, 300];
}
/**
* Record a failed send attempt without exposing the full recipient address.
*/
public function failed(Throwable $exception): void
{
Log::error('Send email job failed', [
'queue' => $this->queue,
'email' => $this->maskedEmail(),
'error' => $exception->getMessage(),
]);
}
/**
* Send the email payload.
*/
protected function sendEmail(array $params): array
{
return MailService::sendEmail($params);
}
/**
* Return a partially masked recipient address for operational logs.
*/
private function maskedEmail(): ?string
{
$email = $this->params['email'] ?? null;
if (!is_string($email) || !str_contains($email, '@')) {
return null;
}
[$local, $domain] = explode('@', $email, 2);
if ($local === '' || $domain === '') {
return null;
}
return substr($local, 0, 1) . str_repeat('*', max(1, strlen($local) - 1)) . '@' . $domain;
}
}