-
Notifications
You must be signed in to change notification settings - Fork 251
/
actors.go
207 lines (171 loc) · 5.88 KB
/
actors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package task
import (
"fmt"
"io"
"time"
"github.com/spf13/pflag"
"github.com/taskcluster/slugid-go/slugid"
tcclient "github.com/taskcluster/taskcluster/v42/clients/client-go"
"github.com/taskcluster/taskcluster/v42/clients/client-go/tcqueue"
)
// runCancel cancels the runs of a given task.
func runCancel(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
noop, _ := flagSet.GetBool("noop")
confirm, _ := flagSet.GetBool("confirm")
q := makeQueue(credentials)
taskID := args[0]
if noop {
return displayNoopMsg("Would cancel", credentials, args)
}
if confirm {
var confirm = confirmMsg("Cancels", credentials, args)
if !confirm {
return nil
}
}
c, err := q.CancelTask(taskID)
if err != nil {
log.Error(err)
return fmt.Errorf("could not cancel the task %s: %v", taskID, err)
}
run := c.Status.Runs[len(c.Status.Runs)-1]
fmt.Fprintln(out, getRunStatusString(run.State, run.ReasonResolved))
return nil
}
// runRerun re-runs a given task.
func runRerun(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
noop, _ := flagSet.GetBool("noop")
confirm, _ := flagSet.GetBool("confirm")
force, _ := flagSet.GetBool("force")
q := makeQueue(credentials)
taskID := args[0]
if noop {
return displayNoopMsg("Would re-run", credentials, args)
}
if confirm {
var confirm = confirmMsg("Will re-run", credentials, args)
if !confirm {
return nil
}
}
if !force {
s, err := q.Status(taskID)
if err != nil {
return fmt.Errorf("could not get status of the task %s: %v", taskID, err)
}
if s.Status.State != "failed" && s.Status.State != "exception" {
return fmt.Errorf("Task %s is in state %s. Disallowing rerun of a non-failed and non-exception task without --force", taskID, s.Status.State)
}
}
c, err := q.RerunTask(taskID)
if err != nil {
return fmt.Errorf("could not rerun the task %s: %v", taskID, err)
}
run := c.Status.Runs[len(c.Status.Runs)-1]
fmt.Fprintln(out, getRunStatusString(run.State, run.ReasonResolved))
return nil
}
// runRetrigger re-triggers a given task.
// It will generate a new taskId, update timestamps and retries to 0
// Optionnally, you can pass '--exact' to keep stuff like:
// - routes,
// - dependencies,
// - ...
//
// Otherwise, default behavior is to omit those as taskcluster-tools does:
// https://github.com/taskcluster/taskcluster-tools/blob/e8b6d45f10e7520f717b7a9f5db87d550c74d15e/src/views/UnifiedInspector/ActionsMenu.jsx#L141-L158
func runRetrigger(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
q := makeQueue(credentials)
taskID := args[0]
t, err := q.Task(taskID)
if err != nil {
return fmt.Errorf("could not get the task %s: %v", taskID, err)
}
exactRetrigger, _ := flagSet.GetBool("exact")
newTaskID := slugid.Nice()
now := time.Now().UTC()
origCreated, err := time.Parse(time.RFC3339, t.Created.String())
if err != nil {
return fmt.Errorf("could not parse created date: %s", t.Created)
}
origDeadline, err := time.Parse(time.RFC3339, t.Deadline.String())
if err != nil {
return fmt.Errorf("could not parse deadline date: %s", t.Deadline)
}
origExpires, err := time.Parse(time.RFC3339, t.Expires.String())
if err != nil {
return fmt.Errorf("could not parse created date: %s", t.Expires)
}
// TaskDefinitionRequest: https://github.com/taskcluster/taskcluster-client-go/blob/88cfe471bfe2eb8fc9bc22d9cde6a65e74a9f3e5/tcqueue/types.go#L1368-L1549
// TaskDefinitionResponse: https://github.com/taskcluster/taskcluster-client-go/blob/88cfe471bfe2eb8fc9bc22d9cde6a65e74a9f3e5/tcqueue/types.go#L1554-L1716
newDependencies := []string{}
newRoutes := []string{}
if exactRetrigger {
newDependencies = t.Dependencies
newRoutes = t.Routes
}
newT := &tcqueue.TaskDefinitionRequest{
Created: tcclient.Time(now),
Deadline: tcclient.Time(now.Add(origDeadline.Sub(origCreated))),
Expires: tcclient.Time(now.Add(origExpires.Sub(origCreated))),
TaskGroupID: t.TaskGroupID,
SchedulerID: t.SchedulerID,
WorkerType: t.WorkerType,
ProvisionerID: t.ProvisionerID,
Priority: t.Priority,
Dependencies: newDependencies,
Extra: t.Extra,
Metadata: t.Metadata,
Payload: t.Payload,
Requires: t.Requires,
Retries: 0,
Routes: newRoutes,
Scopes: t.Scopes,
Tags: t.Tags,
}
c, err := q.CreateTask(newTaskID, newT)
if err != nil {
return fmt.Errorf("could not create task: %v", err)
}
// If we got no error, that means the task was successfully submitted
fmt.Fprintf(out, "Task %s created\n", c.Status.TaskID)
return nil
}
// runComplete completes a given task.
func runComplete(credentials *tcclient.Credentials, args []string, out io.Writer, flagSet *pflag.FlagSet) error {
noop, _ := flagSet.GetBool("noop")
confirm, _ := flagSet.GetBool("confirm")
q := makeQueue(credentials)
taskID := args[0]
s, err := q.Status(taskID)
if err != nil {
return fmt.Errorf("could not get the status of the task %s: %v", taskID, err)
}
if noop {
return displayNoopMsg("Would complete", credentials, args)
}
if confirm {
var confirm = confirmMsg("Will complete", credentials, args)
if !confirm {
return nil
}
}
c, err := q.ClaimTask(taskID, fmt.Sprint(len(s.Status.Runs)-1), &tcqueue.TaskClaimRequest{
WorkerGroup: s.Status.WorkerType,
WorkerID: "taskcluster-cli",
})
if err != nil {
return fmt.Errorf("could not claim the task %s: %v", taskID, err)
}
wq := makeQueue(&tcclient.Credentials{
ClientID: c.Credentials.ClientID,
AccessToken: c.Credentials.AccessToken,
Certificate: c.Credentials.Certificate,
})
r, err := wq.ReportCompleted(taskID, fmt.Sprint(c.RunID))
if err != nil {
return fmt.Errorf("could not complete the task %s: %v", taskID, err)
}
fmt.Fprintln(out, getRunStatusString(r.Status.Runs[c.RunID].State, r.Status.Runs[c.RunID].ReasonResolved))
return nil
}