fix article generation task status logging

This commit is contained in:
2026-05-05 19:47:04 +08:00
parent 82375cff46
commit 81577b6154
14 changed files with 163 additions and 35 deletions
@@ -472,11 +472,44 @@ func (w *ArticleGenerationWorker) requeueExpiredRunningGenerationTask(ctx contex
}()
var (
tenantID int64
articleID int64
taskType string
tenantID int64
articleID int64
taskType string
shouldRequeue bool
)
if err := tx.QueryRow(ctx, `
WITH existing AS (
SELECT gt.id,
gt.tenant_id,
gt.article_id,
gt.task_type,
a.generate_status
FROM generation_tasks gt
JOIN articles a ON a.id = gt.article_id
AND a.tenant_id = gt.tenant_id
WHERE gt.id = $1
AND gt.status = 'running'
AND (
gt.lease_expires_at IS NULL
OR gt.lease_expires_at < NOW()
)
FOR UPDATE OF gt
),
finalized_failed_article AS (
UPDATE generation_tasks gt
SET status = 'failed',
error_message = COALESCE(NULLIF(gt.error_message, ''), '文章已标记生成失败,停止后台恢复重试 [article_failed_stop_retry]'),
completed_at = COALESCE(gt.completed_at, NOW()),
lease_token = NULL,
lease_owner = NULL,
lease_expires_at = NULL,
updated_at = NOW()
FROM existing e
WHERE gt.id = e.id
AND e.generate_status = 'failed'
RETURNING e.tenant_id, e.article_id, e.task_type, false AS should_requeue
),
requeued AS (
UPDATE generation_tasks
SET status = 'queued',
error_message = NULL,
@@ -484,14 +517,17 @@ func (w *ArticleGenerationWorker) requeueExpiredRunningGenerationTask(ctx contex
lease_owner = NULL,
lease_expires_at = NULL,
updated_at = NOW()
WHERE id = $1
AND status = 'running'
AND (
lease_expires_at IS NULL
OR lease_expires_at < NOW()
)
RETURNING tenant_id, article_id, task_type
`, item.ID).Scan(&tenantID, &articleID, &taskType); err != nil {
FROM existing e
WHERE generation_tasks.id = e.id
AND e.generate_status <> 'failed'
RETURNING e.tenant_id, e.article_id, e.task_type, true AS should_requeue
)
SELECT tenant_id, article_id, task_type, should_requeue
FROM finalized_failed_article
UNION ALL
SELECT tenant_id, article_id, task_type, should_requeue
FROM requeued
`, item.ID).Scan(&tenantID, &articleID, &taskType, &shouldRequeue); err != nil {
if err == pgx.ErrNoRows {
return false, nil
}
@@ -501,6 +537,15 @@ func (w *ArticleGenerationWorker) requeueExpiredRunningGenerationTask(ctx contex
if err := tx.Commit(ctx); err != nil {
return false, err
}
if !shouldRequeue {
if w.logger != nil {
w.logger.Info("article generation expired task finalized because article is already failed",
zap.Int64("task_id", item.ID),
zap.Int64("article_id", articleID),
)
}
return true, nil
}
if err := w.publishGenerationTask(ctx, item.ID, taskType, tenantID, articleID); err != nil {
return false, err
}
@@ -245,13 +245,23 @@ func articleGenerationProcessTimeout(cfg config.GenerationConfig) time.Duration
func (w *ArticleGenerationWorker) handleTaskFailure(ctx context.Context, task tenantapp.ClaimedGenerationTask, stage string, taskErr error) {
task.TaskType = strings.TrimSpace(task.TaskType)
var err error
switch {
case task.TaskType == kolGenerationTaskType && w.kolWorker != nil:
w.kolWorker.HandleTaskFailure(ctx, task, stage, taskErr)
case task.TaskType == "imitation" && w.imitationService != nil:
w.imitationService.HandleTaskFailure(ctx, task, stage, taskErr)
default:
_ = tenantapp.HandleClaimedGenerationTaskFailure(ctx, w.pool, w.templateService, w.promptRuleService, task, taskErr)
err = tenantapp.HandleClaimedGenerationTaskFailure(ctx, w.pool, w.templateService, w.promptRuleService, task, taskErr)
}
if err != nil && w.logger != nil {
w.logger.Warn("article generation task failure handling failed",
zap.Error(err),
zap.Int64("task_id", task.ID),
zap.Int64("article_id", task.ArticleID),
zap.String("task_type", task.TaskType),
zap.String("stage", stage),
)
}
}