feat(runtime): add bulk purge for archived queue tasks and chat_attachment i18n
Operators can clear all finally-failed tasks in one queue from the runtime drawer, with audit logging and scoped API access. Also localize the chat_attachment queue label in the runtime queue details table.
This commit is contained in:
@@ -2066,6 +2066,43 @@ func (h *SystemHandler) MutateRuntimeTask(c *gin.Context) {
|
||||
h.mutateRuntimeTask(c, types.RuntimeTaskAction(c.Param("action")))
|
||||
}
|
||||
|
||||
// PurgeArchivedRuntimeTasks clears every archived (finally-failed) task in one
|
||||
// queue. It only touches the archived dead-letter set — live pending/active/
|
||||
// scheduled/retry tasks are untouched — and mirrors single-record delete
|
||||
// semantics by leaving business state as-is (archived tasks already had their
|
||||
// document status flipped to "failed" on their last retry).
|
||||
// @Summary Purge all archived tasks in a queue
|
||||
// @Tags System Admin
|
||||
// @Produce json
|
||||
// @Param queue path string true "Queue name"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Router /system/admin/runtime/queues/{queue}/archived [delete]
|
||||
func (h *SystemHandler) PurgeArchivedRuntimeTasks(c *gin.Context) {
|
||||
queue := c.Param("queue")
|
||||
if !isKnownRuntimeQueue(queue) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid queue"})
|
||||
return
|
||||
}
|
||||
inspector, supported := h.taskInspector.(interfaces.RuntimeTaskInspector)
|
||||
if !supported {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Task queue is unavailable"})
|
||||
return
|
||||
}
|
||||
deleted, available, err := inspector.PurgeArchivedRuntimeTasks(c.Request.Context(), queue)
|
||||
if !available {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "Task queue is unavailable"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
logger.Errorf(c.Request.Context(), "purge archived queue tasks queue=%s: %v", queue, err)
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Archived tasks could not be purged"})
|
||||
return
|
||||
}
|
||||
h.emitQueueTaskAudit(c.Request.Context(), types.AuditActionSystemQueueArchivedPurged, queue, "",
|
||||
map[string]string{"deleted": strconv.Itoa(deleted)})
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "deleted": deleted})
|
||||
}
|
||||
|
||||
func (h *SystemHandler) ListSystemSettings(c *gin.Context) {
|
||||
ctx := logger.CloneContext(c.Request.Context())
|
||||
rows, err := h.systemSettingSvc.List(ctx)
|
||||
|
||||
@@ -83,6 +83,9 @@ type runtimeTaskTestInspector struct {
|
||||
retriedTask string
|
||||
deletedTask string
|
||||
forceDeleted string
|
||||
purgedQueue string
|
||||
purgedCount int
|
||||
purgeErr error
|
||||
cancelKnowledge string
|
||||
cancelDeleted int
|
||||
mutatedQueue string
|
||||
@@ -193,6 +196,16 @@ func (r *runtimeTaskTestInspector) ForceDeleteRuntimeTask(
|
||||
return true, r.forceDeleteErr
|
||||
}
|
||||
|
||||
func (r *runtimeTaskTestInspector) PurgeArchivedRuntimeTasks(
|
||||
_ context.Context, queue string,
|
||||
) (int, bool, error) {
|
||||
r.purgedQueue = queue
|
||||
if r.purgeErr != nil {
|
||||
return 0, true, r.purgeErr
|
||||
}
|
||||
return r.purgedCount, true, nil
|
||||
}
|
||||
|
||||
func TestGetRuntimeQueuesReportsIsolatedPoolCapacity(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
handler := &SystemHandler{
|
||||
@@ -399,6 +412,46 @@ func TestRuntimeTaskMutationsDelegateToInspector(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurgeArchivedRuntimeTasksDelegatesToInspector(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
inspector := &runtimeTaskTestInspector{purgedCount: 7}
|
||||
handler := &SystemHandler{taskInspector: inspector}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Params = gin.Params{{Key: "queue", Value: types.QueueChatAttachment}}
|
||||
ctx.Request = httptest.NewRequest(http.MethodDelete, "/archived", nil)
|
||||
|
||||
handler.PurgeArchivedRuntimeTasks(ctx)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if inspector.purgedQueue != types.QueueChatAttachment {
|
||||
t.Fatalf("purged queue = %q, want %q", inspector.purgedQueue, types.QueueChatAttachment)
|
||||
}
|
||||
var response map[string]any
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if response["deleted"] != float64(7) {
|
||||
t.Fatalf("deleted = %v, want 7", response["deleted"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurgeArchivedRuntimeTasksRejectsUnknownQueue(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
handler := &SystemHandler{taskInspector: &runtimeTaskTestInspector{}}
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Params = gin.Params{{Key: "queue", Value: "unknown"}}
|
||||
ctx.Request = httptest.NewRequest(http.MethodDelete, "/archived", nil)
|
||||
|
||||
handler.PurgeArchivedRuntimeTasks(ctx)
|
||||
if recorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRuntimeTasksRejectsUnknownQueue(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
handler := &SystemHandler{taskInspector: &runtimeTaskTestInspector{}}
|
||||
|
||||
Reference in New Issue
Block a user