完成证书导入后台任务及上传回收,更新回归进度
This commit is contained in:
1 parent
09616e732d
commit
fce5a84dd4
17 files changed
+473
-15
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -166,7 +167,7 @@ func (s *Server) importCertificateFiles(c *gin.Context) {
|
||||
if !s.authorizeVisibleDomainPermissions(c, domain.PermissionCertificateManage, domain.PermissionDeploymentExecute) {
|
||||
return
|
||||
}
|
||||
if s.importCertificate == nil {
|
||||
if s.importCertificate == nil && s.importCertificateTask == nil {
|
||||
s.writeError(c, http.StatusNotImplemented, "证书导入接口尚未配置", errors.New("证书导入流程未配置"))
|
||||
return
|
||||
}
|
||||
@@ -176,6 +177,16 @@ func (s *Server) importCertificateFiles(c *gin.Context) {
|
||||
s.writeError(c, http.StatusBadRequest, "证书上传参数无效", err)
|
||||
return
|
||||
}
|
||||
if s.importCertificateTask != nil {
|
||||
// 仅传递独立参数和文件所有权,后台不得引用请求上下文。
|
||||
s.submitPreparedDomainTask(c, "certificate_import", &preparedCertificateTask{
|
||||
operation: func(ctx context.Context, _ string) (certificate.IssueResult, deployment.Report, error) {
|
||||
return s.importCertificateTask(ctx, request)
|
||||
},
|
||||
cleanup: cleanup,
|
||||
})
|
||||
return
|
||||
}
|
||||
defer cleanup()
|
||||
result, report, err := s.importCertificate(s.taskWorkflowContext(c), request)
|
||||
report = sanitizeDeploymentReport(report)
|
||||
@@ -206,6 +217,12 @@ func parseCertificateImportRequest(c *gin.Context) (certificate.ImportRequest, f
|
||||
cleanup := func() {}
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
if strings.HasPrefix(strings.ToLower(contentType), "multipart/form-data") {
|
||||
// 表单解析产生的暂存文件不属于异步工作线程;独立复制后立即回收。
|
||||
defer func() {
|
||||
if c.Request.MultipartForm != nil {
|
||||
_ = c.Request.MultipartForm.RemoveAll()
|
||||
}
|
||||
}()
|
||||
if err := c.Request.ParseMultipartForm(certificateUploadLimit); err != nil {
|
||||
return certificate.ImportRequest{}, cleanup, fmt.Errorf("解析上传表单失败:%w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
func importUploadRequest(t *testing.T, key string) *http.Request {
|
||||
t.Helper()
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
for _, name := range []string{"certificate", "private_key"} {
|
||||
part, err := writer.CreateFormFile(name, name+".pem")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write([]byte(name)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/certificate/import", body)
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
request.Header.Set("Idempotency-Key", key)
|
||||
return request
|
||||
}
|
||||
|
||||
func TestAsyncImportUploadLifecycle(t *testing.T) {
|
||||
for _, scenario := range []string{"success", "failure", "saved_failure", "partial", "panic", "cancel", "shutdown", "stopped", "full", "invalid_key", "replay"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
temporary := t.TempDir()
|
||||
for _, name := range []string{"TMPDIR", "TMP", "TEMP"} {
|
||||
t.Setenv(name, temporary)
|
||||
}
|
||||
var calls atomic.Int32
|
||||
server, tasks, _, cleanup := newTaskAPITestServer(t, nil)
|
||||
defer cleanup()
|
||||
server.importCertificateTask = func(_ context.Context, request certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error) {
|
||||
calls.Add(1)
|
||||
for path, expected := range map[string]string{request.Certificate: "certificate", request.PrivateKey: "private_key"} {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil || string(data) != expected {
|
||||
t.Errorf("上传文件被提前回收或内容变化:%v", err)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || info.Mode().Perm() != 0600 {
|
||||
t.Errorf("上传文件权限错误:%v", err)
|
||||
}
|
||||
}
|
||||
if scenario == "panic" {
|
||||
panic("private-import-error")
|
||||
}
|
||||
if scenario == "failure" {
|
||||
return certificate.IssueResult{}, deployment.Report{}, errors.New("private-import-error")
|
||||
}
|
||||
result := certificate.IssueResult{DomainID: request.DomainID, Saved: certificate.SavedResource{
|
||||
CertificatePath: "/private/cert.pem", FullChainPath: "/private/fullchain.pem", PrivateKeyPath: "/private/key.pem",
|
||||
}}
|
||||
if scenario == "saved_failure" || scenario == "partial" {
|
||||
status := "failed"
|
||||
if scenario == "partial" {
|
||||
status = "partial"
|
||||
}
|
||||
return result, deployment.Report{Status: status}, errors.New("private-deploy-error")
|
||||
}
|
||||
return result, deployment.Report{Status: "success"}, nil
|
||||
}
|
||||
server.asyncTasks = make(map[string]bool)
|
||||
server.taskSlots = make(chan struct{}, 1)
|
||||
server.taskSlots <- struct{}{}
|
||||
key := "import-lifecycle"
|
||||
switch scenario {
|
||||
case "stopped":
|
||||
server.stopTaskWorkers()
|
||||
case "full":
|
||||
for index := range 32 {
|
||||
server.asyncTasks[string(rune('a'+index))] = true
|
||||
}
|
||||
case "invalid_key":
|
||||
key = strings.Repeat("x", 300)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
request := importUploadRequest(t, key)
|
||||
requestCtx, cancelRequest := context.WithCancel(request.Context())
|
||||
serveAdminHTTP(server, response, request.WithContext(requestCtx))
|
||||
cancelRequest()
|
||||
files, err := filepath.Glob(filepath.Join(temporary, "scert-upload-*"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if scenario == "stopped" || scenario == "full" || scenario == "invalid_key" {
|
||||
want := http.StatusServiceUnavailable
|
||||
if scenario == "invalid_key" {
|
||||
want = http.StatusBadRequest
|
||||
}
|
||||
if response.Code != want || len(files) != 0 || calls.Load() != 0 {
|
||||
t.Fatalf("拒绝提交仍保留上传文件或执行导入:%d %v %d", response.Code, files, calls.Load())
|
||||
}
|
||||
return
|
||||
}
|
||||
task := submittedTask(t, response)
|
||||
if task.TaskType != "certificate_import" || len(files) != 2 || calls.Load() != 0 {
|
||||
t.Fatalf("排队状态或上传文件所有权错误:%+v %v %d", task, files, calls.Load())
|
||||
}
|
||||
if scenario == "replay" {
|
||||
replay := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, replay, importUploadRequest(t, key))
|
||||
remaining, _ := filepath.Glob(filepath.Join(temporary, "scert-upload-*"))
|
||||
if replay.Code != http.StatusConflict || len(remaining) != 2 {
|
||||
t.Fatalf("重复提交未回收本次上传文件:%d %v", replay.Code, remaining)
|
||||
}
|
||||
}
|
||||
switch scenario {
|
||||
case "cancel":
|
||||
cancelled := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, cancelled, httptest.NewRequest(http.MethodDelete, "/api/v1/tasks/"+task.ID, nil))
|
||||
if cancelled.Code != http.StatusAccepted {
|
||||
t.Fatalf("取消排队导入失败:%d %s", cancelled.Code, cancelled.Body.String())
|
||||
}
|
||||
case "shutdown":
|
||||
server.stopTaskWorkers()
|
||||
default:
|
||||
<-server.taskSlots
|
||||
}
|
||||
waitTaskWorkersForTest(t, server)
|
||||
wantStatus := "success"
|
||||
wantCalls := int32(1)
|
||||
switch scenario {
|
||||
case "failure", "saved_failure", "panic":
|
||||
wantStatus = "failed"
|
||||
case "partial":
|
||||
wantStatus = "partial_success"
|
||||
case "cancel", "shutdown":
|
||||
wantStatus, wantCalls = "cancelled", 0
|
||||
}
|
||||
stored, err := tasks.GetTask(t.Context(), task.ID)
|
||||
if err != nil || stored.Status != wantStatus || calls.Load() != wantCalls {
|
||||
t.Fatalf("导入终态错误:%+v %v,执行次数 %d", stored, err, calls.Load())
|
||||
}
|
||||
for _, path := range files {
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Errorf("上传文件未回收:%v", err)
|
||||
}
|
||||
}
|
||||
if scenario == "saved_failure" || scenario == "partial" || scenario == "success" || scenario == "failure" {
|
||||
reportResponse := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, reportResponse, httptest.NewRequest(http.MethodGet, "/api/v1/tasks/"+task.ID+"/deployment-results", nil))
|
||||
var payload struct {
|
||||
Reports []struct {
|
||||
Report deployment.Report `json:"report"`
|
||||
} `json:"reports"`
|
||||
}
|
||||
if err := json.Unmarshal(reportResponse.Body.Bytes(), &payload); err != nil || len(payload.Reports) != 1 {
|
||||
t.Fatalf("完整导入结果未持久化:%s %v", reportResponse.Body.String(), err)
|
||||
}
|
||||
if payload.Reports[0].Report.CertificateImported != (scenario != "failure") {
|
||||
t.Fatalf("导入是否保存的状态丢失:%+v", payload)
|
||||
}
|
||||
if strings.Contains(reportResponse.Body.String(), "private-") || strings.Contains(reportResponse.Body.String(), "/private/") || strings.Contains(stored.Message, "private-") {
|
||||
t.Fatal("公开结果泄露内部错误或证书路径")
|
||||
}
|
||||
if scenario == "saved_failure" || scenario == "partial" {
|
||||
if !strings.Contains(stored.Message, "无需重新导入") {
|
||||
t.Fatalf("已保存证书的失败结果不明确:%s", stored.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
if scenario == "failure" {
|
||||
retry := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, retry, httptest.NewRequest(http.MethodPost, "/api/v1/tasks/"+task.ID+"/retry", nil))
|
||||
if retry.Code != http.StatusConflict || calls.Load() != 1 {
|
||||
t.Fatal("不得重放已回收上传文件的导入任务")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAsyncImportRejectedSubmissionCleansFiles(t *testing.T) {
|
||||
server, _, _, cleanup := newTaskAPITestServer(t, nil)
|
||||
defer cleanup()
|
||||
var cleaned atomic.Int32
|
||||
server.tasks = nil
|
||||
response := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(response)
|
||||
ctx.Request = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
server.submitPreparedDomainTask(ctx, "certificate_import", &preparedCertificateTask{cleanup: func() { cleaned.Add(1) }})
|
||||
if response.Code != http.StatusNotImplemented || cleaned.Load() != 1 {
|
||||
t.Fatal("缺失任务仓储时必须回收上传文件")
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,9 @@ type Dependencies struct {
|
||||
IssueSetupCertificate func(context.Context, certificate.SetupCertificateRequest) (certificate.SavedResource, error)
|
||||
RollbackSetupCertificate func(context.Context, certificate.SavedResource) error
|
||||
ImportCertificate func(context.Context, certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error)
|
||||
DeployCertificate func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
// ImportCertificateTask 只执行导入流程,复用 HTTP 层已经创建的任务。
|
||||
ImportCertificateTask func(context.Context, certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error)
|
||||
DeployCertificate func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
// DeployCertificateTask 执行已有任务,不得重复创建任务或尝试记录。
|
||||
DeployCertificateTask func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
RestoreCertificateVersion func(context.Context, string, string) (certificate.IssueResult, error)
|
||||
@@ -99,6 +101,7 @@ type Server struct {
|
||||
issueSetupCertificate func(context.Context, certificate.SetupCertificateRequest) (certificate.SavedResource, error)
|
||||
rollbackSetupCertificate func(context.Context, certificate.SavedResource) error
|
||||
importCertificate func(context.Context, certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error)
|
||||
importCertificateTask func(context.Context, certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error)
|
||||
deployCertificate func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
deployCertificateTask func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
restoreVersion func(context.Context, string, string) (certificate.IssueResult, error)
|
||||
@@ -167,6 +170,7 @@ func New(deps Dependencies) *Server {
|
||||
issueSetupCertificate: deps.IssueSetupCertificate,
|
||||
rollbackSetupCertificate: deps.RollbackSetupCertificate,
|
||||
importCertificate: deps.ImportCertificate,
|
||||
importCertificateTask: deps.ImportCertificateTask,
|
||||
deployCertificate: deps.DeployCertificate,
|
||||
deployCertificateTask: deps.DeployCertificateTask,
|
||||
restoreVersion: deps.RestoreCertificateVersion,
|
||||
|
||||
@@ -23,6 +23,21 @@ func (s *Server) issueDomainAsTask(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) submitDomainTask(c *gin.Context, taskType string) {
|
||||
s.submitPreparedDomainTask(c, taskType, nil)
|
||||
}
|
||||
|
||||
type preparedCertificateTask struct {
|
||||
operation func(context.Context, string) (certificate.IssueResult, deployment.Report, error)
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
func (s *Server) submitPreparedDomainTask(c *gin.Context, taskType string, prepared *preparedCertificateTask) {
|
||||
transferred := false
|
||||
defer func() {
|
||||
if !transferred && prepared != nil && prepared.cleanup != nil {
|
||||
prepared.cleanup()
|
||||
}
|
||||
}()
|
||||
if (taskType == "certificate_issue" && s.issueCertificate == nil) || (taskType == "certificate_deploy" && s.deployCertificateTask == nil) {
|
||||
s.writeError(c, http.StatusNotImplemented, "证书申请接口尚未配置", errors.New("证书申请工作流未配置"))
|
||||
return
|
||||
@@ -50,7 +65,7 @@ func (s *Server) submitDomainTask(c *gin.Context, taskType string) {
|
||||
s.writeError(c, http.StatusServiceUnavailable, "证书任务权限服务尚未就绪", errors.New("项目授权服务未配置"))
|
||||
return
|
||||
}
|
||||
if taskType == "certificate_issue" {
|
||||
if taskType == "certificate_issue" || taskType == "certificate_import" {
|
||||
if err := authorizer.AuthorizeProject(c.Request.Context(), c.GetString(authenticatedActorKey), resource.ResourceScope.Normalize().ProjectID, domain.PermissionCertificateManage); err != nil {
|
||||
if errors.Is(err, repository.ErrProjectAccessDenied) {
|
||||
s.writeError(c, http.StatusForbidden, "无权管理此项目的证书任务", nil)
|
||||
@@ -149,6 +164,14 @@ func (s *Server) submitDomainTask(c *gin.Context, taskType string) {
|
||||
s.writeError(c, http.StatusInternalServerError, "创建任务尝试失败", err)
|
||||
return
|
||||
}
|
||||
if prepared != nil {
|
||||
transferred = s.enqueueCertificateTask(c, task, attempt, taskType, prepared.operation,
|
||||
taskType, "正在导入证书并部署", "证书导入和部署完成", prepared.cleanup)
|
||||
if transferred {
|
||||
writeAcceptedCertificateTask(c, task)
|
||||
}
|
||||
return
|
||||
}
|
||||
if taskType == "certificate_deploy" {
|
||||
s.executeCertificateDeployTask(c, task, attempt, taskType)
|
||||
return
|
||||
@@ -157,11 +180,15 @@ func (s *Server) submitDomainTask(c *gin.Context, taskType string) {
|
||||
}
|
||||
|
||||
func (s *Server) executeCertificateIssueTask(c *gin.Context, task repository.Task, attempt repository.TaskAttempt, auditAction string) {
|
||||
s.enqueueCertificateTask(c, task, attempt, auditAction, s.issueCertificate, "certificate_issue", "正在申请证书并部署", "证书申请和部署完成")
|
||||
if s.enqueueCertificateTask(c, task, attempt, auditAction, s.issueCertificate, "certificate_issue", "正在申请证书并部署", "证书申请和部署完成", nil) {
|
||||
writeAcceptedCertificateTask(c, task)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) executeCertificateDeployTask(c *gin.Context, task repository.Task, attempt repository.TaskAttempt, auditAction string) {
|
||||
s.enqueueCertificateTask(c, task, attempt, auditAction, s.deployCertificateTask, "certificate_deploy", "正在部署证书", "证书部署完成")
|
||||
if s.enqueueCertificateTask(c, task, attempt, auditAction, s.deployCertificateTask, "certificate_deploy", "正在部署证书", "证书部署完成", nil) {
|
||||
writeAcceptedCertificateTask(c, task)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, attempt repository.TaskAttempt, auditAction string, operation func(context.Context, string) (certificate.IssueResult, deployment.Report, error), stage, runningMessage, successMessage string) {
|
||||
@@ -214,7 +241,11 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
|
||||
if s.finishCancelledTask(task, attempt, executionCtx) {
|
||||
return
|
||||
}
|
||||
_, deploymentReport, issueErr := operation(executionCtx, task.DomainID)
|
||||
certificateResult, deploymentReport, issueErr := operation(executionCtx, task.DomainID)
|
||||
if task.TaskType == "certificate_import" {
|
||||
deploymentReport.CertificateImported = issueErr == nil || (certificateResult.DomainID == task.DomainID &&
|
||||
certificateResult.Saved.CertificatePath != "" && certificateResult.Saved.FullChainPath != "" && certificateResult.Saved.PrivateKeyPath != "")
|
||||
}
|
||||
finishCtx, finishCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer finishCancel()
|
||||
deploymentReport = sanitizeDeploymentReport(deploymentReport)
|
||||
@@ -238,6 +269,12 @@ func (s *Server) runCertificateTask(ctx context.Context, task repository.Task, a
|
||||
} else if partial {
|
||||
status = "partial_success"
|
||||
}
|
||||
if task.TaskType == "certificate_import" && issueErr != nil {
|
||||
message = "证书导入失败"
|
||||
if deploymentReport.CertificateImported {
|
||||
message = "证书已导入,但部署未完成。请检查部署结果后重试部署,无需重新导入。"
|
||||
}
|
||||
}
|
||||
s.taskMu.Lock()
|
||||
defer s.taskMu.Unlock()
|
||||
if persisted, err := s.tasks.GetTask(finishCtx, task.ID); err != nil {
|
||||
|
||||
@@ -26,6 +26,8 @@ func (s *Server) authorizeCertificateTaskExecution(ctx context.Context, task rep
|
||||
case "certificate_issue":
|
||||
permissions = append(permissions, domain.PermissionCertificateManage, domain.PermissionDNSCredentialUse)
|
||||
case "certificate_deploy":
|
||||
case "certificate_import":
|
||||
permissions = append(permissions, domain.PermissionCertificateManage)
|
||||
default:
|
||||
return repository.ErrProjectAccessDenied
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
func TestAsyncExecutionRechecksQueuedAuthorization(t *testing.T) {
|
||||
for _, taskType := range []string{"certificate_issue", "certificate_deploy"} {
|
||||
for _, taskType := range []string{"certificate_issue", "certificate_deploy", "certificate_import"} {
|
||||
for _, scenario := range []string{
|
||||
"unchanged", "disabled_user", "removed_member", "read_only_member",
|
||||
"moved_domain", "deleted_domain", "deployment_denied", "certificate_denied",
|
||||
@@ -32,6 +32,9 @@ func TestAsyncExecutionRechecksQueuedAuthorization(t *testing.T) {
|
||||
server, tasks, audit, cleanup := newTaskAPITestServer(t, operation)
|
||||
defer cleanup()
|
||||
server.deployCertificateTask = operation
|
||||
server.importCertificateTask = func(ctx context.Context, request certificate.ImportRequest) (certificate.IssueResult, deployment.Report, error) {
|
||||
return operation(ctx, request.DomainID)
|
||||
}
|
||||
server.asyncTasks = make(map[string]bool)
|
||||
server.taskSlots = make(chan struct{}, 1)
|
||||
server.taskSlots <- struct{}{}
|
||||
@@ -39,8 +42,12 @@ func TestAsyncExecutionRechecksQueuedAuthorization(t *testing.T) {
|
||||
if taskType == "certificate_deploy" {
|
||||
path = "/api/v1/domains/domain-1/deploy"
|
||||
}
|
||||
if taskType == "certificate_import" {
|
||||
path = "/api/v1/domains/domain-1/certificate/import"
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, path, nil))
|
||||
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, path,
|
||||
strings.NewReader(`{"certificate_path":"/managed/cert.pem","private_key_path":"/managed/key.pem"}`)))
|
||||
task := submittedTask(t, response)
|
||||
if calls.Load() != 0 {
|
||||
t.Fatal("占满执行槽后任务仍执行")
|
||||
@@ -95,7 +102,7 @@ func TestAsyncExecutionRechecksQueuedAuthorization(t *testing.T) {
|
||||
<-server.taskSlots
|
||||
waitTaskWorkersForTest(t, server)
|
||||
want := "failed"
|
||||
if scenario == "unchanged" || taskType == "certificate_deploy" && (scenario == "dns_denied" || scenario == "certificate_denied") {
|
||||
if scenario == "unchanged" || taskType == "certificate_deploy" && (scenario == "dns_denied" || scenario == "certificate_denied") || taskType == "certificate_import" && scenario == "dns_denied" {
|
||||
want = "success"
|
||||
}
|
||||
stored, err := tasks.GetTask(t.Context(), task.ID)
|
||||
|
||||
@@ -15,13 +15,13 @@ import (
|
||||
|
||||
// 持久化任务与尝试后才调用此入口;后台执行不得持有请求或响应对象。
|
||||
// 进程中断由启动恢复标记失败,不自动重放可能已经签发或部署的外部操作。
|
||||
func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, attempt repository.TaskAttempt, action string, operation func(context.Context, string) (certificate.IssueResult, deployment.Report, error), stage, runningMessage, successMessage string) {
|
||||
func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, attempt repository.TaskAttempt, action string, operation func(context.Context, string) (certificate.IssueResult, deployment.Report, error), stage, runningMessage, successMessage string, cleanup func()) bool {
|
||||
s.taskMu.Lock()
|
||||
if s.taskWorkersStopped || len(s.asyncTasks) >= 32 {
|
||||
s.taskMu.Unlock()
|
||||
s.finishTaskBeforeExecution(task, attempt, "failed", "服务正在停止或任务队列已满,任务未执行")
|
||||
s.writeError(c, http.StatusServiceUnavailable, "服务正在停止或任务队列已满,请稍后重试", nil)
|
||||
return
|
||||
return false
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), certificateTaskTimeout)
|
||||
if s.taskCancels == nil {
|
||||
@@ -37,6 +37,9 @@ func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, at
|
||||
s.taskMu.Unlock()
|
||||
go func() {
|
||||
defer s.taskWorkers.Done()
|
||||
if cleanup != nil {
|
||||
defer cleanup()
|
||||
}
|
||||
defer s.unregisterTaskCancel(task.ID)
|
||||
defer cancel()
|
||||
defer func() {
|
||||
@@ -53,6 +56,11 @@ func (s *Server) enqueueCertificateTask(c *gin.Context, task repository.Task, at
|
||||
}
|
||||
s.runCertificateTask(ctx, task, attempt, action, operation, stage, runningMessage, successMessage)
|
||||
}()
|
||||
return true
|
||||
}
|
||||
|
||||
// 上传文件所有权完成转交后再写响应;客户端断开或响应异常不得回收工作线程的文件。
|
||||
func writeAcceptedCertificateTask(c *gin.Context, task repository.Task) {
|
||||
c.Header("Location", "/api/v1/tasks/"+task.ID)
|
||||
c.JSON(http.StatusAccepted, gin.H{"task": task, "accepted": true})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user