完善部署中断恢复提示和安全阶段诊断并记录实机边界
This commit is contained in:
1 parent
b27eedc1d0
commit
6efc89061b
17 files changed
+386
-14
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTaskRecoveryRequiresResourceInspectionBeforeRetry(t *testing.T) {
|
||||
repo, db := newTaskRepositoryTest(t)
|
||||
defer db.Close()
|
||||
ctx := t.Context()
|
||||
if err := repo.CreateTask(ctx, Task{ID: "interrupted-deploy", DomainID: "domain-1", TaskType: "certificate_deploy", Status: "running", Stage: "certificate_deploy"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.CreateAttempt(ctx, TaskAttempt{ID: "interrupted-attempt", TaskID: "interrupted-deploy", AttemptNumber: 1, Stage: "certificate_deploy", Status: "running"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count, err := repo.RecoverInterruptedTasks(ctx, time.Now()); err != nil || count != 1 {
|
||||
t.Fatalf("恢复任务失败:%d %v", count, err)
|
||||
}
|
||||
task, err := repo.GetTask(ctx, "interrupted-deploy")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, expected := range []string{"执行结果未确认", "远端文件", "部署锁", "服务状态", "确认安全后再重试"} {
|
||||
if !strings.Contains(task.Message, expected) {
|
||||
t.Errorf("中断任务缺少恢复边界提示 %q:%s", expected, task.Message)
|
||||
}
|
||||
}
|
||||
attempts, err := repo.ListAttempts(ctx, task.ID)
|
||||
if err != nil || len(attempts) != 1 || attempts[0].Message != task.Message {
|
||||
t.Fatalf("执行尝试与任务恢复说明不一致:%+v %v", attempts, err)
|
||||
}
|
||||
var auditMessage string
|
||||
if err := db.QueryRowContext(ctx, `SELECT message FROM audit_logs WHERE action = 'task_recovery' AND task_id = ?`, task.ID).Scan(&auditMessage); err != nil || auditMessage != task.Message {
|
||||
t.Fatalf("恢复审计说明不一致:%s %v", auditMessage, err)
|
||||
}
|
||||
}
|
||||
@@ -346,7 +346,7 @@ func (r *SQLTaskRepository) RecoverInterruptedTasks(ctx context.Context, finishe
|
||||
rollback := func() {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
recoveryMessage := "\u670d\u52a1\u91cd\u542f\u5bfc\u81f4\u4efb\u52a1\u4e2d\u65ad\uff0c\u8bf7\u91cd\u65b0\u6267\u884c"
|
||||
recoveryMessage := "服务重启导致任务中断,执行结果未确认;请先核查关联资源,证书部署需检查远端文件、部署锁及服务状态,确认安全后再重试"
|
||||
auditResult, err := tx.ExecContext(ctx, r.query(`INSERT INTO audit_logs
|
||||
(action, resource_type, resource_id, status, message, created_at, scope_type, project_id, task_id, operator_id)
|
||||
SELECT 'task_recovery', 'task', id, 'failed', ?, ?, scope_type, project_id, id, operator_id
|
||||
|
||||
Reference in new issue
Block a user