feat(monitor): 取消失败发布并修正危险按钮对比度

- Parse-monitor: the batch-retry button rendered red-on-red because
  weknoraMonitor.css overrode the global .danger style with text color
  only; make .danger a filled red button with proper hover/disabled states.
- Add operator cancel: 批量取消所选 / per-task 不再处理 buttons move
  failed/stalled publications to a new 'cancelled' state in one audited
  batch operation (POST /api/v1/test-publications/cancel-batch, idempotent,
  migration 000041 widens the CHECK constraints). Queued upload jobs are
  cancelled, open dead letters abandoned; source records and spool files
  stay intact so re-marking in the catalog can retry later.
- Queue status filter and labels learn 已取消; OpenAPI contract updated.
This commit is contained in:
姚章浩
2026-09-15 17:58:40 +08:00
parent 822896ea6c
commit 5037c05e93
13 changed files with 476 additions and 14 deletions
+49 -1
View File
@@ -94,6 +94,7 @@ type Store interface {
RetryPublicationWithOperation(ctx context.Context, publicationID string, now time.Time, operationID string) (bool, error)
ListJobFailures(ctx context.Context, filter governance.JobFailureFilter) (governance.JobFailurePage, error)
RetryPublicationsBatchWithOperation(ctx context.Context, request governance.RetryPublicationsBatchRequest, operationID string) (governance.RetryPublicationsBatchResult, bool, error)
CancelPublicationsBatchWithOperation(ctx context.Context, request governance.CancelPublicationsBatchRequest, operationID string) (governance.RetryPublicationsBatchResult, bool, error)
}
type Config struct {
@@ -385,6 +386,9 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("POST /api/v1/test-publications/retry-batch", func(w http.ResponseWriter, r *http.Request) {
retryPublicationsBatch(w, r, config)
})
mux.HandleFunc("POST /api/v1/test-publications/cancel-batch", func(w http.ResponseWriter, r *http.Request) {
cancelPublicationsBatch(w, r, config)
})
mux.HandleFunc("GET /api/v1/job-failures", func(w http.ResponseWriter, r *http.Request) {
listJobFailures(w, r, config.Store)
})
@@ -1914,6 +1918,50 @@ func retryPublicationsBatch(w http.ResponseWriter, r *http.Request, config Confi
writeJSON(w, http.StatusOK, map[string]any{"data": result, "operation": operation})
}
func cancelPublicationsBatch(w http.ResponseWriter, r *http.Request, config Config) {
operationID, ok := requireOperationID(w, r)
if !ok {
return
}
var input startTestPublicationsInput
if err := decodeStrictJSON(r, &input); err != nil || len(input.PublicationIDs) == 0 || len(input.PublicationIDs) > 100 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid cancel batch"})
return
}
reason := strings.TrimSpace(input.Reason)
if reason == "" || len(reason) > 500 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "reason is required"})
return
}
seen := make(map[string]bool, len(input.PublicationIDs))
for _, publicationID := range input.PublicationIDs {
if !validCatalogID(publicationID) || seen[publicationID] {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid publication_ids"})
return
}
seen[publicationID] = true
}
result, replayed, err := config.Store.CancelPublicationsBatchWithOperation(r.Context(), governance.CancelPublicationsBatchRequest{
PublicationIDs: input.PublicationIDs,
OperatorRef: actorFor(r),
Reason: reason,
Now: config.Now().UTC(),
}, operationID)
if errors.Is(err, postgresstore.ErrOperationRequestConflict) {
writeJSON(w, http.StatusConflict, map[string]string{"error": "Idempotency-Key conflicts with a different request"})
return
}
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not cancel publications"})
return
}
operation := "created"
if replayed {
operation = "replayed"
}
writeJSON(w, http.StatusOK, map[string]any{"data": result, "operation": operation})
}
func retryPublication(w http.ResponseWriter, r *http.Request, config Config) {
operationID, ok := requireOperationID(w, r)
if !ok {
@@ -1977,7 +2025,7 @@ func decodeStrictJSON(r *http.Request, destination any) error {
func validProcessingState(state string) bool {
switch state {
case "planned", "pending", "processing", "finalizing", "completed", "failed", "stalled":
case "planned", "pending", "processing", "finalizing", "completed", "failed", "stalled", "cancelled":
return true
default:
return false
+54
View File
@@ -161,6 +161,49 @@ func TestHandlerListsJobFailuresAndRunsServerSideBatchRetry(t *testing.T) {
}
}
func TestHandlerRunsServerSideBatchCancel(t *testing.T) {
store := &fakeJobStore{
cancelBatchResult: governance.RetryPublicationsBatchResult{
BatchKey: "op-cancel-1", Total: 2, Started: 1, NotRetryable: 1,
Outcomes: []governance.RetryOutcome{{PublicationID: "tp-1", Result: "cancelled"}, {PublicationID: "tp-x", Result: "not_retryable", Detail: "publication not found"}},
},
}
handler, err := NewHandler(Config{Store: store, Now: func() time.Time { return time.Date(2026, time.September, 3, 9, 0, 0, 0, time.UTC) }})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/cancel-batch",
strings.NewReader(`{"publication_ids":["tp-1","tp-x"],"reason":"源头为空文档,不再重试"}`))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Idempotency-Key", "op-cancel-1")
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("cancel-batch status = %d body %s", recorder.Code, recorder.Body.String())
}
if store.cancelBatchOperation != "op-cancel-1" || store.cancelBatchRequest == nil ||
len(store.cancelBatchRequest.PublicationIDs) != 2 ||
store.cancelBatchRequest.Reason != "源头为空文档,不再重试" || store.cancelBatchRequest.Now.IsZero() {
t.Fatalf("cancel request = %+v op=%q", store.cancelBatchRequest, store.cancelBatchOperation)
}
// Missing Idempotency-Key and missing reason are rejected before touching the store.
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/cancel-batch",
strings.NewReader(`{"publication_ids":["tp-1"],"reason":"r"}`)))
if recorder.Code != http.StatusBadRequest {
t.Fatalf("missing key status = %d", recorder.Code)
}
recorder = httptest.NewRecorder()
noReason := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/cancel-batch",
strings.NewReader(`{"publication_ids":["tp-1"],"reason":""}`))
noReason.Header.Set("Idempotency-Key", "op-cancel-2")
handler.ServeHTTP(recorder, noReason)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("missing reason status = %d", recorder.Code)
}
}
func TestHandlerListsJobsWithJSONPayload(t *testing.T) {
store := &fakeJobStore{listResult: []jobs.Job{{
ID: "job-6",
@@ -1438,6 +1481,11 @@ type fakeJobStore struct {
retryBatchRequest *governance.RetryPublicationsBatchRequest
retryBatchOperation string
retryBatchResult governance.RetryPublicationsBatchResult
cancelBatchRequest *governance.CancelPublicationsBatchRequest
cancelBatchOperation string
cancelBatchResult governance.RetryPublicationsBatchResult
cancelBatchReplayed bool
cancelBatchErr error
retryBatchReplayed bool
retryBatchErr error
enqueued jobs.EnqueueRequest
@@ -1837,6 +1885,12 @@ func (s *fakeJobStore) RetryPublicationsBatchWithOperation(_ context.Context, re
return s.retryBatchResult, s.retryBatchReplayed, s.retryBatchErr
}
func (s *fakeJobStore) CancelPublicationsBatchWithOperation(_ context.Context, request governance.CancelPublicationsBatchRequest, operationID string) (governance.RetryPublicationsBatchResult, bool, error) {
s.cancelBatchRequest = &request
s.cancelBatchOperation = operationID
return s.cancelBatchResult, s.cancelBatchReplayed, s.cancelBatchErr
}
func (s *fakeJobStore) ListScopeRules(_ context.Context, connectionID string) ([]governance.ScopeRule, error) {
s.scopeRuleConnectionID = connectionID
return s.scopeRules, s.scopeRuleErr