oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5
6 "amadan.net/oficina/equipo/internal/equipo/personapi"
7 "amadan.net/oficina/equipo/internal/equipo/team"
8 "gorm.io/gorm"
9)
10
11type TaskCancellation struct {
12 Contained bool
13 InFlight int
14 Report team.CancelReport
15}
16
17func (s *Store) CancelTask(ctx context.Context, actor Actor, id string) (TaskCancellation, error) {
18 return s.cancelTask(ctx, actor, id, nil)
19}
20func (s *Store) cancelTask(ctx context.Context, actor Actor, id string, m *Mutation) (TaskCancellation, error) {
21 var result TaskCancellation
22 if s.execution == nil {
23 return result, ErrUnavailable
24 }
25 err := s.write(ctx, func(tx *gorm.DB) error {
26 ctx := tx.Statement.Context
27 member, err := currentRunnerMember(tx, actor.Address)
28 if err != nil {
29 return err
30 }
31 var task Task
32 if err = tx.First(&task, "id=?", id).Error; err != nil {
33 return storeError(err)
34 }
35 if member.Address != task.Requester && member.Role != RoleOwner {
36 return ErrForbidden
37 }
38 if m != nil {
39 if ok, e := personapi.Replay(tx, ctx, &result, ErrConflict); ok || e != nil {
40 return e
41 }
42 if task.Revision != m.Revision {
43 return ErrConflict
44 }
45 }
46 result.Contained = true
47 if task.JobID != 0 {
48 result.Report, err = s.execution.Core.Cancel(ctx, Scope{Account: team.AccountID(task.TeamID), Job: task.JobID}, actor.Address, "person_cancelled")
49 if err != nil {
50 return err
51 }
52 result.InFlight = len(result.Report.InFlight)
53 attempts, err := s.execution.Core.Attempts(ctx, Scope{Account: team.AccountID(task.TeamID), Job: task.JobID})
54 if err != nil {
55 return err
56 }
57 result.Contained = true
58 for _, a := range attempts {
59 if a.Containment != team.ContainmentContained {
60 result.Contained = false
61 }
62 }
63 }
64 var pending int64
65 if err = tx.Model(&runnerAssignment{}).Where("task_id=? AND state!='contained' AND claim_request!=''", id).Count(&pending).Error; err != nil {
66 return storeError(err)
67 }
68 if pending > 0 {
69 result.Contained = false
70 }
71 if err = tx.Model(&runnerAssignment{}).Where("task_id=? AND state IN ?", id, []string{"pending", "claimed", "prepared"}).Select("State").Updates(&runnerAssignment{State: "fenced"}).Error; err != nil {
72 return storeError(err)
73 }
74 state := "cancel_requested"
75 if result.Contained {
76 state = "cancelled"
77 }
78 if err = tx.Model(&Task{}).Where("id=?", id).Select("State", "Revision").Updates(&Task{State: state, Revision: task.Revision + 1}).Error; err != nil {
79 return storeError(err)
80 }
81 if err = appendAudit(tx, actor, "task.cancel", id); err != nil {
82 return err
83 }
84 return personapi.SaveResult(tx, ctx, result, ErrConflict)
85 })
86 return result, err
87}
88
89func finishTaskCancellation(tx *gorm.DB, taskID string) error {
90 var pending int64
91 if err := tx.Model(&runnerAssignment{}).Where("task_id=? AND state!='contained' AND claim_request!=''", taskID).Count(&pending).Error; err != nil {
92 return storeError(err)
93 }
94 if pending > 0 {
95 return nil
96 }
97 return storeError(tx.Model(&Task{}).Where("id=? AND state='cancel_requested'", taskID).Select("State").Updates(&Task{State: "cancelled"}).Error)
98}
99