fix(idgen): retry snowflake generation and error on negative ID

NextUint64ID now retries up to 3 times when Int64() is negative, then
returns an error instead of 0 or Fatalf. All call sites propagate the
error to avoid GORM omitting zero-value primary keys.
This commit is contained in:
ryan
2026-06-17 13:08:00 +08:00
parent a27113feb2
commit 92dc6a59c0
11 changed files with 145 additions and 44 deletions
+10 -4
View File
@@ -115,7 +115,10 @@ func DispatchTask(ctx context.Context, taskType string, payload []byte, triggere
}
// 生成唯一的 TaskID
taskID := generateTaskID(taskType, triggeredBy)
taskID, err := generateTaskID(taskType, triggeredBy)
if err != nil {
return "", err
}
// 创建任务执行记录
execution := &model.TaskExecution{
@@ -453,9 +456,12 @@ func handleSuccessfulTask(ctx context.Context, execution *model.TaskExecution, t
}
// generateTaskID 生成任务 ID
func generateTaskID(taskType string, triggeredBy string) string {
uniqueID := idgen.NextUint64ID()
return fmt.Sprintf("%s_%s_%d", triggeredBy, taskType, uniqueID)
func generateTaskID(taskType string, triggeredBy string) (string, error) {
uniqueID, err := idgen.NextUint64ID()
if err != nil {
return "", err
}
return fmt.Sprintf("%s_%s_%d", triggeredBy, taskType, uniqueID), nil
}
// generateRetryTaskID 生成重试任务 ID