feat: add shared media assets and fix credit lifecycle
This commit is contained in:
+27
-3
@@ -125,8 +125,21 @@ func (a *App) retryTask(c *gin.Context) {
|
||||
c.Status(404)
|
||||
return
|
||||
}
|
||||
a.db.Model(&t).Updates(map[string]any{"status": "pending", "error": "", "progress": 0, "downloaded_bytes": 0})
|
||||
t.Status, t.Error, t.Progress, t.DownloadedBytes = "pending", "", 0, 0
|
||||
if t.Status != "error" {
|
||||
c.JSON(409, gin.H{"error": "only failed tasks can be retried"})
|
||||
return
|
||||
}
|
||||
r, reserveErr := a.reserveCredit(user.ID, t.ID, 1)
|
||||
if reserveErr != nil {
|
||||
c.JSON(402, gin.H{"error": reserveErr.Error()})
|
||||
return
|
||||
}
|
||||
if err := a.db.Model(&t).Updates(map[string]any{"status": "pending", "error": "", "progress": 0, "downloaded_bytes": 0, "credit_reservation_id": r.ID}).Error; err != nil {
|
||||
_ = a.refundCredit(&Task{ID: t.ID, UserID: user.ID, CreditReservationID: r.ID})
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
t.Status, t.Error, t.Progress, t.DownloadedBytes, t.CreditReservationID = "pending", "", 0, 0, r.ID
|
||||
a.queue <- t.ID
|
||||
c.JSON(202, t)
|
||||
}
|
||||
@@ -138,7 +151,18 @@ func (a *App) deleteTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
user := c.MustGet("user").(*User)
|
||||
if err := a.db.Where("id = ? AND user_id = ?", id, user.ID).Delete(&Task{}).Error; err != nil {
|
||||
var t Task
|
||||
if err := a.db.Where("id = ? AND user_id = ?", id, user.ID).First(&t).Error; err != nil {
|
||||
c.Status(404)
|
||||
return
|
||||
}
|
||||
if t.CreditReservationID != 0 {
|
||||
if err := a.refundCredit(&t); err != nil {
|
||||
c.JSON(409, gin.H{"error": "cannot release task credit: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := a.db.Delete(&t).Error; err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user