| 1 | package ownermode |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "encoding/json" |
| 7 | "gorm.io/gorm" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TurnStatus(tx *gorm.DB, state string, attemptID int64, claimJSON string) (string, []string, bool, error) { |
| 12 | |
| 13 | if state == "running" { |
| 14 | var attempt struct{ LeaseDeadline string } |
| 15 | if err := tx.Table("attempts").Select("lease_deadline").Where("id=?", attemptID).Take(&attempt).Error; err != nil { |
| 16 | return "", nil, false, err |
| 17 | } |
| 18 | var claim runnerproto.CoordinatorClaim |
| 19 | if err := json.Unmarshal([]byte(claimJSON), &claim); err != nil { |
| 20 | return "", nil, false, err |
| 21 | } |
| 22 | now := time.Now().UTC() |
| 23 | if !now.Before(kernel.ReadTime(attempt.LeaseDeadline)) || !claim.AttemptDeadline.IsZero() && !now.Before(claim.AttemptDeadline) { |
| 24 | state = "timed_out" |
| 25 | } |
| 26 | } |
| 27 | var actions []string |
| 28 | switch state { |
| 29 | case "context_limit", "context_unavailable", "fenced": |
| 30 | actions = []string{"coordinator.reset", "coordinator.retry"} |
| 31 | case "uncertain": |
| 32 | var n int64 |
| 33 | if err := tx.Table("attempts").Where("id=? AND containment='contained'", attemptID).Count(&n).Error; err != nil { |
| 34 | return "", nil, false, err |
| 35 | } |
| 36 | if n == 1 { |
| 37 | actions = []string{"coordinator.reset", "coordinator.retry"} |
| 38 | } |
| 39 | case "admission_denied", "waiting_connection", "budget_needed", "timed_out": |
| 40 | default: |
| 41 | return "", nil, false, nil |
| 42 | } |
| 43 | return state, actions, true, nil |
| 44 | } |
| 45 | |