fix(api): 修复邮件队列超时并补齐调度进程
延长 SendEmailJob 超时并改为超时直接失败,补充重试退避、 失败日志与收件人脱敏,避免 send_email 队列批量超时重试。 新增 MAIL_TIMEOUT 与 QUEUE_RETRY_AFTER 配置,并抽出邮件运行时 配置与 HTML 内容服务,确保 Horizon 常驻进程使用最新邮件配置。 为 Docker、supervisor 与 compose 样例补齐 scheduler 进程,并在 节点管理端开启墙检测托管时立即触发一次检测,保证定时任务持续生效。
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\SendEmailJob;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
class SendEmailJobTest extends TestCase
|
||||
{
|
||||
public function test_job_uses_smtp_safe_timeout_and_backoff(): void
|
||||
{
|
||||
$job = new SendEmailJob([
|
||||
'email' => 'user@example.com',
|
||||
'subject' => 'Subject',
|
||||
'template_name' => 'notify',
|
||||
]);
|
||||
|
||||
$this->assertSame(3, $job->tries);
|
||||
$this->assertSame(60, $job->timeout);
|
||||
$this->assertTrue($job->failOnTimeout);
|
||||
$this->assertSame([60, 300], $job->backoff());
|
||||
}
|
||||
|
||||
public function test_handle_throws_when_mail_service_returns_error(): void
|
||||
{
|
||||
$job = new class([
|
||||
'email' => 'user@example.com',
|
||||
'subject' => 'Subject',
|
||||
'template_name' => 'notify',
|
||||
]) extends SendEmailJob {
|
||||
protected function sendEmail(array $params): array
|
||||
{
|
||||
return ['error' => 'SMTP connection failed'];
|
||||
}
|
||||
};
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('Failed to send email: SMTP connection failed');
|
||||
|
||||
$job->handle();
|
||||
}
|
||||
|
||||
public function test_mass_email_content_gets_send_time_marker_before_send(): void
|
||||
{
|
||||
$job = new class([
|
||||
'email' => 'user@example.com',
|
||||
'subject' => 'Subject',
|
||||
'template_name' => 'notify',
|
||||
'template_value' => [
|
||||
'content' => 'Hello',
|
||||
],
|
||||
], 'send_email_mass') extends SendEmailJob {
|
||||
public array $capturedParams = [];
|
||||
|
||||
protected function sendEmail(array $params): array
|
||||
{
|
||||
$this->capturedParams = $params;
|
||||
|
||||
return ['error' => null];
|
||||
}
|
||||
};
|
||||
|
||||
$job->handle();
|
||||
|
||||
$this->assertStringStartsWith('Hello', $job->capturedParams['template_value']['content']);
|
||||
$this->assertStringContainsString('[Send-Time:', $job->capturedParams['template_value']['content']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\MailRuntimeConfig;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MailServiceConfigTest extends TestCase
|
||||
{
|
||||
public function test_mail_log_config_masks_sensitive_values_recursively(): void
|
||||
{
|
||||
$config = [
|
||||
'host' => 'smtp.example.com',
|
||||
'password' => 'secret-password',
|
||||
'mailers' => [
|
||||
'smtp' => [
|
||||
'username' => 'mailer',
|
||||
'password' => 'nested-secret',
|
||||
'timeout' => 30,
|
||||
],
|
||||
'ses' => [
|
||||
'key' => 'aws-key',
|
||||
'secret' => 'aws-secret',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$sanitized = $this->mailConfigForLog($config);
|
||||
|
||||
$this->assertSame('smtp.example.com', $sanitized['host']);
|
||||
$this->assertSame('******', $sanitized['password']);
|
||||
$this->assertSame('mailer', $sanitized['mailers']['smtp']['username']);
|
||||
$this->assertSame('******', $sanitized['mailers']['smtp']['password']);
|
||||
$this->assertSame(30, $sanitized['mailers']['smtp']['timeout']);
|
||||
$this->assertSame('******', $sanitized['mailers']['ses']['key']);
|
||||
$this->assertSame('******', $sanitized['mailers']['ses']['secret']);
|
||||
}
|
||||
|
||||
public function test_mail_log_config_keeps_empty_sensitive_values_empty(): void
|
||||
{
|
||||
$sanitized = $this->mailConfigForLog([
|
||||
'password' => null,
|
||||
'secret' => '',
|
||||
'timeout' => 30,
|
||||
]);
|
||||
|
||||
$this->assertNull($sanitized['password']);
|
||||
$this->assertSame('', $sanitized['secret']);
|
||||
$this->assertSame(30, $sanitized['timeout']);
|
||||
}
|
||||
|
||||
private function mailConfigForLog(array $config): array
|
||||
{
|
||||
return MailRuntimeConfig::configForLog($config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user