fix: prevent duplicate suno task refunds via cas status update (#6074)
* fix: prevent duplicate suno task refunds via cas status update * fix: reconcile failed task refunds --------- Co-authored-by: CaIon <i@caion.me>
This commit is contained in:
+80
-1
@@ -41,6 +41,10 @@ const (
|
||||
TaskStatusUnknown = "UNKNOWN"
|
||||
)
|
||||
|
||||
// TaskRefundLegacyCutoff separates legacy timeout tasks that intentionally
|
||||
// do not receive automatic refunds from tasks covered by reconciliation.
|
||||
const TaskRefundLegacyCutoff int64 = 1740182400 // 2025-02-22 00:00:00 UTC
|
||||
|
||||
type Task struct {
|
||||
ID int64 `json:"id" gorm:"primary_key;AUTO_INCREMENT"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"index"`
|
||||
@@ -304,6 +308,28 @@ func GetTimedOutUnfinishedTasks(cutoffUnix int64, limit int) []*Task {
|
||||
return tasks
|
||||
}
|
||||
|
||||
// GetUnrefundedFailedTasks returns failed tasks whose non-zero quota marks a
|
||||
// pending refund. Legacy timeout tasks are excluded before LIMIT is applied so
|
||||
// they cannot starve refundable tasks from the reconciliation sweep.
|
||||
func GetUnrefundedFailedTasks(updatedBefore int64, limit int) []*Task {
|
||||
if limit <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var tasks []*Task
|
||||
err := DB.Where("status = ?", TaskStatusFailure).
|
||||
Where("quota != ?", 0).
|
||||
Where("updated_at <= ?", updatedBefore).
|
||||
Where("(submit_time <= ? OR submit_time >= ?)", 0, TaskRefundLegacyCutoff).
|
||||
Order("id").
|
||||
Limit(limit).
|
||||
Find(&tasks).Error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return tasks
|
||||
}
|
||||
|
||||
func GetAllUnFinishSyncTasks(limit int) []*Task {
|
||||
var tasks []*Task
|
||||
var err error
|
||||
@@ -330,6 +356,24 @@ func HasUnfinishedSyncTasks() bool {
|
||||
return err == nil && id != 0
|
||||
}
|
||||
|
||||
// HasTaskPollingWork reports whether polling has either an unfinished task or
|
||||
// a failed task with a pending, non-legacy refund. The latter keeps the system
|
||||
// task scheduler active when reconciliation is the only work left.
|
||||
func HasTaskPollingWork() bool {
|
||||
if HasUnfinishedSyncTasks() {
|
||||
return true
|
||||
}
|
||||
|
||||
var id int64
|
||||
err := DB.Model(&Task{}).
|
||||
Where("status = ?", TaskStatusFailure).
|
||||
Where("quota != ?", 0).
|
||||
Where("(submit_time <= ? OR submit_time >= ?)", 0, TaskRefundLegacyCutoff).
|
||||
Limit(1).
|
||||
Pluck("id", &id).Error
|
||||
return err == nil && id != 0
|
||||
}
|
||||
|
||||
func GetByOnlyTaskId(taskId string) (*Task, bool, error) {
|
||||
if taskId == "" {
|
||||
return nil, false, nil
|
||||
@@ -421,9 +465,44 @@ func (t *Task) UpdateQuota() error {
|
||||
return DB.Model(t).Update("quota", t.Quota).Error
|
||||
}
|
||||
|
||||
// ClaimQuotaForRefund atomically clears an expected non-zero quota. A true
|
||||
// result grants the caller ownership of the corresponding refund attempt.
|
||||
func ClaimQuotaForRefund(id int64, expectedQuota int) (bool, error) {
|
||||
if expectedQuota == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
result := DB.Model(&Task{}).
|
||||
Where("id = ? AND quota = ?", id, expectedQuota).
|
||||
Update("quota", 0)
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return result.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
// RestoreQuotaAfterFailedRefund restores a claimed quota marker only while it
|
||||
// is still zero. It is used when the observable funding adjustment fails, so a
|
||||
// later reconciliation pass can retry without overwriting another writer.
|
||||
func RestoreQuotaAfterFailedRefund(id int64, quota int) (bool, error) {
|
||||
if quota == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
result := DB.Model(&Task{}).
|
||||
Where("id = ? AND quota = ?", id, 0).
|
||||
Update("quota", quota)
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return result.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
// UpdateWithStatus performs a conditional UPDATE guarded by fromStatus (CAS).
|
||||
// Returns (true, nil) if this caller won the update, (false, nil) if
|
||||
// another process already moved the task out of fromStatus.
|
||||
// another process already moved the task out of fromStatus. MySQL commonly
|
||||
// reports changed rows rather than matched rows, so a same-value no-op update
|
||||
// can also return false even when the status predicate still matched.
|
||||
//
|
||||
// Uses Model().Select("*").Updates() instead of Save() because GORM's Save
|
||||
// falls back to INSERT ON CONFLICT when the WHERE-guarded UPDATE matches
|
||||
|
||||
Reference in New Issue
Block a user