-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run_summary.go
352 lines (297 loc) · 10.5 KB
/
run_summary.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Package runsummary implements structs that report on a `turbo run` and `turbo run --dry`
package runsummary
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"sync"
"time"
"github.com/mitchellh/cli"
"github.com/segmentio/ksuid"
"github.com/vercel/turbo/cli/internal/ci"
"github.com/vercel/turbo/cli/internal/client"
"github.com/vercel/turbo/cli/internal/env"
"github.com/vercel/turbo/cli/internal/scm"
"github.com/vercel/turbo/cli/internal/spinner"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
"github.com/vercel/turbo/cli/internal/workspace"
)
// MissingTaskLabel is printed when a package is missing a definition for a task that is supposed to run
// E.g. if `turbo run build --dry` is run, and package-a doesn't define a `build` script in package.json,
// the RunSummary will print this, instead of the script (e.g. `next build`).
const MissingTaskLabel = "<NONEXISTENT>"
// NoFrameworkDetected is a string to identify when a workspace doesn't detect a framework
const NoFrameworkDetected = "<NO FRAMEWORK DETECTED>"
// FrameworkDetectionSkipped is a string to identify when framework detection was skipped
const FrameworkDetectionSkipped = "<FRAMEWORK DETECTION SKIPPED>"
const runSummarySchemaVersion = "0"
const runsEndpoint = "/v0/spaces/%s/runs"
const runsPatchEndpoint = "/v0/spaces/%s/runs/%s"
const tasksEndpoint = "/v0/spaces/%s/runs/%s/tasks"
type runType int
const (
runTypeReal runType = iota
runTypeDryText
runTypeDryJSON
)
// Meta is a wrapper around the serializable RunSummary, with some extra information
// about the Run and references to other things that we need.
type Meta struct {
RunSummary *RunSummary
ui cli.Ui
repoRoot turbopath.AbsoluteSystemPath // used to write run summary
repoPath turbopath.RelativeSystemPath
singlePackage bool
shouldSave bool
apiClient *client.APIClient
spaceID string
runType runType
synthesizedCommand string
}
// RunSummary contains a summary of what happens in the `turbo run` command and why.
type RunSummary struct {
ID ksuid.KSUID `json:"id"`
Version string `json:"version"`
TurboVersion string `json:"turboVersion"`
GlobalHashSummary *GlobalHashSummary `json:"globalCacheInputs"`
Packages []string `json:"packages"`
EnvMode util.EnvMode `json:"envMode"`
FrameworkInference bool `json:"frameworkInference"`
ExecutionSummary *executionSummary `json:"execution,omitempty"`
Tasks []*TaskSummary `json:"tasks"`
SCM *scmState `json:"scm"`
User string `json:"originationUser"`
}
// NewRunSummary returns a RunSummary instance
func NewRunSummary(
startAt time.Time,
ui cli.Ui,
repoRoot turbopath.AbsoluteSystemPath,
repoPath turbopath.RelativeSystemPath,
turboVersion string,
apiClient *client.APIClient,
runOpts util.RunOpts,
packages []string,
globalEnvMode util.EnvMode,
globalHashSummary *GlobalHashSummary,
synthesizedCommand string,
) Meta {
singlePackage := runOpts.SinglePackage
profile := runOpts.Profile
shouldSave := runOpts.Summarize
spaceID := runOpts.ExperimentalSpaceID
runType := runTypeReal
if runOpts.DryRun {
runType = runTypeDryText
if runOpts.DryRunJSON {
runType = runTypeDryJSON
}
}
executionSummary := newExecutionSummary(synthesizedCommand, repoPath, startAt, profile)
envVars := env.GetEnvMap()
return Meta{
RunSummary: &RunSummary{
ID: ksuid.New(),
Version: runSummarySchemaVersion,
ExecutionSummary: executionSummary,
TurboVersion: turboVersion,
Packages: packages,
EnvMode: globalEnvMode,
FrameworkInference: runOpts.FrameworkInference,
Tasks: []*TaskSummary{},
GlobalHashSummary: globalHashSummary,
SCM: getSCMState(envVars, repoRoot),
User: getUser(envVars, repoRoot),
},
ui: ui,
runType: runType,
repoRoot: repoRoot,
singlePackage: singlePackage,
shouldSave: shouldSave,
apiClient: apiClient,
spaceID: spaceID,
synthesizedCommand: synthesizedCommand,
}
}
// getPath returns a path to where the runSummary is written.
// The returned path will always be relative to the dir passsed in.
// We don't do a lot of validation, so `../../` paths are allowed.
func (rsm *Meta) getPath() turbopath.AbsoluteSystemPath {
filename := fmt.Sprintf("%s.json", rsm.RunSummary.ID)
return rsm.repoRoot.UntypedJoin(filepath.Join(".turbo", "runs"), filename)
}
// Close wraps up the RunSummary at the end of a `turbo run`.
func (rsm *Meta) Close(ctx context.Context, exitCode int, workspaceInfos workspace.Catalog) error {
if rsm.runType == runTypeDryJSON || rsm.runType == runTypeDryText {
return rsm.closeDryRun(workspaceInfos)
}
rsm.RunSummary.ExecutionSummary.exitCode = exitCode
rsm.RunSummary.ExecutionSummary.endedAt = time.Now()
summary := rsm.RunSummary
if err := writeChrometracing(summary.ExecutionSummary.profileFilename, rsm.ui); err != nil {
rsm.ui.Error(fmt.Sprintf("Error writing tracing data: %v", err))
}
// TODO: printing summary to local, writing to disk, and sending to API
// are all the same thng, we should use a strategy similar to cache save/upload to
// do this in parallel.
// Otherwise, attempt to save the summary
// Warn on the error, but we don't need to throw an error
if rsm.shouldSave {
if err := rsm.save(); err != nil {
rsm.ui.Warn(fmt.Sprintf("Error writing run summary: %v", err))
}
}
rsm.printExecutionSummary()
// If we don't have a spaceID, we can exit now
if rsm.spaceID == "" {
return nil
}
return rsm.sendToSpace(ctx)
}
func (rsm *Meta) sendToSpace(ctx context.Context) error {
if !rsm.apiClient.IsLinked() {
rsm.ui.Warn("Failed to post to space because repo is not linked to a Space. Run `turbo link` first.")
return nil
}
// Wrap the record function so we can hoist out url/errors but keep
// the function signature/type the spinner.WaitFor expects.
var url string
var errs []error
record := func() {
url, errs = rsm.record()
}
func() {
_ = spinner.WaitFor(ctx, record, rsm.ui, "...sending run summary...", 1000*time.Millisecond)
}()
// After the spinner is done, print any errors and the url
if len(errs) > 0 {
rsm.ui.Warn("Errors recording run to Spaces")
for _, err := range errs {
rsm.ui.Warn(fmt.Sprintf("%v", err))
}
}
if url != "" {
rsm.ui.Output(fmt.Sprintf("Run: %s", url))
rsm.ui.Output("")
}
return nil
}
// closeDryRun wraps up the Run Summary at the end of `turbo run --dry`.
// Ideally this should be inlined into Close(), but RunSummary doesn't currently
// have context about whether a run was real or dry.
func (rsm *Meta) closeDryRun(workspaceInfos workspace.Catalog) error {
// Render the dry run as json
if rsm.runType == runTypeDryJSON {
rendered, err := rsm.FormatJSON()
if err != nil {
return err
}
rsm.ui.Output(string(rendered))
return nil
}
return rsm.FormatAndPrintText(workspaceInfos)
}
// TrackTask makes it possible for the consumer to send information about the execution of a task.
func (summary *RunSummary) TrackTask(taskID string) (func(outcome executionEventName, err error, exitCode *int), *TaskExecutionSummary) {
return summary.ExecutionSummary.run(taskID)
}
// Save saves the run summary to a file
func (rsm *Meta) save() error {
json, err := rsm.FormatJSON()
if err != nil {
return err
}
// summaryPath will always be relative to the dir passsed in.
// We don't do a lot of validation, so `../../` paths are allowed
summaryPath := rsm.getPath()
if err := summaryPath.EnsureDir(); err != nil {
return err
}
return summaryPath.WriteFile(json, 0644)
}
// record sends the summary to the API
func (rsm *Meta) record() (string, []error) {
errs := []error{}
// Right now we'll send the POST to create the Run and the subsequent task payloads
// after all execution is done, but in the future, this first POST request
// can happen when the Run actually starts, so we can send updates to the associated Space
// as tasks complete.
createRunEndpoint := fmt.Sprintf(runsEndpoint, rsm.spaceID)
response := &spacesRunResponse{}
payload := rsm.newSpacesRunCreatePayload()
if startPayload, err := json.Marshal(payload); err == nil {
if resp, err := rsm.apiClient.JSONPost(createRunEndpoint, startPayload); err != nil {
errs = append(errs, fmt.Errorf("POST %s: %w", createRunEndpoint, err))
} else {
if err := json.Unmarshal(resp, response); err != nil {
errs = append(errs, fmt.Errorf("Error unmarshaling response: %w", err))
}
}
}
if response.ID != "" {
if taskErrs := rsm.postTaskSummaries(response.ID); len(taskErrs) > 0 {
errs = append(errs, taskErrs...)
}
if donePayload, err := json.Marshal(newSpacesDonePayload(rsm.RunSummary)); err == nil {
patchURL := fmt.Sprintf(runsPatchEndpoint, rsm.spaceID, response.ID)
if _, err := rsm.apiClient.JSONPatch(patchURL, donePayload); err != nil {
errs = append(errs, fmt.Errorf("PATCH %s: %w", patchURL, err))
}
}
}
if len(errs) > 0 {
return response.URL, errs
}
return response.URL, nil
}
func (rsm *Meta) postTaskSummaries(runID string) []error {
errs := []error{}
// We make at most 8 requests at a time.
maxParallelRequests := 8
taskSummaries := rsm.RunSummary.Tasks
taskCount := len(taskSummaries)
taskURL := fmt.Sprintf(tasksEndpoint, rsm.spaceID, runID)
parallelRequestCount := maxParallelRequests
if taskCount < maxParallelRequests {
parallelRequestCount = taskCount
}
queue := make(chan int, taskCount)
wg := &sync.WaitGroup{}
for i := 0; i < parallelRequestCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for index := range queue {
task := taskSummaries[index]
payload := newSpacesTaskPayload(task)
if taskPayload, err := json.Marshal(payload); err == nil {
if _, err := rsm.apiClient.JSONPost(taskURL, taskPayload); err != nil {
errs = append(errs, fmt.Errorf("Error sending %s summary to space: %w", task.TaskID, err))
}
}
}
}()
}
for index := range taskSummaries {
queue <- index
}
close(queue)
wg.Wait()
if len(errs) > 0 {
return errs
}
return nil
}
func getUser(envVars env.EnvironmentVariableMap, dir turbopath.AbsoluteSystemPath) string {
var username string
if ci.IsCi() {
vendor := ci.Info()
username = envVars[vendor.UsernameEnvVar]
}
if username == "" {
username = scm.GetCurrentUser(dir)
}
return username
}