Compare commits

...

2 Commits

25 changed files with 1228 additions and 119 deletions

View File

@ -0,0 +1,101 @@
# 证书异步任务接口接线说明
更新日期:2026年10月2日。依据当前源码;本批尚未部署,不代表生产验收通过。
## 业务范围
证书申请、当前证书部署及手动重试先持久化任务,再交给后台工作线程执行。浏览器断线不取消已接收工作,前端通过任务接口确认最终结果。提交成功、逐服务器推送成功、整体任务成功是不同层级,不能混用。
## 接口与权限
所有接口要求完成初始化、有效登录会话及来源校验;前端沿用现有带会话的 fetchAdmin,不在 URL 中携带凭据。任务与域名接口返回 private, no-store 和 Vary: Cookie,不使用共享缓存。
| 方法与路径 | 行为 | 权限及返回 |
|---|---|---|
| POST /api/v1/domains/:id/issue | 提交申请及关联部署,无请求体 | 可见域名、证书管理、部署执行、DNS 凭据使用权限;202 |
| POST /api/v1/domains/:id/deploy | 提交已有证书部署,无请求体 | 可见域名、部署执行权限;202 |
| POST /api/v1/tasks/:id/retry | 同一任务新增一次尝试,无请求体 | 重试、证书管理、部署执行权限;申请类还检查 DNS 凭据;202 |
| DELETE /api/v1/tasks/:id | 请求取消 | 任务可见及取消权限;后台任务返回 202,非异步历史任务可能返回 200 |
| GET /api/v1/tasks | 当前身份可见任务列表 | 支持 domain_id 和 limit;返回 200 与 items 数组 |
| GET /api/v1/tasks/:id | 单个任务最新状态 | 返回 200 与任务对象本身,不包 task 字段 |
| GET /api/v1/tasks/:id/attempts | 任务尝试历史 | 返回 200 与 items 数组 |
| GET /api/v1/tasks/:id/deployment-results | 逐尝试的逐服务器部署结果 | 返回 200 与 items 数组;前端按最近 attempt_number 筛选 |
列表 limit 默认 50,有效范围为 1~200,越界由仓储回退至 50。不存在和无权访问的任务都返回 403,不能根据接口泄露其他项目资源是否存在。
### 异步提交响应
返回 202 和 Location: /api/v1/tasks/任务编号。以下仅为字段示例,编号不对应真实环境:
```json
{
"accepted": true,
"task": {
"id": "example-task-id",
"domain_id": "example-domain-id",
"scope_type": "project",
"project_id": "example-project-id",
"task_type": "certificate_deploy",
"status": "queued",
"stage": "queued",
"message": "等待执行证书任务",
"created_at": "2026-10-02T08:00:00Z",
"updated_at": "2026-10-02T08:00:00Z"
}
}
```
202 的 task 是提交时快照,不保证仍处于 queued。重试响应可能已经是 running。前端必须使用后续 GET 状态作为依据,不凭提交响应显示成功。
### 幂等与错误
- 首次申请和部署支持 Idempotency-Key:长度 8~128,只允许英文字母、数字、连字符、下划线及英文句点。前端当前为每次明确操作生成新键,网络重发若需要去重必须复用原键。
- 同一域名、同一键命中已结束任务返回 200、task 和 replayed: true;命中活动任务返回 409、task 和 replayed: true,不再次执行。跨操作复用同一键返回 409。
- 同类活动任务、仍在收尾、成功任务重试或不支持的任务类型返回 409。部署的 partial_success 允许重试;申请类只允许 failed 或 cancelled。
- 取消 202 只说明接收取消意图,状态为 cancelling;不能提前显示已取消或允许重试。已经完成的外部操作仍可最终成功,并提示取消未撤销该操作。
- 400:幂等键等输入无效;401:登录失效;403:无权访问或来源错误;409:初始化未完成或状态冲突;500:持久化等内部错误;501:工作流未配置;503:权限服务未就绪、服务停止或队列已满。
- 错误响应一般含 error,可能带 request_id 或 task;网关也可能返回非 JSON 页面。解析失败只能提示暂时无法确认,不能推断任务已失败或自动再次提交。
## 数据模型
| 模型 | 字段 |
|---|---|
| Task | id、domain_id、task_type、status、stage、message、scope_type、project_id、created_at、updated_at;可选 operator_id、certificate_version_id、idempotency_key、started_at、finished_at |
| TaskAttempt | id、task_id、attempt_number、stage、status、message、scope_type、project_id、created_at;可选 started_at、finished_at |
| TaskDeploymentResult | task_id、attempt_id、attempt_number、result_index、server_id、server、status、message、created_at;可选 backup_id |
编号和时间为字符串,时间使用带时区的 RFC3339 表示;attempt_number 与 result_index 为整数。Task 与 TaskAttempt 的状态含义如下:
| 状态 | 中文含义 | 是否终态 |
|---|---|---|
| queued | 排队中 | 否 |
| running | 执行中 | 否 |
| retry_wait | 等待重试 | 否 |
| cancelling | 正在取消并等待收尾 | 否 |
| success | 已完成 | 是 |
| failed | 执行失败或结果未可靠确认 | 是 |
| cancelled | 已停止;不保证外部副作用全部撤销 | 是 |
| partial_success | 部分完成 | 是 |
未知状态不得默认视为成功。逐服务器结果不包含完整的站点验证及恢复报告,不能据此推导 HTTPS 校验成功。
## 执行及前端交互边界
- 当前后台最多接收 32 个在途任务,4 个执行槽,总执行预算 30 分钟;这些是实现限制,不是保证耗时。
- 队列满或关闭时拒绝执行,并尝试保存失败终态;持久化失败保留中断状态,不伪造已完成。
- 服务关闭停止接收、发送取消信号并等待工作线程收尾。启动恢复将中断记录标为失败,不自动重放外部签发、部署。
- 任务和本次尝试的终态通过同一数据库事务保存;逐目标结果和完成审计目前仍为独立写入,不能描述成所有记录完全原子化。
- 前端每三秒查询一次,单次周期十秒超时且不重叠;卸载后停止轮询、取消请求。
- 切换任务时丢弃旧选择的迟到响应;撤权清除详情及部署结果。一般查询故障保留最后有效状态并提示未知,不改变任务终态。
- 三个详情 GET 不是一次原子快照;任务终态与最大 attempt_number 的状态不一致时暂不合并,下一周期重新查询。
- 任务从活动变终态后刷新域名、证书、运行状态和审计,但不覆盖正在编辑的设置表单。
## 回归与未完成项
已加入自动用例:断线后继续执行、排队取消、取消收尾禁止重试、队列满、异常脱敏、读取和保存失败、跨任务尝试拒绝、终态事务回滚、已完成与部分成功不被取消意图覆盖、前端迟到响应和网络恢复。
- [~] 当前 Linux 自动回归持续执行;竞争检测尚无可靠通过结果。
- [ ] 完整验证及恢复报告持久化与页面展示仍需补齐。
- [ ] 完成审计、逐服务器结果与任务终态之间的跨记录一致性继续复核。
- [ ] 导入、版本恢复、校验及手动续期等其他长操作尚未全部异步化。
- [ ] 正式构建部署后验证超过反代等待窗口的真实操作、断线查询、取消回滚和服务重启;本说明不替代生产验收。

View File

@ -12,6 +12,7 @@ import (
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
"git.pcux.cn/xuans/Scert/backend/internal/repository"
)
func TestApplicationDeployRetryUsesExistingTask(t *testing.T) {
@ -62,21 +63,43 @@ func TestApplicationDeployRetryUsesExistingTask(t *testing.T) {
}
cookies = login.Result().Cookies()
initial := request("/api/v1/domains/task-domain/deploy", "")
if initial.Code != http.StatusBadGateway || calls.Load() != 1 {
t.Fatalf("首次故障未经过真实部署接线:%d %s", initial.Code, initial.Body.String())
if initial.Code != http.StatusAccepted {
t.Fatalf("首次部署没有异步接收:%d %s", initial.Code, initial.Body.String())
}
tasks, err := application.Tasks.ListTasks(t.Context(), "task-domain", 10)
if err != nil || len(tasks) != 1 || tasks[0].Status != "failed" {
t.Fatalf("首次部署未生成唯一失败任务:%+v %v", tasks, err)
waitTerminal := func(status string) []repository.Task {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
items, err := application.Tasks.ListTasks(t.Context(), "task-domain", 10)
if err != nil {
t.Fatal(err)
}
if len(items) == 1 && items[0].Status == status {
return items
}
time.Sleep(5 * time.Millisecond)
}
t.Fatalf("等待任务终态超时:%s", status)
return nil
}
tasks := waitTerminal("failed")
if calls.Load() != 1 {
t.Fatal("首次故障未经过真实部署接线")
}
task := tasks[0]
retryPath := "/api/v1/tasks/" + task.ID + "/retry"
done := make(chan *httptest.ResponseRecorder, 1)
go func() { done <- request(retryPath, "") }()
retry := request(retryPath, "")
// 终态持久化后执行器可能仍在释放资源,收尾期间重试必须保持拒绝。
deadline := time.Now().Add(5 * time.Second)
for retry.Code == http.StatusConflict && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
retry = request(retryPath, "")
}
if retry.Code != http.StatusAccepted {
t.Fatalf("真实部署重试未接收:%d %s", retry.Code, retry.Body.String())
}
select {
case <-started:
case response := <-done:
t.Fatalf("重试未进入原始部署工作流:%d %s", response.Code, response.Body.String())
case <-time.After(5 * time.Second):
t.Fatal("重试工作流启动超时")
}
@ -85,17 +108,9 @@ func TestApplicationDeployRetryUsesExistingTask(t *testing.T) {
t.Fatalf("并发重试未被阻止:%d %s", concurrent.Code, concurrent.Body.String())
}
release <- struct{}{}
select {
case response := <-done:
if response.Code != http.StatusOK {
t.Fatalf("真实应用部署重试失败:%d %s", response.Code, response.Body.String())
}
case <-time.After(5 * time.Second):
t.Fatal("重试工作流收尾超时")
}
tasks, err = application.Tasks.ListTasks(t.Context(), "task-domain", 10)
if err != nil || len(tasks) != 1 || tasks[0].ID != task.ID || tasks[0].Status != "success" {
t.Fatalf("重试重复创建任务或未成功:%+v %v", tasks, err)
tasks = waitTerminal("success")
if tasks[0].ID != task.ID {
t.Fatalf("重试重复创建任务:%+v", tasks)
}
attempts, err := application.Tasks.ListAttempts(t.Context(), task.ID)
if err != nil || len(attempts) != 2 {

View File

@ -219,7 +219,7 @@ func (s *Server) registerRoutes() {
domainAPI.POST("/domains", s.dnsOperation, s.createDomain)
domainAPI.PUT("/domains/:id", s.dnsOperation, s.updateDomain)
domainAPI.DELETE("/domains/:id", s.deleteDomain)
domainAPI.POST("/domains/:id/issue", s.longOperation(certificateTaskTimeout), s.issueDomain)
domainAPI.POST("/domains/:id/issue", s.issueDomain)
api := s.router.Group("/api/v1", privateProjectResponse, s.requireAuthenticated)
api.GET("/tasks", s.listTasks)
api.GET("/tasks/:id", s.getTask)
@ -237,7 +237,7 @@ func (s *Server) registerRoutes() {
adminAPI.POST("/renewal/run", s.requireSuperAdmin, s.longOperation(certificateTaskTimeout), s.runRenewalNow)
domainAPI.POST("/domains/:id/certificate/import", s.longOperation(certificateTaskTimeout), s.importCertificateFiles)
domainAPI.POST("/domains/:id/certificate/validate", s.longOperation(certificateTaskTimeout), s.validateCertificate)
domainAPI.POST("/domains/:id/deploy", s.longOperation(certificateTaskTimeout), s.deployCertificateCurrent)
domainAPI.POST("/domains/:id/deploy", s.deployCertificateCurrent)
domainAPI.POST("/domains/:id/certificate/versions/:version/restore", s.longOperation(certificateTaskTimeout), s.restoreCertificateVersion)
api.POST("/servers", s.createServer)
api.PUT("/servers/:id", s.updateServer)

View File

@ -170,19 +170,20 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
})
now := time.Now().UTC()
if err := executionCtx.Err(); err != nil {
s.finishTaskBeforeExecution(task, attempt, "cancelled", "任务在开始前已停止")
s.finishTaskContext(task, attempt, err)
return
}
if err := s.tasks.UpdateTask(executionCtx, task.ID, "running", stage, runningMessage, &now, nil); err != nil {
finished := time.Now().UTC()
failureMessage := "任务启动失败"
if s.finishCancelledTask(task, attempt, executionCtx) {
return
}
_ = s.tasks.UpdateTask(context.Background(), task.ID, "failed", stage, failureMessage, &now, &finished)
_ = s.tasks.FinishAttempt(context.Background(), attempt.ID, "failed", failureMessage, &finished)
attempt.Stage = stage
s.finishTaskBeforeExecution(task, attempt, "failed", failureMessage)
if s.audit != nil {
_ = s.audit.CreateAuditLog(context.Background(), repository.AuditLog{
auditCtx, auditCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer auditCancel()
_ = s.audit.CreateAuditLog(auditCtx, repository.AuditLog{
TaskID: task.ID, OperatorID: task.OperatorID,
Action: auditAction, ResourceType: "domain", ResourceID: task.DomainID,
Status: "error", Message: failureMessage,
@ -196,13 +197,12 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
Action: auditAction, ResourceType: "domain", ResourceID: task.DomainID,
Status: "running", Message: runningMessage,
}); err != nil {
finished := time.Now().UTC()
failureMessage := "任务启动审计记录失败"
if s.finishCancelledTask(task, attempt, executionCtx) {
return
}
_ = s.tasks.UpdateTask(context.Background(), task.ID, "failed", "record", failureMessage, &now, &finished)
_ = s.tasks.FinishAttempt(context.Background(), attempt.ID, "failed", failureMessage, &finished)
attempt.Stage = "record"
s.finishTaskBeforeExecution(task, attempt, "failed", failureMessage)
return
}
}
@ -216,7 +216,8 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
finishCtx, finishCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer finishCancel()
deploymentReport = sanitizeDeploymentReport(deploymentReport)
if recordErr := s.tasks.RecordTaskDeploymentResults(finishCtx, task.ID, attempt.ID, attempt.AttemptNumber, taskDeploymentResults(deploymentReport)); recordErr != nil {
recordErr := s.tasks.RecordTaskDeploymentResults(finishCtx, task.ID, attempt.ID, attempt.AttemptNumber, taskDeploymentResults(deploymentReport))
if recordErr != nil {
issueErr = errors.Join(issueErr, recordErr)
}
finished := time.Now().UTC()
@ -233,30 +234,29 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
if s.logger != nil {
s.logger.Error("证书任务执行失败", "task_id", task.ID, "domain_id", task.DomainID, "error", issueErr)
}
if errors.Is(issueErr, context.Canceled) {
if errors.Is(issueErr, context.Canceled) && !partial && deploymentReport.RecoveryStatus != "unconfirmed" && deploymentReport.RecoveryStatus != "failed" {
status = "cancelled"
}
} else if partial {
status = "partial_success"
}
if recordErr != nil {
status, message = "failed", "部署结果保存失败,请核查实际部署状态后再重试"
}
s.taskMu.Lock()
defer s.taskMu.Unlock()
if persisted, err := s.tasks.GetTask(finishCtx, task.ID); err == nil && (persisted.Status == "cancelled" || persisted.Status == "cancelling") {
status = "cancelled"
message = persisted.Message
if issueErr == nil {
issueErr = errors.New(message)
if persisted, err := s.tasks.GetTask(finishCtx, task.ID); err != nil {
status, message = "failed", "无法确认任务收尾状态,请核查部署结果后再重试"
issueErr = errors.Join(issueErr, err)
} else if persisted.Status == "cancelled" || persisted.Status == "cancelling" {
// 取消请求只是意图,不能覆盖已经完成的外部操作,也不能隐藏失败及部分成功。
if issueErr == nil && !partial {
message = "任务已完成,取消请求未撤销已完成的操作"
} else if status == "cancelled" {
status = "cancelled"
message = "任务已停止;已执行操作是否回滚请查看部署结果"
}
}
if err := s.tasks.FinishAttempt(finishCtx, attempt.ID, status, message, &finished); err != nil {
status, message = "failed", "任务尝试结果保存失败,请检查任务恢复状态"
}
task.Status = status
task.Stage = stage
task.Message = message
task.StartedAt = &now
task.FinishedAt = &finished
task.UpdatedAt = finished
if s.audit != nil {
auditStatus := status
if issueErr != nil && status != "cancelled" && status != "partial_success" {
@ -269,15 +269,28 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
status, message = "failed", "任务完成审计保存失败,请检查任务恢复状态"
}
}
if err := s.tasks.UpdateTask(finishCtx, task.ID, status, stage, message, &now, &finished); err != nil && s.logger != nil {
s.logger.Error("保存任务终态失败", "task_id", task.ID, "error", err)
if err := s.tasks.FinishTaskAttempt(finishCtx, repository.TaskCompletion{
TaskID: task.ID, AttemptID: attempt.ID, Status: status, Stage: stage,
Message: message, StartedAt: &now, FinishedAt: finished,
}); err != nil && s.logger != nil {
s.logger.Error("保存任务及尝试终态失败,保留中断恢复状态", "task_id", task.ID, "error", err)
}
}
func (s *Server) finishCancelledTask(task repository.Task, attempt repository.TaskAttempt, ctx context.Context) bool {
persisted, err := s.tasks.GetTask(context.Background(), task.ID)
if (err == nil && (persisted.Status == "cancelled" || persisted.Status == "cancelling")) || errors.Is(ctx.Err(), context.Canceled) {
s.finishTaskBeforeExecution(task, attempt, "cancelled", "任务已取消,未继续执行")
if ctx.Err() != nil {
s.finishTaskContext(task, attempt, ctx.Err())
return true
}
checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
persisted, err := s.tasks.GetTask(checkCtx, task.ID)
if err != nil {
s.finishTaskBeforeExecution(task, attempt, "failed", "无法确认任务状态,未执行外部操作")
return true
}
if persisted.Status == "cancelled" || persisted.Status == "cancelling" {
s.finishTaskContext(task, attempt, context.Canceled)
return true
}
return false

View File

@ -0,0 +1,129 @@
package httpapi
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
"git.pcux.cn/xuans/Scert/backend/internal/repository"
)
func TestAsyncCancellationPreservesActualOperationOutcome(t *testing.T) {
for _, test := range []struct {
name string
report deployment.Report
err error
want string
}{
{name: "already_completed", report: deployment.Report{Status: "success"}, want: "success"},
{name: "failed", report: deployment.Report{Status: "failed"}, err: errors.New("部署收尾失败"), want: "failed"},
{name: "partially_completed", report: deployment.Report{Status: "partial"}, err: context.Canceled, want: "partial_success"},
{name: "cancelled", err: context.Canceled, want: "cancelled"},
{name: "recovery_unconfirmed", report: deployment.Report{Status: "failed", RecoveryStatus: "unconfirmed"}, err: errors.Join(context.Canceled, errors.New("回滚未确认")), want: "failed"},
{name: "recovery_failed", report: deployment.Report{Status: "failed", RecoveryStatus: "failed"}, err: errors.Join(context.Canceled, errors.New("回滚失败")), want: "failed"},
} {
t.Run(test.name, func(t *testing.T) {
started := make(chan struct{})
server, tasks, _, cleanup := newTaskAPITestServer(t, func(ctx context.Context, _ string) (certificate.IssueResult, deployment.Report, error) {
close(started)
<-ctx.Done()
return certificate.IssueResult{}, test.report, test.err
})
defer cleanup()
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
task := submittedTask(t, response)
select {
case <-started:
case <-time.After(2 * time.Second):
t.Fatal("任务没有开始")
}
response = httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodDelete, "/api/v1/tasks/"+task.ID, nil))
if response.Code != http.StatusAccepted {
t.Fatalf("取消未被接收:%s", response.Body.String())
}
waitTaskWorkersForTest(t, server)
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status != test.want {
t.Fatalf("取消请求覆盖了实际结果:%+v %v", stored, err)
}
attempts, err := tasks.ListAttempts(t.Context(), task.ID)
if err != nil || len(attempts) != 1 || attempts[0].Status != stored.Status {
t.Fatalf("任务与尝试不一致:%+v %v", attempts, err)
}
if test.want == "success" && !strings.Contains(stored.Message, "未撤销") {
t.Fatal("必须提示取消未撤销已完成操作")
}
})
}
}
type failingCompletionTaskRepository struct {
repository.TaskRepository
operationFinished *atomic.Bool
failRead bool
}
func (r failingCompletionTaskRepository) GetTask(ctx context.Context, id string) (repository.Task, error) {
if r.failRead && r.operationFinished.Load() {
return repository.Task{}, errors.New("模拟收尾状态读取失败")
}
return r.TaskRepository.GetTask(ctx, id)
}
func (r failingCompletionTaskRepository) FinishTaskAttempt(ctx context.Context, item repository.TaskCompletion) error {
if !r.failRead {
return errors.New("模拟收尾事务保存失败")
}
return r.TaskRepository.FinishTaskAttempt(ctx, item)
}
func TestAsyncCompletionFailureDoesNotPublishFalseSuccess(t *testing.T) {
for _, failRead := range []bool{true, false} {
name := "atomic_write_failure"
if failRead {
name = "state_read_failure"
}
t.Run(name, func(t *testing.T) {
var finished atomic.Bool
server, tasks, _, cleanup := newTaskAPITestServer(t, func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
finished.Store(true)
return certificate.IssueResult{}, deployment.Report{Status: "success"}, nil
})
defer cleanup()
server.tasks = failingCompletionTaskRepository{TaskRepository: tasks, operationFinished: &finished, failRead: failRead}
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
task := submittedTask(t, response)
waitTaskWorkersForTest(t, server)
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status == "success" {
t.Fatalf("无法可靠持久化却标记成功:%+v %v", stored, err)
}
attempts, err := tasks.ListAttempts(t.Context(), task.ID)
if err != nil || len(attempts) != 1 || attempts[0].Status == "success" {
t.Fatalf("尝试被单独保存为成功:%+v %v", attempts, err)
}
if failRead {
if stored.Status != "failed" || attempts[0].Status != "failed" {
t.Fatal("状态读取失败必须闭合为失败")
}
} else {
if !isActiveTaskStatus(stored.Status) || stored.FinishedAt != nil || attempts[0].FinishedAt != nil {
t.Fatal("事务失败必须保留可由启动恢复接管的中断状态")
}
if count, err := tasks.RecoverInterruptedTasks(t.Context(), time.Now().UTC()); err != nil || count != 1 {
t.Fatalf("中断记录无法恢复:%d %v", count, err)
}
}
})
}
}

View File

@ -28,9 +28,10 @@ func TestTaskDeploymentResultsIssueAndRead(t *testing.T) {
defer closeDB()
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
if response.Code != http.StatusMultiStatus {
t.Fatalf("部分部署失败响应错误:%d %s", response.Code, response.Body.String())
if response.Code != http.StatusAccepted {
t.Fatalf("任务提交响应错误:%d %s", response.Code, response.Body.String())
}
waitTaskWorkersForTest(t, server)
items, err := tasks.ListTasks(t.Context(), "", 10)
if err != nil || len(items) != 1 || items[0].Status != "partial_success" {
t.Fatalf("部分成功任务记录错误:%+v %v", items, err)
@ -86,9 +87,10 @@ func TestTaskDeploymentResultsKeepAllowlistedLockDiagnosis(t *testing.T) {
defer closeDB()
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
if response.Code != http.StatusBadGateway || strings.Contains(response.Body.String(), "secret-path") || !strings.Contains(response.Body.String(), "lock_unavailable") {
t.Fatalf("部署接口缺少安全的锁诊断:%d %s", response.Code, response.Body.String())
if response.Code != http.StatusAccepted || strings.Contains(response.Body.String(), "secret-path") {
t.Fatalf("任务提交失败或泄露诊断原文:%d %s", response.Code, response.Body.String())
}
waitTaskWorkersForTest(t, server)
items, err := tasks.ListTasks(t.Context(), "", 10)
if err != nil || len(items) != 1 {
t.Fatalf("任务未保存:%+v %v", items, err)
@ -116,21 +118,19 @@ func TestTaskDeploymentResultsHTTPSFailurePreservesTransferEvidence(t *testing.T
defer closeDB()
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
if response.Code != http.StatusBadGateway {
t.Fatalf("HTTPS 校验失败应返回失败响应:%d %s", response.Code, response.Body.String())
if response.Code != http.StatusAccepted {
t.Fatalf("任务提交失败:%d %s", response.Code, response.Body.String())
}
var payload struct {
Deployment deployment.Report `json:"deployment"`
TaskStatus string `json:"task_status"`
}
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
waitTaskWorkersForTest(t, server)
location := response.Header().Get("Location")
response = httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodGet, location, nil))
var task repository.Task
if err := json.Unmarshal(response.Body.Bytes(), &task); err != nil {
t.Fatal(err)
}
if payload.TaskStatus != "failed" || payload.Deployment.Status != "failed" || payload.Deployment.VerificationStatus != "failed" {
t.Fatalf("接口未保留整体失败与站点校验失败:%+v", payload)
}
if payload.Deployment.Success != 1 || payload.Deployment.Results[0].Status != "success" || strings.Contains(response.Body.String(), "/secret-path") {
t.Fatalf("接口丢失真实推送结果或泄露底层错误:%s", response.Body.String())
if response.Code != http.StatusOK || task.Status != "failed" || strings.Contains(response.Body.String(), "/secret-path") {
t.Fatalf("状态查询未保留整体失败或泄露底层错误:%d %s", response.Code, response.Body.String())
}
items, err := tasks.ListTasks(t.Context(), "", 10)
if err != nil || len(items) != 1 || items[0].Status != "failed" {

View File

@ -2,6 +2,7 @@ package httpapi
import (
"context"
"errors"
"net/http"
"time"
@ -47,7 +48,7 @@ func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, at
case s.taskSlots <- struct{}{}:
defer func() { <-s.taskSlots }()
case <-ctx.Done():
s.finishTaskBeforeExecution(task, attempt, "cancelled", "任务在开始前已停止")
s.finishTaskContext(task, attempt, ctx.Err())
return
}
s.runCertificateTask(ctx, task, attempt, action, operation, stage, runningMessage, successMessage)
@ -56,15 +57,23 @@ func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, at
c.JSON(http.StatusAccepted, gin.H{"task": task, "accepted": true})
}
func (s *Server) finishTaskContext(task repository.Task, attempt repository.TaskAttempt, err error) {
if errors.Is(err, context.DeadlineExceeded) {
s.finishTaskBeforeExecution(task, attempt, "failed", "任务执行超时,未继续执行")
return
}
s.finishTaskBeforeExecution(task, attempt, "cancelled", "任务已取消,未继续执行")
}
func (s *Server) finishTaskBeforeExecution(task repository.Task, attempt repository.TaskAttempt, status, message string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
finished := time.Now().UTC()
if err := s.tasks.FinishAttempt(ctx, attempt.ID, status, message, &finished); err != nil && s.logger != nil {
s.logger.Error("保存任务尝试失败", "task_id", task.ID, "error", err)
}
if err := s.tasks.UpdateTask(ctx, task.ID, status, attempt.Stage, message, nil, &finished); err != nil && s.logger != nil {
s.logger.Error("保存任务终态失败", "task_id", task.ID, "error", err)
if err := s.tasks.FinishTaskAttempt(ctx, repository.TaskCompletion{
TaskID: task.ID, AttemptID: attempt.ID, Status: status, Stage: attempt.Stage,
Message: message, FinishedAt: finished,
}); err != nil && s.logger != nil {
s.logger.Error("保存任务及尝试终态失败,保留中断恢复状态", "task_id", task.ID, "error", err)
}
}

View File

@ -6,6 +6,8 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
@ -15,6 +17,128 @@ import (
"git.pcux.cn/xuans/Scert/backend/internal/repository"
)
func TestAsyncQueueOverloadClosesPersistedTask(t *testing.T) {
server, tasks, _, cleanup := newTaskAPITestServer(t, func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
t.Error("队列已满时不能执行外部操作")
return certificate.IssueResult{}, deployment.Report{}, nil
})
defer cleanup()
server.asyncTasks = make(map[string]bool)
for i := 0; i < 32; i++ {
server.asyncTasks[strconv.Itoa(i)] = true
}
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
if response.Code != http.StatusServiceUnavailable {
t.Fatalf("队列过载未拒绝:%d %s", response.Code, response.Body.String())
}
items, err := tasks.ListTasks(t.Context(), "", 10)
if err != nil || len(items) != 1 || items[0].Status != "failed" {
t.Fatalf("过载后遗留活动任务:%+v %v", items, err)
}
attempts, err := tasks.ListAttempts(t.Context(), items[0].ID)
if err != nil || len(attempts) != 1 || attempts[0].Status != "failed" {
t.Fatalf("过载后尝试未关闭:%+v %v", attempts, err)
}
}
func TestAsyncQueuedCancellationNeverStartsOperation(t *testing.T) {
server, tasks, _, cleanup := newTaskAPITestServer(t, func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
t.Error("排队期间取消的任务不能执行外部操作")
return certificate.IssueResult{}, deployment.Report{}, nil
})
defer cleanup()
server.asyncTasks = make(map[string]bool)
server.taskSlots = make(chan struct{}, 4)
for i := 0; i < 4; i++ {
server.taskSlots <- struct{}{}
}
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
task := submittedTask(t, response)
response = httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodDelete, "/api/v1/tasks/"+task.ID, nil))
if response.Code != http.StatusAccepted {
t.Fatalf("排队取消未接收:%d %s", response.Code, response.Body.String())
}
waitTaskWorkersForTest(t, server)
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status != "cancelled" {
t.Fatalf("排队取消终态错误:%+v %v", stored, err)
}
}
func TestAsyncPanicClosesTaskWithoutExposingPanic(t *testing.T) {
server, tasks, _, cleanup := newTaskAPITestServer(t, func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
panic("private-panic-secret")
})
defer cleanup()
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
task := submittedTask(t, response)
waitTaskWorkersForTest(t, server)
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status != "failed" || strings.Contains(stored.Message, "private-panic-secret") {
t.Fatalf("异常任务未安全关闭:%+v %v", stored, err)
}
attempts, err := tasks.ListAttempts(t.Context(), task.ID)
if err != nil || len(attempts) != 1 || attempts[0].Status != "failed" || strings.Contains(attempts[0].Message, "private-panic-secret") {
t.Fatalf("异常尝试未安全关闭:%+v %v", attempts, err)
}
}
func TestAsyncDeadlineBeforeExecutionIsFailureNotUserCancellation(t *testing.T) {
server, tasks, _, cleanup := newTaskAPITestServer(t, nil)
defer cleanup()
task := repository.Task{ID: "deadline-task", DomainID: "domain-1", TaskType: "certificate_issue", Status: "queued", Stage: "queued"}
attempt := repository.TaskAttempt{ID: "deadline-attempt", TaskID: task.ID, AttemptNumber: 1, Status: "queued", Stage: "queued"}
if err := tasks.CreateTask(t.Context(), task); err != nil {
t.Fatal(err)
}
if err := tasks.CreateAttempt(t.Context(), attempt); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-time.Second))
defer cancel()
server.runCertificateTask(ctx, task, attempt, "certificate_issue", func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
t.Error("超时任务不能执行外部操作")
return certificate.IssueResult{}, deployment.Report{}, nil
}, "issue", "执行中", "执行完成")
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status != "failed" || !strings.Contains(stored.Message, "超时") {
t.Fatalf("超时被误记为用户取消:%+v %v", stored, err)
}
}
type failingTaskFinishAuditRepository struct{ repository.AuditRepository }
func (r failingTaskFinishAuditRepository) CreateAuditLog(ctx context.Context, item repository.AuditLog) error {
if item.Status == "success" {
return errors.New("模拟完成审计写入失败")
}
return r.AuditRepository.CreateAuditLog(ctx, item)
}
func TestAsyncFinishAuditFailureDoesNotLeaveSuccessfulAttempt(t *testing.T) {
server, tasks, audit, cleanup := newTaskAPITestServer(t, func(context.Context, string) (certificate.IssueResult, deployment.Report, error) {
return certificate.IssueResult{}, deployment.Report{Status: "success"}, nil
})
defer cleanup()
server.audit = failingTaskFinishAuditRepository{audit}
response := httptest.NewRecorder()
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
task := submittedTask(t, response)
waitTaskWorkersForTest(t, server)
stored, err := tasks.GetTask(t.Context(), task.ID)
if err != nil || stored.Status != "failed" {
t.Fatalf("完成审计失败却标记成功:%+v %v", stored, err)
}
attempts, err := tasks.ListAttempts(t.Context(), task.ID)
if err != nil || len(attempts) != 1 || attempts[0].Status != "failed" || attempts[0].Message != stored.Message {
t.Fatalf("任务与尝试终态不一致:%+v %v", attempts, err)
}
}
func submittedTask(t *testing.T, response *httptest.ResponseRecorder) repository.Task {
t.Helper()
if response.Code != http.StatusAccepted {

View File

@ -0,0 +1,73 @@
package repository
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"time"
)
// TaskCompletion 同时关闭任务与对应尝试,避免只保存其中一方的终态。
type TaskCompletion struct {
TaskID string
AttemptID string
Status string
Stage string
Message string
StartedAt *time.Time
FinishedAt time.Time
}
func (r *SQLTaskRepository) FinishTaskAttempt(ctx context.Context, completion TaskCompletion) error {
if r == nil || r.db == nil {
return errors.New("数据库连接不能为空")
}
completion.TaskID = strings.TrimSpace(completion.TaskID)
completion.AttemptID = strings.TrimSpace(completion.AttemptID)
completion.Stage = strings.TrimSpace(completion.Stage)
if completion.TaskID == "" || completion.AttemptID == "" || completion.Stage == "" || completion.FinishedAt.IsZero() {
return errors.New("任务终态记录不完整")
}
switch completion.Status {
case "success", "failed", "cancelled", "partial_success":
default:
return errors.New("任务终态无效")
}
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("开始任务收尾事务失败:%w", err)
}
defer tx.Rollback()
// 与取消、重试保持任务在前、尝试在后的加锁顺序。旧尝试或跨任务尝试必须整体回滚。
result, err := tx.ExecContext(ctx, r.query(`UPDATE tasks
SET status = ?, stage = ?, message = ?, started_at = COALESCE(?, started_at), finished_at = ?, updated_at = ?
WHERE id = ? AND status IN ('queued', 'running', 'retry_wait', 'cancelling')`),
completion.Status, completion.Stage, completion.Message, nullableTime(completion.StartedAt),
completion.FinishedAt.UTC(), completion.FinishedAt.UTC(), completion.TaskID)
if err != nil {
return fmt.Errorf("保存任务终态失败:%w", err)
}
if count, err := result.RowsAffected(); err != nil {
return fmt.Errorf("读取任务收尾结果失败:%w", err)
} else if count != 1 {
return sql.ErrNoRows
}
result, err = tx.ExecContext(ctx, r.query(`UPDATE task_attempts
SET status = ?, stage = ?, message = ?, finished_at = ?
WHERE id = ? AND task_id = ? AND status IN ('queued', 'running', 'retry_wait', 'cancelling')`),
completion.Status, completion.Stage, completion.Message, completion.FinishedAt.UTC(), completion.AttemptID, completion.TaskID)
if err != nil {
return fmt.Errorf("保存任务尝试终态失败:%w", err)
}
if count, err := result.RowsAffected(); err != nil {
return fmt.Errorf("读取任务尝试收尾结果失败:%w", err)
} else if count != 1 {
return sql.ErrNoRows
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("提交任务收尾事务失败:%w", err)
}
return nil
}

View File

@ -0,0 +1,99 @@
package repository
import (
"database/sql"
"errors"
"testing"
"time"
)
func seedTaskCompletion(t *testing.T, repo *SQLTaskRepository) TaskCompletion {
t.Helper()
started := time.Now().UTC().Add(-time.Minute)
if err := repo.CreateTask(t.Context(), Task{
ID: "completion-task", DomainID: "domain-1", TaskType: "certificate_deploy",
Status: "running", Stage: "deployment", StartedAt: &started,
}); err != nil {
t.Fatal(err)
}
if err := repo.CreateAttempt(t.Context(), TaskAttempt{
ID: "completion-attempt", TaskID: "completion-task", AttemptNumber: 1,
Status: "running", Stage: "deployment", StartedAt: &started,
}); err != nil {
t.Fatal(err)
}
return TaskCompletion{
TaskID: "completion-task", AttemptID: "completion-attempt", Status: "success",
Stage: "deployment", Message: "部署完成", FinishedAt: time.Now().UTC(),
}
}
func TestTaskCompletionPersistsBothTerminalRecords(t *testing.T) {
for _, status := range []string{"success", "failed", "cancelled", "partial_success"} {
t.Run(status, func(t *testing.T) {
repo, db := newTaskRepositoryTest(t)
defer db.Close()
completion := seedTaskCompletion(t, repo)
completion.Status = status
if err := repo.FinishTaskAttempt(t.Context(), completion); err != nil {
t.Fatal(err)
}
task, err := repo.GetTask(t.Context(), completion.TaskID)
if err != nil || task.Status != status || task.StartedAt == nil || task.FinishedAt == nil || task.Message != completion.Message {
t.Fatalf("任务终态未完整保存:%+v %v", task, err)
}
attempts, err := repo.ListAttempts(t.Context(), completion.TaskID)
if err != nil || len(attempts) != 1 || attempts[0].Status != status || attempts[0].FinishedAt == nil || !attempts[0].FinishedAt.Equal(*task.FinishedAt) || attempts[0].Message != task.Message {
t.Fatalf("任务和尝试终态不一致:%+v %v", attempts, err)
}
if err := repo.FinishTaskAttempt(t.Context(), completion); !errors.Is(err, sql.ErrNoRows) {
t.Fatalf("终态不能被迟到的收尾重复覆盖:%v", err)
}
})
}
}
func TestTaskCompletionRollsBackWhenAttemptCannotFinish(t *testing.T) {
for _, failure := range []string{"write_failure", "missing_attempt", "foreign_attempt", "stale_attempt", "invalid_status"} {
t.Run(failure, func(t *testing.T) {
repo, db := newTaskRepositoryTest(t)
defer db.Close()
completion := seedTaskCompletion(t, repo)
wantAttemptStatus := "running"
switch failure {
case "write_failure":
if _, err := db.ExecContext(t.Context(), `CREATE TRIGGER reject_attempt_completion BEFORE UPDATE ON task_attempts BEGIN SELECT RAISE(ABORT, 'injected completion failure'); END`); err != nil {
t.Fatal(err)
}
case "missing_attempt":
completion.AttemptID = "missing"
case "foreign_attempt":
if err := repo.CreateTask(t.Context(), Task{ID: "foreign-task", DomainID: "domain-2", TaskType: "certificate_deploy", Status: "running", Stage: "deployment"}); err != nil {
t.Fatal(err)
}
if err := repo.CreateAttempt(t.Context(), TaskAttempt{ID: "foreign-attempt", TaskID: "foreign-task", AttemptNumber: 1, Status: "running", Stage: "deployment"}); err != nil {
t.Fatal(err)
}
completion.AttemptID = "foreign-attempt"
case "stale_attempt":
if err := repo.FinishAttempt(t.Context(), completion.AttemptID, "failed", "此前失败", &completion.FinishedAt); err != nil {
t.Fatal(err)
}
wantAttemptStatus = "failed"
case "invalid_status":
completion.Status = "running"
}
if err := repo.FinishTaskAttempt(t.Context(), completion); err == nil {
t.Fatal("应拒绝无法同时关闭任务和尝试的操作")
}
task, err := repo.GetTask(t.Context(), completion.TaskID)
if err != nil || task.Status != "running" || task.FinishedAt != nil {
t.Fatalf("尝试保存失败后任务未回滚:%+v %v", task, err)
}
attempts, err := repo.ListAttempts(t.Context(), completion.TaskID)
if err != nil || len(attempts) != 1 || attempts[0].Status != wantAttemptStatus {
t.Fatalf("失败操作修改了尝试:%+v %v", attempts, err)
}
})
}
}

View File

@ -68,6 +68,7 @@ type TaskRepository interface {
FindTaskByIdempotencyKey(context.Context, string, string) (Task, error)
CreateAttempt(context.Context, TaskAttempt) error
FinishAttempt(context.Context, string, string, string, *time.Time) error
FinishTaskAttempt(context.Context, TaskCompletion) error
ListAttempts(context.Context, string) ([]TaskAttempt, error)
RecordTaskDeploymentResults(context.Context, string, string, int, []TaskDeploymentResult) error
ListTaskDeploymentResults(context.Context, string) ([]TaskDeploymentResult, error)

View File

@ -180,7 +180,7 @@ func renderUnitFile(options Options) (string, error) {
"Environment=SCERT_BOOTSTRAP_FIREWALL_MODE=external",
"Environment=SCERT_SERVER_HOST=127.0.0.1",
"KillSignal=SIGTERM",
"TimeoutStopSec=30",
"TimeoutStopSec=180",
"Restart=on-failure",
"UMask=0077",
"RuntimeDirectory=scert",

View File

@ -35,6 +35,7 @@ func TestRenderUnitFileIncludesExecutableConfigurationAndWorkDir(t *testing.T) {
"Type=notify",
"NotifyAccess=main",
"TimeoutStartSec=90",
"TimeoutStopSec=180",
"User=Scert",
"Group=Scert",
"WorkingDirectory=\"/opt/Scert data\"",
@ -78,6 +79,7 @@ func TestPackagedUnitAllowsInterfaceDiscoveryWithoutCapabilities(t *testing.T) {
"Type=notify",
"NotifyAccess=main",
"TimeoutStartSec=90",
"TimeoutStopSec=180",
"CapabilityBoundingSet=",
"AmbientCapabilities=",
"NoNewPrivileges=yes",

View File

@ -17,7 +17,8 @@ ExecStart=/scert/scert --config /scert/conf/scert.json serve
Environment=SCERT_BOOTSTRAP_FIREWALL_MODE=external
Environment=SCERT_SERVER_HOST=127.0.0.1
KillSignal=SIGTERM
TimeoutStopSec=30
# 为任务取消、部署回滚及持久化收尾预留时间,超过预算仍由启动恢复处理。
TimeoutStopSec=180
Restart=on-failure
UMask=0077
RuntimeDirectory=scert

View File

@ -1,5 +1,30 @@
# Linux 生产流程测试记录
## 2026年10月2日17:11 任务收尾事务与服务配置回归(未部署)
- [x] 第二台隔离源码首次关联回归:internal/httpapi、internal/repository、internal/servicehost、internal/app 全部通过,COMPLETION_FIRST_EXIT=0;日志为 /var/tmp/scert-async-20261002-1556/completion-first.log。
- [x] 补充回滚失败的取消边界后,使用非特权用户、GOMAXPROCS=2、包并行数一执行完整后端测试和 go vet,COMPLETION_FULL_EXIT=0、COMPLETION_VET_EXIT=0;日志为同目录 completion-full.log 与 completion-vet.log。
- [x] 终态仓储失败注入、任务与尝试一致性、启动恢复接管、取消不覆盖实际结果、动态及静态单元预算断言均纳入本批测试;数据库用例为隔离 SQLite,未触及线上业务库。
- [~] 同目录 completion-repeat.log 正在记录异步、终态事务及服务配置二十次普通重复回归;completion-unit.log 将记录静态单元检查。
- [!] 第一台 race-second.log 的竞争检测尚未取得退出状态。新建本机 SSH 会话无握手结果;从第二台按固定 ED25519 指纹和密码方式连接同样在 banner exchange 超时。原执行会话无输出,已发送中断但无确认,不以本地句柄存活断言远端仍正常运行,也不擅自重复启动测试。
- [!] Git 推送出现两次连接超时,本地提交 9e666be 已保存。网络问题不能算产品用例失败,也不能掩盖缺失的竞争检测证据。
- [!] 本批没有安装线上程序、清空业务数据库、开放外部端口、关闭 TLS 校验或创建辅助验收脚本。最终构建部署、三数据库首次 Web 流程和生产复审仍需独立完成。
## 2026年10月2日16:47 异步任务开发回归记录(未部署)
### 已完成的开发回归
- [x] 本机前端:npm.cmd test 为 252 项通过、零失败、零跳过;npm.cmd run build 通过。日志为 .runtime/async-frontend-20261002-final.log 与 .runtime/async-frontend-build-20261002-final.log。
- [x] 第二台隔离源码、非特权用户执行 go test ./... -count=1 和 go vet ./... 均返回零;日志位于 /var/tmp/scert-async-20261002-1556/backend-full.log 与 backend-vet.log。首次旧同步接口断言失败已修复,原始日志保留。
- [~] 第二台竞争检测因未启用 CGO 且无本机编译器未执行;第一台首次离线代理缺少依赖也未执行用例。已复用第二台公共模块缓存、三端校验归档摘要一致,在第一台启用 CGO 后重新执行异步专项竞争检测二十次,等待结果。
### 证据边界和剩余任务
- [~] 复核发现分发单元与动态安装模板的停止预算尚未同步,继续修复并回归,不以静态文件修改代替实际安装行为。
- [!] 本批未替换线上候选,未执行浏览器首次初始化,未清理线上数据库,未新增辅助验收脚本,也未降低 TLS 或 SSH 主机身份校验。
- [ ] 正式候选统一部署后仍需验证长任务超过反代等待窗口、断线后查询、取消回滚、进程重启恢复及三数据库首次完整 Web 流程。
- [!] 下方 504 与 502 为真实历史失败,异步开发回归通过并不自动关闭实机问题;旧事务、系统通知、备份恢复及总体生产门禁继续保留。
## 2026年10月2日15:31 正式候选实机负向恢复与重试
### 范围和准备

View File

@ -1,5 +1,32 @@
# Scert 整改任务执行清单
## 2026年10月2日17:11 异步收尾一致性整改
本节接续 16:47 的任务,不缩减原始 Linux 上线范围;本批仍未部署。
- [x] 修复动态安装模板仍为三十秒的遗漏:动态模板和分发单元统一停止预算一百八十秒,原生 Linux 两种路径的断言通过。该预算不等于已证明所有关闭路径无中断。
- [x] 新增任务和执行尝试的同事务终态保存;第二条写入失败、尝试缺失、跨任务尝试、过期尝试和重复终态均拒绝部分提交。覆盖数据库触发器注入失败后的回滚。
- [x] 取消请求不覆盖已完成、部分成功、回滚失败或未确认的真实结果;收尾状态读取或逐服务器结果保存失败不发布成功;终态事务失败保留中断记录供启动恢复。
- [x] 第二台隔离 Linux 四个关联包首次回归通过;最新完整 go test -p 1 ./... -count=1 与 go vet -p 1 ./... 均通过。新增仓储用例使用隔离 SQLite,不冒充三数据库完整流程通过。
- [~] 异步、终态事务、动态和静态服务配置专项重复二十次运行中;普通重复回归不能替代竞争检测。
- [!] 第一台竞争检测尚无可靠结果;本机和第二台新增 SSH 连接都未完成握手,原会话无输出。已向原会话发送中断信号,但未确认远端停止,不重复启动负载、不推断原因、不记通过。
- [x] 已补充[异步接口接线说明](../.claude/docs/ai/async-tasks/api-handoff.md),记录 202、取消语义、查询结构、权限、错误及未完成边界;不修改 Smail API 参考文档。
- [!] 开发基线提交 9e666be 已保存在本地,推送两次均连接超时,尚无远程同步成功证据;不进行强制推送。
- [ ] 后续继续完整验证与恢复报告持久化、完成审计与结果跨记录一致性、其他长操作异步化及关停边界;原有恢复、通知重置、三库首次 Web、备份恢复及统一验收门禁不变。
## 2026年10月2日16:47 异步开发与自动回归进度
本节更新下方 15:43 的早期状态。范围仍为 Linux;先完成开发和自动回归,再统一正式部署验收。开发回归使用隔离源码与临时数据库,不清理线上业务数据。
- [x] 申请、首次部署、重试持久化任务与尝试后返回 202;后台上下文不依赖浏览器连接,保留项目授权、幂等键和任务历史。
- [x] 已实现任务取消收尾、关闭时拒绝新工作、有限执行槽、超时和异常终态;补充排队取消、队列满、异常脱敏和审计保存失败回归,等待竞争检测及复审。
- [x] 前端已接入任务查询、最近尝试的逐服务器结果、刷新后查看及撤权清缓存;查询失败不自动重新提交,提交成功不等于部署成功。
- [x] 本机前端 252 项测试全部通过,生产构建通过;第二台隔离 Linux 全后端测试与 go vet 通过。上述结果均非正式部署验收。
- [~] 第一台使用现有编译器运行异步专项竞争检测,重复二十次;第二台缺少编译器、第一台首次缺少离线依赖的记录保留,不将环境失败计作产品用例失败或通过。
- [~] 复核发现静态 systemd 单元已增加停止预算,但动态安装模板仍为三十秒;下一步同步模板并增加一致性测试。
- [ ] 补齐异步查询结果契约、持久化失败及取消提交竞态的复核;其他长操作仍需逐项整改。
- [!] 本批代码未部署,重启 502、两条缺证据历史事务、系统通知与密码重置、三数据库首次 Web 流程、备份恢复及最终生产门禁仍未关闭。
## 2026年10月2日15:43 长任务异步整改入口
需求确认:正式申请、部署与重试不能等待整个工作流结束才响应;沿用持久化任务与权限边界,不以增加代理超时替代异步执行。仅处理 Linux,开发回归不清理线上业务数据;首次初始化验收另行清库执行。

View File

@ -1,5 +1,22 @@
# Scert 整改、合并与优化清单
## 2026年10月2日17:11 最新开发进度入口
- [x] P0-10 新增任务与尝试的原子终态保存,避免部分写入造成长期状态冲突;补齐取消到达较晚、部分成功、回滚未确认和收尾持久化失败的处理与测试。
- [x] P0-04 配套修正 Linux 动态安装模板停止预算,与分发单元一致;两种配置路径均有原生回归覆盖。
- [x] 最新隔离 Linux 全后端测试及 go vet 通过;前端沿用未再改动的 252 项通过和构建通过证据。本批仍未部署,各条目仅为完成待复审,不标整体通过。
- [~] 普通专项重复回归正在进行;第一台竞争检测因 SSH 无法获得新结果保持未确认,不能用普通测试替代。Git 两次推送连接超时,本地开发成果已保存。
- [!] 完整部署报告、其他长操作、审计与结果一致性、重启 502、历史恢复证据、系统通知重置、三数据库首次流程和备份恢复仍有待办。P0-10、P0-11 与最终生产验收不关闭。
## 2026年10月2日16:47 最新开发进度入口
- [x] P0-10/P1-04 的申请、部署、重试已完成异步提交和前端任务查询接线,浏览器断线不终止已接收任务;最近尝试、取消收尾和查询失败提示已补充。完成待复审,不表示整个条目关闭。
- [x] 本机前端 252 项全部通过、生产构建通过;第二台隔离原生 Linux 全后端测试与 go vet 通过。本批尚未部署,不能沿用旧候选实机结果作为新代码验收。
- [~] 异步专项竞争检测正在第一台隔离环境执行;同时补核动态安装模板停止预算与终态持久化边界。
- [!] 其他长操作、单实例重启 502、旧恢复事务证据、通知重置、三数据库首次流程和备份恢复仍有未完成项。P0-10、P0-11 和整体生产门禁继续保持未通过。
详细任务点见《Scert整改任务执行清单》,开发回归与实机验收边界见《Linux生产流程测试记录》。下方按时间保留历史证据,不以旧入口覆盖本节进度。
## 2026年10月2日15:31 最新进度入口
- [x] 新候选真实部署负向检查:旧文件摘要变化时停止回滚并保留完整现场;启动恢复遇到锁归属变化同样拒绝接管。仅还原受控注入后,产品自动完成双机回滚。

View File

@ -2,6 +2,7 @@ import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
import { settingsPayload } from "./SettingsPayload.js";
import { deploymentSummary, serverAuthLabel } from "./DeploymentFeedback.js";
import { useDomainForm } from "./DomainForm.js";
import { useTaskMonitor } from "./TaskMonitor.js";
const defaultDNSVerificationTimeoutSeconds = 60;
const aliyunDNSVerificationTimeoutSeconds = 6;
@ -75,6 +76,11 @@ export function useDashboard() {
const taskDetailsError = ref("");
const deploymentResult = ref(null);
const domainActionRunning = ref(false);
const taskMonitor = useTaskMonitor({
fetchAdmin, tasks, deploymentResult, showTaskDetails, taskDetails,
taskAttempts, taskDetailsLoading, taskDetailsError,
refreshResources: refreshTaskResources,
});
const hasOpenModal = computed(
() =>
showForm.value ||
@ -549,6 +555,28 @@ export function useDashboard() {
}
}
async function refreshTaskResources(signal) {
// 任务结束后只刷新运行数据,不覆盖用户正在编辑的系统设置或表单。
const responses = await Promise.all([
fetchAdmin("/api/v1/status", { signal }),
fetchAdmin("/api/v1/domains", { signal }),
fetchAdmin("/api/v1/certificates", { signal }),
fetchAdmin("/api/v1/audit-logs?limit=20", { signal }),
]);
if (responses.some((response) => !response.ok)) throw new Error("刷新任务关联数据失败");
const [statusResult, domainResult, certificateResult, auditResult] = await Promise.all(
responses.map((response) => response.json()),
);
if (signal.aborted) return;
if (![domainResult, certificateResult, auditResult].every((result) => Array.isArray(result.items))) {
throw new Error("任务关联数据响应不完整");
}
status.value = statusResult;
domains.value = domainResult.items;
certificates.value = certificateResult.items;
auditLogs.value = auditResult.items;
}
async function runDomainAction(item, action, successMessage) {
if (domainActionRunning.value) return;
domainActionRunning.value = true;
@ -559,12 +587,26 @@ export function useDashboard() {
notice.value = "";
try {
const options = { method: "POST" };
if (action === "issue") {
if (action === "issue" || action === "deploy") {
const suffix = crypto.randomUUID ? crypto.randomUUID() : createRandomSuffix();
options.headers = { "Idempotency-Key": `issue-${item.id}-${suffix}` };
options.headers = { "Idempotency-Key": `${action}-${item.id}-${suffix}` };
}
const response = await fetchAdmin(`/api/v1/domains/${item.id}/${action}`, options);
const result = await response.json().catch(() => ({}));
if (response.status === 202 && !result.task?.id) {
throw new Error("服务未返回任务编号,请在运行概览确认是否已接收,勿重复提交");
}
if (response.ok && result.task?.id && (response.status === 202 || result.accepted || result.replayed)) {
notice.value = `任务已提交:${result.task.id},可在运行概览查看执行结果`;
if (action === "deploy") {
deploymentResult.value = {
domain: item.name, running: false, pending: true,
taskID: result.task.id, taskStatus: result.task.status,
taskMessage: result.task.message || "", report: null, error: "", queryError: "",
};
}
return;
}
if (action === "deploy") {
deploymentResult.value = {
domain: item.name,
@ -601,39 +643,6 @@ export function useDashboard() {
return runDomainAction(item, "deploy", `已提交 ${item.name} 的证书部署`);
}
async function openTaskDetails(item) {
showTaskDetails.value = true;
taskDetails.value = null;
taskAttempts.value = [];
taskDetailsError.value = "";
taskDetailsLoading.value = true;
try {
const [taskResponse, attemptsResponse] = await Promise.all([
fetchAdmin(`/api/v1/tasks/${item.id}`),
fetchAdmin(`/api/v1/tasks/${item.id}/attempts`),
]);
if (!taskResponse.ok) {
throw new Error(await responseError(taskResponse, "读取任务详情失败"));
}
if (!attemptsResponse.ok) {
throw new Error(await responseError(attemptsResponse, "读取任务尝试记录失败"));
}
taskDetails.value = await taskResponse.json();
taskAttempts.value = (await attemptsResponse.json()).items || [];
} catch (err) {
taskDetailsError.value = err.message || "读取任务详情失败";
} finally {
taskDetailsLoading.value = false;
}
}
function closeTaskDetails() {
showTaskDetails.value = false;
taskDetails.value = null;
taskAttempts.value = [];
taskDetailsError.value = "";
}
async function cancelTask(item) {
if (!window.confirm("确定取消这项正在执行的证书任务吗?")) return;
error.value = "";
@ -642,8 +651,11 @@ export function useDashboard() {
const response = await fetchAdmin(`/api/v1/tasks/${item.id}`, { method: "DELETE" });
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "取消任务失败");
notice.value = "证书任务已取消";
notice.value = response.status === 202 || result.task?.status === "cancelling"
? "正在取消,等待任务停止和部署收尾;此时不能重试"
: "证书任务已取消";
await loadData();
await taskMonitor.refreshTaskStatus();
} catch (err) {
error.value = err.message || "取消任务失败";
}
@ -656,8 +668,9 @@ export function useDashboard() {
const response = await fetchAdmin(`/api/v1/tasks/${item.id}/retry`, { method: "POST" });
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "重试任务失败");
notice.value = "证书任务已重新执行";
notice.value = "重试已提交,请在任务详情查看本次执行结果";
await loadData();
await taskMonitor.refreshTaskStatus();
} catch (err) {
error.value = err.message || "重试任务失败";
}
@ -1022,6 +1035,7 @@ export function useDashboard() {
return {
...domainForm,
...taskMonitor,
status,
currentUser,
canManageSystemSettings,
@ -1112,8 +1126,6 @@ export function useDashboard() {
deployDomain,
cancelTask,
retryTask,
openTaskDetails,
closeTaskDetails,
showTaskDetails,
taskDetails,
taskAttempts,

View File

@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import "./DeploymentResult.test.js";
import "./TaskMonitor.test.js";
import "./DashboardLogout.test.js";
import "./DomainModal.test.js";
import "./DomainForm.test.js";
@ -139,6 +140,7 @@ test("完整页面逐个渲染六个板块,每次只有一个内容标题", as
app.component("ProjectManagement", { render: () => Vue.h("h2", headings.projects) });
app.component("DNSAccounts", { render: () => Vue.h("h2", headings["dns-accounts"]) });
app.component("DeploymentResult", { render: () => null });
app.component("TaskDeploymentResults", { render: () => null });
const warnings = [];
app.config.warnHandler = (message) => warnings.push(message);
const html = await renderToString(app);

View File

@ -10,6 +10,7 @@ import DashboardNavigation from "./DashboardNavigation.vue";
import { useDashboardNavigation } from "./DashboardNavigation.js";
import { useDashboard } from "./Dashboard.js";
import DeploymentResult from "./DeploymentResult.vue";
import TaskDeploymentResults from "./TaskDeploymentResults.vue";
const dashboard = useDashboard();
const {
@ -37,6 +38,8 @@ const {
saving,
error,
notice,
taskQueryError,
taskDeploymentReport,
formError,
showForm,
editingId,
@ -152,6 +155,7 @@ const { activeSection, activeLabel, sections, selectSection } = useDashboardNavi
<div class="dashboard-messages" aria-live="polite">
<p v-if="error" class="error-banner">{{ error }}</p>
<p v-if="notice" class="notice-banner">{{ notice }}</p>
<p v-if="taskQueryError" class="error-banner" role="alert">{{ taskQueryError }}</p>
</div>
<div class="dashboard-workspace">
@ -504,8 +508,8 @@ const { activeSection, activeLabel, sections, selectSection } = useDashboardNavi
<button class="close-button" type="button" aria-label="关闭" @click="closeTaskDetails">×</button>
</div>
<div v-if="taskDetailsLoading" class="empty-state">正在读取任务详情……</div>
<p v-else-if="taskDetailsError" class="form-error">{{ taskDetailsError }}</p>
<template v-else-if="taskDetails">
<p v-if="taskDetailsError" class="form-error" role="alert">{{ taskDetailsError }}</p>
<template v-if="taskDetails">
<div class="task-detail-summary">
<div><span>当前状态</span><strong class="tag" :class="statusClass(taskDetails.status)">{{ displayLabel(taskStatusLabels, taskDetails.status) }}</strong></div>
<div><span>执行阶段</span><strong>{{ taskStageLabel(taskDetails.stage) }}</strong></div>
@ -516,6 +520,7 @@ const { activeSection, activeLabel, sections, selectSection } = useDashboardNavi
<span>任务信息</span>
<p>{{ taskDetails.message || "无附加信息" }}</p>
</div>
<TaskDeploymentResults v-if="taskDeploymentReport" :report="taskDeploymentReport" />
<h3 class="modal-section-title">执行尝试</h3>
<div v-if="taskAttempts.length === 0" class="empty-state compact-empty">暂无执行尝试记录</div>
<div v-else class="attempt-list">

View File

@ -2,6 +2,14 @@ export function serverAuthLabel(server) {
return server.password_configured ? "SSH 密码已配置" : "SSH 密码未配置";
}
export function taskExecutionLabel(status) {
return {
queued: "排队中", running: "执行中", retry_wait: "等待重试",
cancelling: "正在取消,等待收尾", cancelled: "已取消",
success: "已完成", failed: "失败", partial_success: "部分成功",
}[status] || "等待确认状态";
}
export function deploymentSummary(report) {
if (!report || !Array.isArray(report.results)) return "未收到有效部署结果";
if (report.status !== "success" && report.recovery_status === "success") {

View File

@ -1,5 +1,5 @@
<script setup>
import { deploymentStatusLabel, deploymentSummary } from "./DeploymentFeedback.js";
import { deploymentStatusLabel, deploymentSummary, taskExecutionLabel } from "./DeploymentFeedback.js";
defineProps({ result: { type: Object, required: true } });
defineEmits(["close"]);
@ -27,8 +27,14 @@ defineEmits(["close"]);
@click="$emit('close')"
>×</button>
</div>
<p v-if="result.running" role="status">正在连接服务器、推送证书并校验重载,请稍候……</p>
<p v-if="result.running" role="status">正在提交操作,请稍候……</p>
<template v-else>
<template v-if="result.taskID">
<p class="muted">任务编号:{{ result.taskID }}</p>
<p role="status">{{ taskExecutionLabel(result.taskStatus) }}:{{ result.taskMessage }}</p>
<p v-if="result.pending" class="muted">任务已在后台接收,可关闭此窗口,在运行概览继续查看;请勿重复提交。</p>
<p v-if="result.queryError" class="form-error" role="alert">{{ result.queryError }}</p>
</template>
<p v-if="result.certificateImported" role="status">证书已导入并保存。</p>
<p v-if="result.error" class="form-error" role="alert">{{ result.error }}</p>
<p v-if="result.report" class="deployment-summary" role="status">
@ -49,7 +55,7 @@ defineEmits(["close"]);
</span>
</article>
</div>
<p class="muted">各服务器结果记录文件推送、配置校验与重载;站点 HTTPS 校验单独汇总,不代表逐台服务器均已验证。</p>
<p v-if="result.report" class="muted">各服务器结果仅记录本次尝试的推送结果,整体状态以任务终态为准;不能据此推断每台站点的 HTTPS 校验均通过。</p>
</template>
<div class="modal-actions">
<button class="secondary-button" type="button" :disabled="result.running" @click="$emit('close')">

View File

@ -0,0 +1,24 @@
<script setup>
import { deploymentSummary, deploymentStatusLabel } from "./DeploymentFeedback.js";
defineProps({ report: { type: Object, required: true } });
</script>
<template>
<section aria-label="最近一次尝试的部署结果">
<h3 class="modal-section-title">本次部署结果</h3>
<p role="status">{{ deploymentSummary(report) }}</p>
<div class="attempt-list">
<article v-for="(item, index) in report.results" :key="item.server_id || index" class="attempt-row">
<div>
<strong>{{ item.server || "未命名服务器" }}</strong>
<span class="muted">{{ item.message || "无附加信息" }}</span>
</div>
<span class="tag">{{ deploymentStatusLabel(item.status) }}</span>
</article>
</div>
<p class="muted">逐服务器结果不代表整体部署或站点 HTTPS 校验成功,请结合任务状态判断。</p>
</section>
</template>
<style scoped src="./Dashboard.css"></style>

View File

@ -0,0 +1,190 @@
import { onBeforeUnmount, onMounted, ref } from "vue";
const terminalStatuses = new Set(["success", "failed", "cancelled", "partial_success"]);
export function taskIsPending(task) {
return !terminalStatuses.has(task?.status);
}
export function latestDeploymentReport(task, attempts, results) {
const latest = Math.max(0, ...attempts.map((item) => item.attempt_number || 0));
const items = results.filter((item) => item.attempt_number === latest);
if (taskIsPending(task) || latest === 0 || items.length === 0) return null;
return {
status: task.status,
total: items.length,
success: items.filter((item) => item.status === "success").length,
failed: items.filter((item) => item.status !== "success").length,
results: items,
};
}
// 查询失败只表示暂时无法确认状态,不能据此重发任务或宣布执行失败。
export function useTaskMonitor({ fetchAdmin, tasks, deploymentResult, showTaskDetails,
taskDetails, taskAttempts, taskDetailsLoading, taskDetailsError, refreshResources }) {
const taskQueryError = ref("");
const taskDeploymentReport = ref(null);
let selectedID = "";
let stopped = false;
let timer;
let controller;
let inFlight;
let resourcesDirty = false;
async function readJSON(path, signal) {
const response = await fetchAdmin(path, { signal });
const result = await response.json().catch(() => null);
if (!response.ok || !result) {
const error = new Error("任务状态查询失败,将自动重试;请勿重复提交任务");
error.status = response.status;
throw error;
}
return result;
}
async function readTask(id, signal) {
const path = `/api/v1/tasks/${encodeURIComponent(id)}`;
const task = await readJSON(path, signal);
if (task.id !== id || !task.status) throw new Error("任务状态响应不完整,将自动重试");
const [attempts, results] = await Promise.all([
readJSON(`${path}/attempts`, signal),
readJSON(`${path}/deployment-results`, signal),
]);
if (!Array.isArray(attempts.items) || !Array.isArray(results.items)) {
throw new Error("任务执行记录响应不完整,将自动重试");
}
const latestAttempt = attempts.items.reduce((latest, item) =>
!latest || item.attempt_number > latest.attempt_number ? item : latest, null);
if (!taskIsPending(task) && latestAttempt && latestAttempt.status !== task.status) {
throw new Error("任务记录正在更新,将重新查询一致的执行结果");
}
return { task, attempts: attempts.items, report: latestDeploymentReport(task, attempts.items, results.items) };
}
async function poll() {
const activeController = new AbortController();
controller = activeController;
const signal = activeController.signal;
const timeout = setTimeout(() => activeController.abort(), 10000);
const detailID = selectedID;
const deploymentID = deploymentResult.value?.pending ? deploymentResult.value.taskID : "";
try {
const list = await readJSON("/api/v1/tasks?limit=20", signal);
if (!Array.isArray(list.items)) throw new Error("任务列表响应不完整,将自动重试");
if (stopped) return;
const previous = new Map(tasks.value.map((item) => [item.id, item]));
if (list.items.some((item) => previous.has(item.id) &&
taskIsPending(previous.get(item.id)) && !taskIsPending(item))) resourcesDirty = true;
tasks.value = list.items;
taskQueryError.value = "";
const ids = [...new Set([detailID, deploymentID].filter(Boolean))];
await Promise.all(ids.map(async (id) => {
try {
const snapshot = await readTask(id, signal);
if (stopped) return;
if (selectedID === id && showTaskDetails.value) {
taskDetails.value = snapshot.task;
taskAttempts.value = snapshot.attempts;
taskDeploymentReport.value = snapshot.report;
taskDetailsError.value = "";
}
if (deploymentResult.value?.taskID === id) {
if (deploymentResult.value.pending && !taskIsPending(snapshot.task)) resourcesDirty = true;
deploymentResult.value = {
...deploymentResult.value,
pending: taskIsPending(snapshot.task),
taskStatus: snapshot.task.status,
taskMessage: snapshot.task.message || "",
report: snapshot.report,
queryError: "",
};
}
} catch (error) {
if (stopped) return;
const denied = error.status === 401 || error.status === 403;
const message = denied ? "当前已无权读取此任务,请联系项目管理员"
: "暂时无法查询任务状态,将自动重试;这不代表任务失败,请勿重复提交";
if (selectedID === id) {
taskDetailsError.value = message;
if (denied) {
taskDetails.value = null;
taskAttempts.value = [];
taskDeploymentReport.value = null;
}
}
if (deploymentResult.value?.taskID === id) {
deploymentResult.value.queryError = message;
if (denied) {
deploymentResult.value.report = null;
deploymentResult.value.taskMessage = "";
deploymentResult.value.taskStatus = "";
}
}
}
}));
if (!stopped && resourcesDirty && refreshResources) {
await refreshResources(signal);
resourcesDirty = false;
}
} catch (error) {
if (!stopped) {
if (error.status === 401 || error.status === 403) {
tasks.value = [];
taskDetails.value = null;
taskAttempts.value = [];
taskDeploymentReport.value = null;
if (deploymentResult.value) {
deploymentResult.value.report = null;
deploymentResult.value.taskMessage = "";
}
}
taskQueryError.value = "任务状态查询失败,将自动重试;请勿重复提交任务";
if (selectedID === detailID && detailID) taskDetailsError.value = taskQueryError.value;
if (deploymentID && deploymentResult.value?.taskID === deploymentID) {
deploymentResult.value.queryError = taskQueryError.value;
}
}
} finally {
clearTimeout(timeout);
if (!stopped && selectedID === detailID) taskDetailsLoading.value = false;
}
}
function refreshTaskStatus() {
if (stopped) return Promise.resolve();
if (!inFlight) inFlight = poll().finally(() => { inFlight = null; });
return inFlight;
}
async function openTaskDetails(item) {
selectedID = item.id;
showTaskDetails.value = true;
taskDetails.value = null;
taskAttempts.value = [];
taskDeploymentReport.value = null;
taskDetailsError.value = "";
taskDetailsLoading.value = true;
await refreshTaskStatus();
// 用户切换详情时,旧请求不能覆盖新选择;等待旧查询结束后再读取新任务。
if (!stopped && selectedID === item.id && taskDetailsLoading.value) await refreshTaskStatus();
}
function closeTaskDetails() {
selectedID = "";
showTaskDetails.value = false;
taskDetails.value = null;
taskAttempts.value = [];
taskDeploymentReport.value = null;
taskDetailsError.value = "";
taskDetailsLoading.value = false;
}
onMounted(() => { timer = window.setInterval(refreshTaskStatus, 3000); });
onBeforeUnmount(() => {
stopped = true;
window.clearInterval(timer);
controller?.abort();
});
return { taskQueryError, taskDeploymentReport, refreshTaskStatus, openTaskDetails, closeTaskDetails };
}

View File

@ -0,0 +1,226 @@
import assert from "node:assert/strict";
import test from "node:test";
import { useDashboard } from "./Dashboard.js";
import { latestDeploymentReport } from "./TaskMonitor.js";
import { deploymentSummary } from "./DeploymentFeedback.js";
import { deferred, mountComposable } from "../../test-support/mount.js";
const initialTask = { id: "async-task", domain_id: "domain-1", task_type: "certificate_deploy", status: "running" };
const target = { server_id: "server-1", server: "部署目标", status: "success", attempt_number: 1 };
function taskFetcher(readTask, onPost = () => {}) {
return async (path, options = {}) => {
const task = readTask();
if (options.method === "POST") {
onPost(options);
return Response.json({ task, accepted: true }, { status: 202 });
}
if (path === `/api/v1/tasks/${task.id}`) return Response.json(task);
if (path.endsWith("/attempts")) return Response.json({ items: [{ attempt_number: 1, status: task.status }] });
if (path.endsWith("/deployment-results")) return Response.json({ items: task.status === "running" ? [] : [target] });
if (path.startsWith("/api/v1/tasks?")) return Response.json({ items: [task] });
return Response.json({ items: [] });
};
}
test("异步部署接收后不误报成功或缺失结果,最终失败不被逐目标成功覆盖", async (t) => {
let current = { ...initialTask };
const posts = [];
const { state } = await mountComposable(t, useDashboard, taskFetcher(() => current, (options) => posts.push(options)));
await state.deployDomain({ id: "domain-1", name: "example.test" });
assert.equal(posts.length, 1);
assert.match(posts[0].headers["Idempotency-Key"], /^deploy-domain-1-/);
assert.equal(state.deploymentResult.value.pending, true);
assert.equal(state.deploymentResult.value.running, false);
assert.equal(state.deploymentResult.value.error, "");
assert.equal(state.deploymentResult.value.report, null);
assert.match(state.notice.value, /任务已提交/);
current = { ...current, status: "failed", message: "站点校验未通过" };
await state.refreshTaskStatus();
assert.equal(state.deploymentResult.value.pending, false);
assert.equal(state.deploymentResult.value.taskStatus, "failed");
assert.equal(state.deploymentResult.value.report.results[0].status, "success");
assert.doesNotMatch(deploymentSummary(state.deploymentResult.value.report), /部署完成/);
assert.equal(posts.length, 1);
});
test("刷新页面从持久化任务恢复详情,关闭结果窗口不取消任务", async (t) => {
let current = { ...initialTask };
const { state } = await mountComposable(t, useDashboard, taskFetcher(() => current));
assert.equal(state.tasks.value[0].id, current.id);
await state.openTaskDetails(current);
assert.equal(state.taskDetails.value.status, "running");
state.deploymentResult.value = null;
current = { ...current, status: "success", message: "证书部署成功" };
await state.refreshTaskStatus();
assert.equal(state.taskDetails.value.status, "success");
assert.equal(state.taskDeploymentReport.value.results.length, 1);
state.closeTaskDetails();
assert.equal(state.taskDetails.value, null);
});
test("部署结果只汇总最新尝试,不混用上一次失败或伪造站点验证", () => {
const task = { ...initialTask, status: "success" };
const attempts = [{ attempt_number: 1 }, { attempt_number: 2 }];
const report = latestDeploymentReport(task, attempts, [
{ ...target, status: "failed" }, { ...target, attempt_number: 2 },
]);
assert.equal(report.total, 1);
assert.equal(report.failed, 0);
assert.equal(report.verification_status, undefined);
assert.equal(latestDeploymentReport(task, attempts, [target]), null);
assert.equal(latestDeploymentReport(initialTask, attempts, [target]), null);
});
test("状态查询断线保留后台任务,恢复后继续读取且不会再次提交", async (t) => {
let unavailable = false;
let posts = 0;
const fetchTask = taskFetcher(() => initialTask, () => { posts += 1; });
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (unavailable && path.startsWith("/api/v1/tasks")) return Promise.reject(new Error("连接中断"));
return fetchTask(path, options);
});
await state.deployDomain({ id: "domain-1", name: "example.test" });
unavailable = true;
await state.refreshTaskStatus();
assert.equal(state.deploymentResult.value.pending, true);
assert.equal(state.deploymentResult.value.taskStatus, "running");
assert.match(state.deploymentResult.value.queryError, /勿重复提交/);
unavailable = false;
await state.refreshTaskStatus();
assert.equal(state.deploymentResult.value.queryError, "");
assert.equal(posts, 1);
});
test("任务轮询不重叠,卸载取消在途查询并停止计时器", async (t) => {
let block = false;
let requests = 0;
let aborted = false;
const fetchTask = taskFetcher(() => initialTask);
const { state, unmount, runIntervals } = await mountComposable(t, useDashboard, (path, options) => {
if (block && path.startsWith("/api/v1/tasks?")) {
requests += 1;
return new Promise((resolve, reject) => {
options.signal.addEventListener("abort", () => { aborted = true; reject(new Error("已停止查询")); });
});
}
return fetchTask(path, options);
});
block = true;
const pending = state.refreshTaskStatus();
runIntervals();
runIntervals();
assert.equal(requests, 1);
unmount();
await pending;
runIntervals();
assert.equal(aborted, true);
assert.equal(requests, 1);
assert.equal(state.taskQueryError.value, "");
});
test("切换详情时迟到请求不能覆盖新任务", async (t) => {
const delayed = deferred();
let blocked = true;
const fetchTask = taskFetcher(() => initialTask);
const { state } = await mountComposable(t, useDashboard, async (path, options) => {
if (path === "/api/v1/tasks/first" && blocked) return delayed.promise;
if (path === "/api/v1/tasks/second") return Response.json({ ...initialTask, id: "second" });
return fetchTask(path, options);
});
const first = state.openTaskDetails({ id: "first" });
await new Promise((resolve) => setImmediate(resolve));
const second = state.openTaskDetails({ id: "second" });
blocked = false;
delayed.resolve(Response.json({ ...initialTask, id: "first" }));
await Promise.all([first, second]);
assert.equal(state.taskDetails.value.id, "second");
assert.equal(state.taskDetailsLoading.value, false);
});
test("异步取消与重试只提示请求已接收,不提前宣布终态", async (t) => {
const fetchTask = taskFetcher(() => initialTask);
const { state } = await mountComposable(t, useDashboard, (path, options = {}) => {
if (options.method === "DELETE") return Response.json({ task: { ...initialTask, status: "cancelling" } }, { status: 202 });
return fetchTask(path, options);
});
window.confirm = () => true;
await state.cancelTask(initialTask);
assert.match(state.notice.value, /等待任务停止和部署收尾/);
assert.doesNotMatch(state.notice.value, /任务已取消/);
await state.retryTask({ ...initialTask, status: "failed" });
assert.match(state.notice.value, /重试已提交/);
assert.doesNotMatch(state.notice.value, /已重新执行/);
});
test("代理返回非 JSON 时保留原状态,不泄露 HTML 或误报完成", async (t) => {
let invalid = false;
const fetchTask = taskFetcher(() => initialTask);
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (invalid && path.startsWith("/api/v1/tasks/")) return Promise.resolve(new Response("<html>504</html>", { status: 504 }));
return fetchTask(path, options);
});
await state.openTaskDetails(initialTask);
invalid = true;
await state.refreshTaskStatus();
assert.equal(state.taskDetails.value.status, "running");
assert.match(state.taskDetailsError.value, /不代表任务失败/);
assert.doesNotMatch(state.taskDetailsError.value, /<html>|Unexpected/);
});
test("任务与尝试读取遇到并发重试时不混用旧终态和新结果", async (t) => {
let retrying = false;
const current = { ...initialTask, status: "success" };
const fetchTask = taskFetcher(() => current);
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (retrying && path.endsWith("/attempts")) {
return Promise.resolve(Response.json({ items: [{ attempt_number: 2, status: "running" }] }));
}
return fetchTask(path, options);
});
await state.openTaskDetails(current);
retrying = true;
await state.refreshTaskStatus();
assert.match(state.taskDetailsError.value, /暂时无法查询/);
assert.equal(state.taskAttempts.value[0].attempt_number, 1);
});
test("项目撤权后清除缓存的任务详情和部署结果", async (t) => {
let denied = false;
const current = { ...initialTask, status: "success" };
const fetchTask = taskFetcher(() => current);
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (denied && path.startsWith("/api/v1/tasks/")) {
return Promise.resolve(Response.json({ error: "无权访问此任务" }, { status: 403 }));
}
return fetchTask(path, options);
});
await state.openTaskDetails(current);
assert.equal(state.taskDeploymentReport.value.results.length, 1);
denied = true;
await state.refreshTaskStatus();
assert.equal(state.taskDetails.value, null);
assert.equal(state.taskDeploymentReport.value, null);
assert.deepEqual(state.taskAttempts.value, []);
assert.match(state.taskDetailsError.value, /无权读取/);
});
test("任务完成后自动刷新域名证书数据,但不覆盖正在编辑的设置", async (t) => {
let current = { ...initialTask };
let resourceReads = 0;
const fetchTask = taskFetcher(() => current);
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (path === "/api/v1/domains") {
resourceReads += 1;
return Promise.resolve(Response.json({ items: [{ id: "domain-1", status: current.status === "success" ? "active" : "pending" }] }));
}
return fetchTask(path, options);
});
state.settingsForm.value.notification.sender_name = "正在编辑的名称";
assert.equal(state.domains.value[0].status, "pending");
current = { ...current, status: "success" };
await state.refreshTaskStatus();
assert.equal(state.domains.value[0].status, "active");
assert.equal(state.settingsForm.value.notification.sender_name, "正在编辑的名称");
assert.equal(resourceReads, 2);
});