| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 6 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "amadan.net/oficina/equipo/internal/release" |
| 9 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 10 | "context" |
| 11 | "crypto/rand" |
| 12 | "errors" |
| 13 | "gorm.io/gorm" |
| 14 | "math" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | type Actor = kernel.Actor |
| 19 | type Mutation = kernel.Mutation |
| 20 | type Application struct { |
| 21 | BindingSelection func(*gorm.DB, BindingRecord) (runnerproto.Selection, error) |
| 22 | WaitingConnection error |
| 23 | BindingRelease *release.Manifest |
| 24 | Copy func(string) string |
| 25 | DecodeConnection func(*planning.ConnectionRecord) error |
| 26 | BindingEligible func(*gorm.DB, planning.ConnectionRecord, runnerproto.ConnectionReport) (bool, error) |
| 27 | Projects func(*gorm.DB) *setup.ProjectsStore |
| 28 | ProjectCreated, ProjectDeclined string |
| 29 | Read func(context.Context, func(*gorm.DB) error) error |
| 30 | Now func() time.Time |
| 31 | Core *team.Store |
| 32 | Write func(context.Context, func(*gorm.DB) error) error |
| 33 | Conversation func(*gorm.DB) *Store |
| 34 | Normalize func(string) string |
| 35 | BindingIdentity func(*gorm.DB, Actor, string, string) (int64, int64, error) |
| 36 | LimitsAuthority func(*gorm.DB, Actor, string) error |
| 37 | Audit func(*gorm.DB, Actor, string, string) error |
| 38 | Invalid, Conflict, Forbidden, Unavailable, NotFound error |
| 39 | } |
| 40 | |
| 41 | func (s *Application) Coordinator(ctx context.Context, actor Actor) (CoordinatorBinding, error) { |
| 42 | var b BindingRecord |
| 43 | err := s.Write(ctx, func(tx *gorm.DB) error { var err error; b, err = s.Conversation(tx).Binding(actor); return err }) |
| 44 | return b.CoordinatorBinding, err |
| 45 | } |
| 46 | func (s *Application) SaveCoordinatorBinding(ctx context.Context, actor Actor, runnerID, connectionID string, m Mutation) (CoordinatorBinding, error) { |
| 47 | return s.changeCoordinator(ctx, actor, runnerID, connectionID, false, m) |
| 48 | } |
| 49 | func (s *Application) ResetCoordinator(ctx context.Context, actor Actor, m Mutation) (CoordinatorBinding, error) { |
| 50 | return s.changeCoordinator(ctx, actor, "", "", true, m) |
| 51 | } |
| 52 | func (s *Application) changeCoordinator(ctx context.Context, actor Actor, runnerID, connectionID string, reset bool, m Mutation) (CoordinatorBinding, error) { |
| 53 | actor.Address = s.Normalize(actor.Address) |
| 54 | var result CoordinatorBinding |
| 55 | pending := false |
| 56 | op := "coordinator.bind" |
| 57 | if reset { |
| 58 | op = "coordinator.reset" |
| 59 | } |
| 60 | input := struct { |
| 61 | Runner, Connection string |
| 62 | Reset bool |
| 63 | Revision int64 |
| 64 | }{runnerID, connectionID, reset, m.Revision} |
| 65 | err := s.Write(ctx, func(tx *gorm.DB) error { |
| 66 | ctx := tx.Statement.Context |
| 67 | teamID, err := s.Conversation(tx).deps.Authorize(tx, actor) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | cs := s.Conversation(tx) |
| 72 | if ok, err := cs.CoordinatorReplay(actor, op, actor.Address, m, input, &result); ok || err != nil { |
| 73 | return err |
| 74 | } |
| 75 | b, err := cs.Binding(actor) |
| 76 | exists := err == nil |
| 77 | if err != nil && !errors.Is(err, s.NotFound) { |
| 78 | return err |
| 79 | } |
| 80 | if (!exists && reset) || m.Revision != b.Revision { |
| 81 | return s.Conflict |
| 82 | } |
| 83 | if reset { |
| 84 | runnerID, connectionID = b.RunnerID, b.ConnectionID |
| 85 | } |
| 86 | memberRevision, connectionRevision, err := s.BindingIdentity(tx, actor, runnerID, connectionID) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if !reset { |
| 91 | candidate := BindingRecord{CoordinatorBinding: CoordinatorBinding{Person: actor.Address, RunnerID: runnerID, ConnectionID: connectionID}, MemberRevision: memberRevision, ConnectionRevision: connectionRevision} |
| 92 | if _, err := s.BindingSelection(tx, candidate); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | } |
| 96 | var turns []TurnRecord |
| 97 | if exists { |
| 98 | if err = tx.Where("binding_id=? AND job_id>0 AND state<>'superseded'", b.ID).Find(&turns).Error; err != nil { |
| 99 | return err |
| 100 | } |
| 101 | for _, turn := range turns { |
| 102 | if s.Core == nil { |
| 103 | return s.Unavailable |
| 104 | } |
| 105 | scope := team.Scope{Account: team.AccountID(teamID), Job: team.JobID(turn.JobID)} |
| 106 | job, err := s.Core.GetJob(ctx, scope) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | if !job.State.Terminal() { |
| 111 | if _, err = s.Core.Cancel(ctx, scope, actor.Address, op); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | var n int64 |
| 116 | if err = tx.Table("attempts").Where("job_id=? AND containment='pending'", turn.JobID).Count(&n).Error; err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if n > 0 { |
| 120 | pending = true |
| 121 | } |
| 122 | if turn.State != "delivered" { |
| 123 | if err = tx.Model(&TurnRecord{}).Where("id=? AND state<>'uncertain'", turn.ID).Select("state", "revision").Updates(map[string]any{"state": "uncertain", "revision": gorm.Expr("revision+1")}).Error; err != nil { |
| 124 | return err |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if err = cs.clearCoordinatorWaiting(b); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | b.WaitingTaskID, b.WaitingTaskURL = "", "" |
| 133 | if pending { |
| 134 | return s.Audit(tx, actor, op+".containment", b.ID) |
| 135 | } |
| 136 | if b.Generation == math.MaxInt64 || b.Revision == math.MaxInt64 { |
| 137 | return s.Conflict |
| 138 | } |
| 139 | if !exists { |
| 140 | b.ID = rand.Text() |
| 141 | b.TeamID = teamID |
| 142 | b.PersonKey = actor.Address |
| 143 | b.Person = actor.Address |
| 144 | } |
| 145 | b.RunnerID, b.ConnectionID = runnerID, connectionID |
| 146 | b.Generation++ |
| 147 | b.Revision++ |
| 148 | b.MemberRevision = memberRevision |
| 149 | b.ConnectionRevision = connectionRevision |
| 150 | if exists { |
| 151 | err = tx.Model(&BindingRecord{}).Where("id=?", b.ID).Select("runner_id", "connection_id", "generation", "revision", "member_revision", "connection_revision").Updates(&b).Error |
| 152 | } else { |
| 153 | err = tx.Create(&b).Error |
| 154 | } |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if err = tx.Model(&TurnRecord{}).Where("binding_id=? AND state IN ('queued','waiting_connection','waiting_task','budget_needed','admission_denied','context_limit','context_unavailable')", b.ID).Select("state", "revision").Updates(map[string]any{"state": "superseded", "revision": gorm.Expr("revision+1")}).Error; err != nil { |
| 159 | return err |
| 160 | } |
| 161 | result = b.CoordinatorBinding |
| 162 | if err = s.Audit(tx, actor, op, b.ID); err != nil { |
| 163 | return err |
| 164 | } |
| 165 | return cs.SaveCoordinatorResult(actor, op, actor.Address, m, input, result) |
| 166 | }) |
| 167 | if err == nil && pending { |
| 168 | err = s.Conflict |
| 169 | } |
| 170 | return result, err |
| 171 | } |
| 172 | func (s *Application) QueueCoordinatorTurn(ctx context.Context, actor Actor, messageID string, projectID int64, m Mutation) (CoordinatorTurn, error) { |
| 173 | var out CoordinatorTurn |
| 174 | if projectID < 0 { |
| 175 | return out, s.Invalid |
| 176 | } |
| 177 | err := s.Write(ctx, func(tx *gorm.DB) error { |
| 178 | var err error |
| 179 | out, err = s.Conversation(tx).QueueCoordinator(actor, messageID, projectID, m) |
| 180 | return err |
| 181 | }) |
| 182 | return out, err |
| 183 | } |
| 184 | func (s *Application) RetryCoordinatorTurn(ctx context.Context, actor Actor, id string, m Mutation) (CoordinatorTurn, error) { |
| 185 | var out CoordinatorTurn |
| 186 | err := s.Write(ctx, func(tx *gorm.DB) error { |
| 187 | var err error |
| 188 | out, err = s.Conversation(tx).RetryCoordinator(actor, id, m) |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | return s.Audit(tx, actor, "coordinator.retry", out.ID) |
| 193 | }) |
| 194 | return out, err |
| 195 | } |
| 196 | func (s *Application) SetCoordinatorLimits(ctx context.Context, actor Actor, person string, limits team.Limits, m Mutation) error { |
| 197 | if s.Core == nil { |
| 198 | return s.Unavailable |
| 199 | } |
| 200 | person = s.Normalize(person) |
| 201 | input := struct { |
| 202 | Person string |
| 203 | Limits team.Limits |
| 204 | Revision int64 |
| 205 | }{person, limits, m.Revision} |
| 206 | return s.Write(ctx, func(tx *gorm.DB) error { |
| 207 | if err := s.LimitsAuthority(tx, actor, person); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | teamID, err := s.Conversation(tx).deps.Authorize(tx, actor) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | var result bool |
| 215 | cs := s.Conversation(tx) |
| 216 | if ok, err := cs.CoordinatorReplay(actor, "coordinator.limits", person, m, input, &result); ok || err != nil { |
| 217 | return err |
| 218 | } |
| 219 | if err = s.Core.ConfigureCoordinatorLimits(tx.Statement.Context, team.AccountID(teamID), person, m.Revision, limits); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | if err = s.Audit(tx, actor, "coordinator.limits", person); err != nil { |
| 223 | return err |
| 224 | } |
| 225 | return cs.SaveCoordinatorResult(actor, "coordinator.limits", person, m, input, true) |
| 226 | }) |
| 227 | } |
| 228 | |