refactor(task): 重构异步任务处理流程

This commit is contained in:
2026-05-27 09:36:25 +08:00
parent a28fcbaee9
commit e487b4bb5e
9 changed files with 305 additions and 231 deletions

View File

@@ -20,7 +20,7 @@ func (d *taskDao) ClaimPendingGlobal(ctx context.Context, batchSize int) (tasks
}
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
sql := fmt.Sprintf(
`SELECT id, tenant_id, creator, model_name, task_id, biz_name, callback_url, model_key, input_ref, request_payload, phase, tmp_file
`SELECT id, tenant_id, creator, model_name, task_id, biz_name, callback_url, model_key, retry_count, input_ref, request_payload, phase, tmp_file
FROM %s
WHERE deleted_at IS NULL AND state = 0
ORDER BY enqueue_at ASC
@@ -63,7 +63,7 @@ func (d *taskDao) ClaimPendingByTaskIDGlobal(ctx context.Context, taskID string)
}
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
sql := fmt.Sprintf(
`SELECT id, tenant_id, creator, model_name, task_id, biz_name, callback_url, model_key, input_ref, request_payload, phase, tmp_file
`SELECT id, tenant_id, creator, model_name, task_id, biz_name, callback_url, model_key, retry_count, input_ref, request_payload, phase, tmp_file
FROM %s
WHERE deleted_at IS NULL AND state = 0 AND task_id = ?
LIMIT 1
@@ -91,43 +91,40 @@ func (d *taskDao) ClaimPendingByTaskIDGlobal(ctx context.Context, taskID string)
return
}
func (d *taskDao) UpdateSuccessGlobal(ctx context.Context, id int64, ossFile, fileType, textResult string, fileSize int64, expireAt *gtime.Time, expendTokens int) error {
// UpdateSuccessGlobal 更新任务成功
func (d *taskDao) UpdateSuccessGlobal(ctx context.Context, t *entity.AsynchTask) error {
now := gtime.Now()
_, err := gfdb.DB(ctx).Exec(ctx,
fmt.Sprintf(`UPDATE %s
SET state=2,
oss_file=?,
file_type=?,
text_result=?,
expend_tokens=?,
file_size=?,
error_msg='',
finished_at=?,
duration_seconds=EXTRACT(EPOCH FROM (? - created_at))::BIGINT,
expire_at=NULL,
phase=0,
tmp_file='',
updated_at=?
WHERE id=?`, public.TableNameTask),
ossFile, fileType, textResult, expendTokens, fileSize, now, now, now, id,
)
_, err := gfdb.DB(ctx).Model(ctx, public.TableNameTask).OmitEmpty().
Where(entity.AsynchTaskCol.Id, t.Id).
Data(entity.AsynchTask{
State: 2,
OssFile: t.OssFile,
FileType: t.FileType,
TextResult: t.TextResult,
FileSize: t.FileSize,
ErrorMsg: "",
FinishedAt: now,
Phase: 0,
TmpFile: "",
ExpendTokens: t.ExpendTokens,
}).
Update()
return err
}
func (d *taskDao) UpdateFailedGlobal(ctx context.Context, id int64, errorMsg string) error {
// UpdateFailedGlobal 模型调用失败
func (d *taskDao) UpdateFailedGlobal(ctx context.Context, t *entity.AsynchTask) error {
now := gtime.Now()
_, err := gfdb.DB(ctx).Exec(ctx,
fmt.Sprintf(`UPDATE %s
SET state=3,
error_msg=?,
finished_at=?,
duration_seconds=EXTRACT(EPOCH FROM (? - created_at))::BIGINT,
phase=0,
tmp_file='',
updated_at=?
WHERE id=?`, public.TableNameTask),
errorMsg, now, now, now, id,
)
_, err := gfdb.DB(ctx).Model(ctx, public.TableNameTask).OmitEmpty().
Where(entity.AsynchTaskCol.Id, t.Id).
Data(entity.AsynchTask{
State: 3,
ErrorMsg: t.ErrorMsg,
FinishedAt: now,
Phase: 0,
TmpFile: "",
}).
Update()
return err
}