补齐任务部署结果持久化与查询接口
保存批量部署每台服务器的执行结果,新增数据库迁移、仓储查询和任务部署结果接口,并补充应用与仓储测试。
This commit is contained in:
1 parent
253f2bd824
commit
126cb6d566
11 files changed
+330
-1
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
@@ -148,6 +149,9 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
issueCtx, cancel := context.WithTimeout(taskCtx, 30*time.Minute)
|
||||
result, resultErr = execute(issueCtx, id)
|
||||
cancel()
|
||||
if recordErr := w.tasks.RecordTaskDeploymentResults(taskCtx, task.ID, attempt.ID, number, taskDeploymentResults(result.Deployment)); recordErr != nil {
|
||||
resultErr = errors.Join(resultErr, recordErr)
|
||||
}
|
||||
finishCtx, finishCancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
finished := time.Now().UTC()
|
||||
status, message := certificateTaskOutcome(result, resultErr)
|
||||
@@ -169,6 +173,24 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
return result, resultErr
|
||||
}
|
||||
|
||||
func taskDeploymentResults(report deployment.Report) []repository.TaskDeploymentResult {
|
||||
if len(report.Results) == 0 {
|
||||
return nil
|
||||
}
|
||||
items := make([]repository.TaskDeploymentResult, 0, len(report.Results))
|
||||
for index, item := range report.Results {
|
||||
items = append(items, repository.TaskDeploymentResult{
|
||||
ResultIndex: index,
|
||||
ServerID: item.ServerID,
|
||||
Server: item.Server,
|
||||
Status: item.Status,
|
||||
Message: item.Message,
|
||||
BackupID: item.BackupID,
|
||||
})
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func certificateTaskOutcome(result CertificateWorkflowResult, err error) (string, string) {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return "cancelled", "\u8bc1\u4e66\u5de5\u4f5c\u6d41\u5df2\u53d6\u6d88"
|
||||
|
||||
@@ -421,3 +421,34 @@ func TestCertificateTasksRecordPartialDeploymentFailure(t *testing.T) {
|
||||
}
|
||||
assertCertificateTask(t, a, "partial_success", "partial_success")
|
||||
}
|
||||
|
||||
func TestCertificateTaskPersistsDeploymentResults(t *testing.T) {
|
||||
w, a := newCertificateTaskFixture(t)
|
||||
w.workflow = &CertificateWorkflow{
|
||||
Certificates: &workflowCertificateService{result: certificate.IssueResult{DomainID: "task-domain"}},
|
||||
Domains: &workflowDomainState{},
|
||||
Servers: workflowServerLister{servers: []deployment.Server{{ID: "server-1"}, {ID: "server-2"}}},
|
||||
CurrentCertificate: func(context.Context, string) (certificate.IssueResult, error) {
|
||||
return certificate.IssueResult{DomainID: "task-domain", Saved: certificate.SavedResource{CertificatePath: "cert.pem"}}, nil
|
||||
},
|
||||
Deployment: &workflowDeployer{report: deployment.Report{
|
||||
Total: 2, Success: 1, Failed: 1, Status: "partial",
|
||||
Results: []deployment.Result{
|
||||
{ServerID: "server-1", Server: "第一台服务器", Status: "success", Message: "部署成功"},
|
||||
{ServerID: "server-2", Server: "第二台服务器", Status: "failed", Message: "部署失败"},
|
||||
},
|
||||
}},
|
||||
}
|
||||
_, err := w.DeployCurrent(t.Context(), "task-domain")
|
||||
if err == nil {
|
||||
t.Fatal("部分部署失败应返回错误")
|
||||
}
|
||||
items, err := a.Tasks.ListTasks(t.Context(), "task-domain", 10)
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("任务读取失败:%+v %v", items, err)
|
||||
}
|
||||
results, err := a.Tasks.ListTaskDeploymentResults(t.Context(), items[0].ID)
|
||||
if err != nil || len(results) != 2 || results[0].AttemptID == "" || results[1].Status != "failed" {
|
||||
t.Fatalf("部署结果未持久化:%+v %v", results, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user