feat(system): expand runtime queue task inspection and safe actions

Replace failed-only task views with state-aware runtime task listing,
operator-safe cancel/run-now/delete actions, orphan detection, and
multimodal queue cleanup for deleted knowledge rows.
This commit is contained in:
wizardchen
2026-07-14 15:17:45 +08:00
committed by lyingbug
parent 1eae475a1f
commit 4ae48d6acf
22 changed files with 2337 additions and 481 deletions
+4 -2
View File
@@ -125,8 +125,10 @@ const (
// Runtime queue mutations are privileged SystemAdmin actions. Retrying an
// archived task can repeat its original side effects; deleting one removes
// the Redis failure record. Both must leave a platform audit trail.
AuditActionSystemQueueTaskRetried AuditAction = "system.queue_task_retried"
AuditActionSystemQueueTaskDeleted AuditAction = "system.queue_task_deleted"
AuditActionSystemQueueTaskRetried AuditAction = "system.queue_task_retried"
AuditActionSystemQueueTaskDeleted AuditAction = "system.queue_task_deleted"
AuditActionSystemQueueTaskRunNow AuditAction = "system.queue_task_run_now"
AuditActionSystemQueueTaskCancelled AuditAction = "system.queue_task_cancelled"
)
// AuditOutcome distinguishes successful mutations from middleware-level
+8
View File
@@ -40,6 +40,8 @@ func TestAuditAction_DotNamespaceConvention(t *testing.T) {
AuditActionSystemUserPasswordReset,
AuditActionSystemQueueTaskRetried,
AuditActionSystemQueueTaskDeleted,
AuditActionSystemQueueTaskRunNow,
AuditActionSystemQueueTaskCancelled,
}
for _, a := range all {
s := string(a)
@@ -124,6 +126,8 @@ func TestAuditAction_NoCollisionsAcrossNamespaces(t *testing.T) {
register("AuditActionSystemUserPasswordReset", AuditActionSystemUserPasswordReset)
register("AuditActionSystemQueueTaskRetried", AuditActionSystemQueueTaskRetried)
register("AuditActionSystemQueueTaskDeleted", AuditActionSystemQueueTaskDeleted)
register("AuditActionSystemQueueTaskRunNow", AuditActionSystemQueueTaskRunNow)
register("AuditActionSystemQueueTaskCancelled", AuditActionSystemQueueTaskCancelled)
}
// TestAuditAction_SystemNamespacePrefix pins the system.* actions
@@ -140,6 +144,8 @@ func TestAuditAction_SystemNamespacePrefix(t *testing.T) {
AuditActionSystemUserPasswordReset,
AuditActionSystemQueueTaskRetried,
AuditActionSystemQueueTaskDeleted,
AuditActionSystemQueueTaskRunNow,
AuditActionSystemQueueTaskCancelled,
}
for _, a := range cases {
assert.True(t,
@@ -164,6 +170,8 @@ func TestAuditAction_SystemWireValues(t *testing.T) {
{AuditActionSystemUserPasswordReset, "system.user_password_reset"},
{AuditActionSystemQueueTaskRetried, "system.queue_task_retried"},
{AuditActionSystemQueueTaskDeleted, "system.queue_task_deleted"},
{AuditActionSystemQueueTaskRunNow, "system.queue_task_run_now"},
{AuditActionSystemQueueTaskCancelled, "system.queue_task_cancelled"},
}
for _, c := range cases {
assert.Equal(t, c.wire, string(c.constant))
-21
View File
@@ -1,21 +0,0 @@
package types
import "time"
// FailedTaskInfo is the operator-facing projection of an asynq task that
// exhausted its automatic retry budget. Payloads are intentionally not
// exposed by the SystemAdmin API: they may contain document content or
// connector credentials. Only stable routing identifiers are copied out.
type FailedTaskInfo struct {
ID string `json:"id"`
Queue string `json:"queue"`
Type string `json:"type"`
LastError string `json:"last_error"`
LastFailedAt time.Time `json:"last_failed_at"`
Retried int `json:"retried"`
MaxRetry int `json:"max_retry"`
TenantID uint64 `json:"tenant_id,omitempty"`
KnowledgeBaseID string `json:"knowledge_base_id,omitempty"`
KnowledgeID string `json:"knowledge_id,omitempty"`
TaskID string `json:"task_id,omitempty"`
}
+15 -9
View File
@@ -64,16 +64,22 @@ type TaskInspector interface {
WorkerServerStats(ctx context.Context) (stats []types.WorkerServerStat, supported bool, err error)
}
// FailedTaskInspector is the optional operator surface implemented by queue
// backends that retain tasks after their automatic retry budget is exhausted.
// It is separate from TaskInspector so Lite-mode and test implementations that
// only support cancellation/metrics do not need to pretend failed tasks exist.
type FailedTaskInspector interface {
ListFailedTasks(
// RuntimeTaskInspector is the optional operator surface implemented by queue
// backends that retain inspectable task state. It is separate from
// TaskInspector so Lite mode and light-weight tests do not need to implement
// queue mutations.
type RuntimeTaskInspector interface {
ListRuntimeTasks(
ctx context.Context,
queue string,
state types.RuntimeTaskState,
page, pageSize int,
) (tasks []types.FailedTaskInfo, supported bool, err error)
RetryFailedTask(ctx context.Context, queue, taskID string) (supported bool, err error)
DeleteFailedTask(ctx context.Context, queue, taskID string) (supported bool, err error)
) (tasks []types.RuntimeTaskInfo, supported bool, err error)
GetRuntimeTask(ctx context.Context, queue, taskID string) (task *types.RuntimeTaskInfo, supported bool, err error)
RunRuntimeTask(ctx context.Context, queue, taskID string) (supported bool, err error)
DeleteRuntimeTask(ctx context.Context, queue, taskID string) (supported bool, err error)
// ForceDeleteRuntimeTask removes a queue record without checking the
// operator-facing AllowedActions. Used when the business row is already
// gone but a retry/pending task survived (orphan cleanup).
ForceDeleteRuntimeTask(ctx context.Context, queue, taskID string) (supported bool, err error)
}
+79
View File
@@ -0,0 +1,79 @@
package types
import "time"
// RuntimeTaskState is the stable operator-facing task lifecycle. It mirrors
// the durable states exposed by asynq while keeping the HTTP API independent
// from the queue library's Go enum.
type RuntimeTaskState string
const (
RuntimeTaskPending RuntimeTaskState = "pending"
RuntimeTaskActive RuntimeTaskState = "active"
RuntimeTaskScheduled RuntimeTaskState = "scheduled"
RuntimeTaskRetry RuntimeTaskState = "retry"
RuntimeTaskArchived RuntimeTaskState = "archived"
RuntimeTaskCompleted RuntimeTaskState = "completed"
)
func (s RuntimeTaskState) Valid() bool {
switch s {
case RuntimeTaskPending, RuntimeTaskActive, RuntimeTaskScheduled,
RuntimeTaskRetry, RuntimeTaskArchived, RuntimeTaskCompleted:
return true
default:
return false
}
}
// RuntimeTaskAction is returned by the backend for every task. The frontend
// renders only these actions instead of inferring safety from the state.
type RuntimeTaskAction string
const (
RuntimeTaskActionCancel RuntimeTaskAction = "cancel"
RuntimeTaskActionRunNow RuntimeTaskAction = "run_now"
RuntimeTaskActionDelete RuntimeTaskAction = "delete"
)
// RuntimeTaskInfo is the safe SystemAdmin projection of one queue task.
// Raw payloads and results are deliberately excluded because they may contain
// document content, signed object URLs, or connector credentials.
type RuntimeTaskInfo struct {
ID string `json:"id"`
Queue string `json:"queue"`
Type string `json:"type"`
State RuntimeTaskState `json:"state"`
AllowedActions []RuntimeTaskAction `json:"allowed_actions"`
LastError string `json:"last_error,omitempty"`
LastFailedAt *time.Time `json:"last_failed_at,omitempty"`
NextProcessAt *time.Time `json:"next_process_at,omitempty"`
StartedAt *time.Time `json:"started_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Deadline *time.Time `json:"deadline,omitempty"`
EnqueuedAt *time.Time `json:"enqueued_at,omitempty"`
Retried int `json:"retried"`
MaxRetry int `json:"max_retry"`
IsOrphaned bool `json:"is_orphaned,omitempty"`
Worker string `json:"worker,omitempty"`
TenantID uint64 `json:"tenant_id,omitempty"`
KnowledgeBaseID string `json:"knowledge_base_id,omitempty"`
KnowledgeID string `json:"knowledge_id,omitempty"`
TaskID string `json:"task_id,omitempty"`
SourceID string `json:"source_id,omitempty"`
TargetID string `json:"target_id,omitempty"`
SourceKBID string `json:"source_kb_id,omitempty"`
TargetKBID string `json:"target_kb_id,omitempty"`
DataSourceID string `json:"data_source_id,omitempty"`
SyncLogID string `json:"sync_log_id,omitempty"`
KnowledgeCount int `json:"knowledge_count,omitempty"`
}
func (t RuntimeTaskInfo) Allows(action RuntimeTaskAction) bool {
for _, allowed := range t.AllowedActions {
if allowed == action {
return true
}
}
return false
}