feat(monitor): 死信中文提示与成功/取消自动退信

This commit is contained in:
姚章浩
2026-09-15 14:37:46 +08:00
parent f8cef26f4a
commit 767684bb1f
11 changed files with 147 additions and 16 deletions
@@ -158,6 +158,14 @@ WHERE intent.source_connection_id=$1 AND filename_is_filtered(intent.source_conn
AND job.payload->>'node_id'=intent.node_id AND job.state IN ('pending','retry','failed')`, connectionID, now); err != nil {
return fmt.Errorf("cancel filename-filtered fetch jobs: %w", err)
}
// A cancelled or later-succeeded job is no longer waiting on a human:
// retire its dead-letter projection so the DLQ lane shows only real work.
if _, err := tx.Exec(ctx, `UPDATE job_failures f SET state='requeued',resolved_at=$2,resolution_actor_ref='system:filename-filter',resolution_reason='任务已被格式白名单取消'
FROM jobs job
WHERE job.id=f.job_id AND f.state='open' AND job.type='artifact.fetch'
AND job.payload->>'source_connection_id'=$1 AND job.state='cancelled'`, connectionID, now); err != nil {
return fmt.Errorf("retire filename-filtered dead letters: %w", err)
}
if _, err := tx.Exec(ctx, `UPDATE catalog_import_intents intent SET fetch_state='filtered',last_error='',updated_at=$2
WHERE intent.source_connection_id=$1 AND filename_is_filtered(intent.source_connection_id,intent.relative_path,intent.name)
AND intent.fetch_state<>'ready'`, connectionID, now); err != nil {
+12 -2
View File
@@ -15,10 +15,20 @@ const listJobFailuresSQL = `
SELECT jf.id, jf.job_id, jf.job_type, jf.publication_id, jf.error_code,
jf.attempt, jf.max_attempts_at_failure, jf.business_change_set_id,
jf.business_action, jf.business_target_kb_id, jf.state, jf.created_at,
COALESCE(siv.source_item_id,'')
COALESCE(siv.source_item_id,''),
COALESCE(NULLIF(item.title,''), NULLIF(ci.name,''), ''),
COALESCE(NULLIF(jf.last_error,''), j.last_error, '')
FROM job_failures jf
LEFT JOIN test_publications p ON p.id = jf.publication_id
LEFT JOIN source_item_versions siv ON siv.id = p.source_item_version_id
LEFT JOIN source_items item ON item.id = siv.source_item_id
LEFT JOIN jobs j ON j.id = jf.job_id
LEFT JOIN LATERAL (
SELECT intent.name FROM catalog_import_intents intent
WHERE intent.source_connection_id = j.payload->>'source_connection_id'
AND intent.node_id = j.payload->>'node_id'
LIMIT 1
) ci ON TRUE
WHERE ($1 = '' OR jf.state = $1)
ORDER BY jf.created_at DESC, jf.id DESC
LIMIT $2 OFFSET $3
@@ -60,7 +70,7 @@ func (s *Store) ListJobFailures(ctx context.Context, filter governance.JobFailur
var item governance.JobFailure
if err := rows.Scan(&item.ID, &item.JobID, &item.JobType, &item.PublicationID, &item.ErrorCode,
&item.Attempt, &item.MaxAttempts, &item.ChangeSetID, &item.Action, &item.TargetKBID,
&item.State, &item.CreatedAt, &item.SourceItemID); err != nil {
&item.State, &item.CreatedAt, &item.SourceItemID, &item.DocName, &item.LastError); err != nil {
return governance.JobFailurePage{}, fmt.Errorf("scan job failure: %w", err)
}
items = append(items, item)
+10
View File
@@ -64,6 +64,16 @@ WHERE id = $1
AND state = 'running'
`
// retireOpenFailuresSQL keeps the dead-letter lane honest: once the job
// itself succeeded, its terminal-attempt projection is no longer waiting on
// a human (a later automatic retry recovered it).
const retireOpenFailuresSQL = `
UPDATE job_failures
SET state = 'requeued', resolved_at = $2, resolution_actor_ref = 'system:queue',
resolution_reason = '任务后续重试成功'
WHERE job_id = $1 AND state = 'open'
`
const heartbeatJobSQL = `
UPDATE jobs
SET lease_until = $4,
+12 -1
View File
@@ -87,13 +87,24 @@ func (s *Store) List(ctx context.Context, limit int) ([]jobs.Job, error) {
}
func (s *Store) Complete(ctx context.Context, id, owner string, now time.Time) error {
tag, err := s.db.Exec(ctx, completeJobSQL, id, owner, now)
tx, err := s.db.Begin(ctx)
if err != nil {
return fmt.Errorf("complete job: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
tag, err := tx.Exec(ctx, completeJobSQL, id, owner, now)
if err != nil {
return fmt.Errorf("complete job: %w", err)
}
if tag.RowsAffected() != 1 {
return ErrLeaseLost
}
if _, err := tx.Exec(ctx, retireOpenFailuresSQL, id, now); err != nil {
return fmt.Errorf("complete job: retire failures: %w", err)
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("complete job: %w", err)
}
return nil
}