接入证书任务取消上下文
为证书导入、恢复、校验和当前部署工作流绑定任务取消回调,补充取消接口与回归测试,并同步四份整改清单进度。
This commit is contained in:
1 parent
b8997512f9
commit
4cf9fb6076
11 files changed
+162
-14
No files matched your search
@@ -70,6 +70,12 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
if err := w.tasks.CreateTask(ctx, task); err != nil {
|
||||
return result, err
|
||||
}
|
||||
taskCtx, taskCancel := context.WithCancel(ctx)
|
||||
if hooks, ok := repository.TaskCancellationHooksFromContext(ctx); ok {
|
||||
hooks.Register(task.ID, taskCancel)
|
||||
defer hooks.Unregister(task.ID)
|
||||
}
|
||||
defer taskCancel()
|
||||
defer func() {
|
||||
finishCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
@@ -90,7 +96,7 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
w.logError(resultErr)
|
||||
}
|
||||
}()
|
||||
if err := w.audit.CreateAuditLog(ctx, repository.AuditLog{
|
||||
if err := w.audit.CreateAuditLog(taskCtx, repository.AuditLog{
|
||||
TaskID: task.ID,
|
||||
Action: taskType, ResourceType: "domain", ResourceID: id,
|
||||
Status: "running", Message: "开始执行证书工作流",
|
||||
@@ -98,10 +104,10 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
return result, err
|
||||
}
|
||||
for number := 1; number <= attempts; number++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
if err := taskCtx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if err := w.tasks.UpdateTask(ctx, task.ID, "running", stage, "正在执行证书工作流", &started, nil); err != nil {
|
||||
if err := w.tasks.UpdateTask(taskCtx, task.ID, "running", stage, "正在执行证书工作流", &started, nil); err != nil {
|
||||
return result, err
|
||||
}
|
||||
attemptStarted := time.Now().UTC()
|
||||
@@ -110,10 +116,10 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
Stage: stage, Status: "running", Message: "正在执行证书工作流",
|
||||
StartedAt: &attemptStarted, CreatedAt: attemptStarted,
|
||||
}
|
||||
if err := w.tasks.CreateAttempt(ctx, attempt); err != nil {
|
||||
if err := w.tasks.CreateAttempt(taskCtx, attempt); err != nil {
|
||||
return result, err
|
||||
}
|
||||
issueCtx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
issueCtx, cancel := context.WithTimeout(taskCtx, 30*time.Minute)
|
||||
result, resultErr = execute(issueCtx, id)
|
||||
cancel()
|
||||
finishCtx, finishCancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
@@ -127,10 +133,10 @@ func (w *certificateTaskWorkflow) run(ctx context.Context, id, taskType, stage s
|
||||
if resultErr == nil || number == attempts || result.Certificate.Saved.CertificatePath != "" || errors.Is(resultErr, context.Canceled) || errors.Is(resultErr, context.DeadlineExceeded) {
|
||||
return result, resultErr
|
||||
}
|
||||
if err := w.tasks.UpdateTask(ctx, task.ID, "retry_wait", "retry_wait", "证书申请失败,等待重试", &started, nil); err != nil {
|
||||
if err := w.tasks.UpdateTask(taskCtx, task.ID, "retry_wait", "retry_wait", "证书申请失败,等待重试", &started, nil); err != nil {
|
||||
return result, errors.Join(resultErr, err)
|
||||
}
|
||||
if err := waitForManagementRetry(ctx, interval); err != nil {
|
||||
if err := waitForManagementRetry(taskCtx, interval); err != nil {
|
||||
return result, errors.Join(resultErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,6 +259,38 @@ func TestCertificateTerminalPersistenceFailureIsReturnedWithoutReissue(t *testin
|
||||
}
|
||||
}
|
||||
|
||||
func TestCertificateTasksBindCancellationHook(t *testing.T) {
|
||||
w, a := newCertificateTaskFixture(t)
|
||||
registerCh := make(chan context.CancelFunc, 1)
|
||||
unregisterCh := make(chan string, 1)
|
||||
ctx := repository.WithTaskCancellationHooks(t.Context(), repository.TaskCancellationHooks{
|
||||
Register: func(_ string, cancel context.CancelFunc) { registerCh <- cancel },
|
||||
Unregister: func(taskID string) { unregisterCh <- taskID },
|
||||
})
|
||||
started := make(chan struct{})
|
||||
finished := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := w.run(ctx, "task-domain", "certificate_validate", "certificate_validate", 1, 0, func(ctx context.Context, _ string) (CertificateWorkflowResult, error) {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
return CertificateWorkflowResult{}, ctx.Err()
|
||||
})
|
||||
finished <- err
|
||||
}()
|
||||
<-started
|
||||
cancel := <-registerCh
|
||||
cancel()
|
||||
if err := <-finished; !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("task cancellation error = %v", err)
|
||||
}
|
||||
select {
|
||||
case <-unregisterCh:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("task cancellation hook was not unregistered")
|
||||
}
|
||||
assertCertificateTask(t, a, "cancelled", "cancelled")
|
||||
}
|
||||
|
||||
func TestCertificateTasksRecordPartialDeploymentFailure(t *testing.T) {
|
||||
w, a := newCertificateTaskFixture(t)
|
||||
_, err := w.run(t.Context(), "task-domain", "renewal_run", "certificate_issue", 1, 0, func(context.Context, string) (CertificateWorkflowResult, error) {
|
||||
|
||||
Reference in new issue
Block a user