| 307 | } |
| 308 | |
| 309 | func UpdateSystemTaskState(taskID string, lockedBy string, state any) error { |
| 310 | stateText, err := marshalSystemTaskJSON(state) |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | now := common.GetTimestamp() |
| 315 | result := DB.Model(&SystemTask{}). |
| 316 | Where("task_id = ? AND status = ? AND locked_by = ?", taskID, SystemTaskStatusRunning, lockedBy). |
| 317 | Where("EXISTS (SELECT 1 FROM system_task_locks WHERE system_task_locks.task_id = system_tasks.task_id AND system_task_locks.locked_by = ? AND system_task_locks.locked_until >= ?)", lockedBy, now). |
| 318 | Updates(map[string]any{ |
| 319 | "state": stateText, |
| 320 | "updated_at": now, |
| 321 | }) |
| 322 | if result.Error != nil { |
| 323 | return result.Error |
| 324 | } |
| 325 | if result.RowsAffected == 0 { |
| 326 | return ErrSystemTaskLockLost |
| 327 | } |
| 328 | return nil |
| 329 | } |
| 330 | |
| 331 | func RenewSystemTaskLock(taskID string, lockedBy string, lockUntil int64) error { |
| 332 | now := common.GetTimestamp() |