| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 5 | "context" |
| 6 | "gorm.io/gorm" |
| 7 | ) |
| 8 | |
| 9 | func (s *Store) OldRunnerStopped(ctx context.Context, actor Actor, id string) error { |
| 10 | if s.execution == nil { |
| 11 | return ErrUnavailable |
| 12 | } |
| 13 | return s.execution.atomic(ctx, func(ctx context.Context, tx *gorm.DB) error { |
| 14 | member, err := currentRunnerMember(tx, actor.Address) |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | if member.Role != RoleOwner { |
| 19 | return ErrForbidden |
| 20 | } |
| 21 | var task Task |
| 22 | if err = tx.First(&task, "id=?", id).Error; err != nil { |
| 23 | return storeError(err) |
| 24 | } |
| 25 | if task.WaitCode != "task.wait.reconcile" { |
| 26 | return ErrConflict |
| 27 | } |
| 28 | if task.JobID != 0 { |
| 29 | scope := Scope{Account: team.AccountID(task.TeamID), Job: task.JobID} |
| 30 | attempts, err := s.execution.Core.Attempts(ctx, scope) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | for _, attempt := range attempts { |
| 35 | var direct int64 |
| 36 | if err = tx.Model(&runnerAssignment{}).Where("task_id=? AND attempt_id=? AND protocol_version!=1", id, attempt.ID).Count(&direct).Error; err != nil { |
| 37 | return storeError(err) |
| 38 | } |
| 39 | if direct != 0 && attempt.Containment != team.ContainmentContained { |
| 40 | return ErrConflict |
| 41 | } |
| 42 | if direct == 0 { |
| 43 | if err = s.execution.Core.Contain(ctx, scope, attempt.ID, actor.Address); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | if task.State == "cancel_requested" { |
| 49 | if _, err = s.execution.Core.Cancel(ctx, scope, actor.Address, "person_cancelled"); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | } else { |
| 53 | if err = s.execution.Core.ResumeContained(ctx, scope); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | if err = tx.Model(&runnerAssignment{}).Where("task_id=? AND protocol_version=1", id).Select("State").Updates(&runnerAssignment{State: "contained"}).Error; err != nil { |
| 59 | return storeError(err) |
| 60 | } |
| 61 | state, wait := "waiting", "task.wait.setup" |
| 62 | if task.State == "cancel_requested" { |
| 63 | state, wait = "cancel_requested", "" |
| 64 | } |
| 65 | if err = tx.Model(&Task{}).Where("id=?", id).Select("State", "WaitCode", "Revision").Updates(&Task{State: state, WaitCode: wait, Revision: task.Revision + 1}).Error; err != nil { |
| 66 | return storeError(err) |
| 67 | } |
| 68 | out := "prepare" |
| 69 | if task.JobID != 0 { |
| 70 | out = "done" |
| 71 | } |
| 72 | if err = tx.Model(&taskOutbox{}).Where("task_id=?", id).Select("State").Updates(&taskOutbox{State: out}).Error; err != nil { |
| 73 | return storeError(err) |
| 74 | } |
| 75 | if err = finishTaskCancellation(tx, id); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | return appendAudit(tx, actor, "task.old_runner_stopped", id) |
| 79 | }) |
| 80 | } |
| 81 | |