Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulkar committed Apr 12, 2023
1 parent da395cb commit 047c882
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 77 deletions.
6 changes: 1 addition & 5 deletions cli/internal/run/dry_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ func populateCacheState(turboCache cache.Cache, taskSummaries []*runsummary.Task
for index := range queue {
task := taskSummaries[index]
itemStatus := turboCache.Exists(task.Hash)
task.CacheState = runsummary.TaskCacheSummary{
Local: itemStatus.Local,
Remote: itemStatus.Remote,
// TODO: could add in TimeSaved here if we add it into .Exists()
}
task.CacheSummary = runsummary.NewTaskCacheSummary(itemStatus, nil)
}
}()
}
Expand Down
12 changes: 5 additions & 7 deletions cli/internal/run/real_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ func RealRun(
if taskExecutionSummary != nil {
taskSummary.ExpandedOutputs = taskHashTracker.GetExpandedOutputs(taskSummary.TaskID)
taskSummary.Execution = taskExecutionSummary
cacheDetails := taskHashTracker.GetCacheStatus(taskSummary.TaskID)
taskSummary.CacheState = runsummary.TaskCacheSummary{
Local: cacheDetails.ItemStatus.Local,
Remote: cacheDetails.ItemStatus.Remote,
TimeSaved: cacheDetails.TimeSaved,
}
taskSummary.CacheSummary = taskHashTracker.GetCacheStatus(taskSummary.TaskID)

// lock since multiple things to be appending to this array at the same time
mu.Lock()
Expand Down Expand Up @@ -277,7 +272,10 @@ func (ec *execContext) exec(ctx gocontext.Context, packageTask *nodes.PackageTas
WarnPrefix: prettyPrefix,
}
cacheStatus, timeSaved, err := taskCache.RestoreOutputs(ctx, prefixedUI, progressLogger)
ec.taskHashTracker.SetCacheStatus(packageTask.TaskID, cacheStatus, timeSaved)
ec.taskHashTracker.SetCacheStatus(
packageTask.TaskID,
runsummary.NewTaskCacheSummary(cacheStatus, &timeSaved),
)

hit := cacheStatus.Local || cacheStatus.Remote
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cli/internal/runcache/runcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ func (tc *TaskCache) RestoreOutputs(ctx context.Context, prefixedUI *cli.Prefixe
default:
// NoLogs, do not output anything
}
// TODO: timeSaved could be part of cacheStatus, so we don't have to make a new struct
// downstream, but this would be a more invasive change right now.
return cacheStatus, timeSaved, nil
}

Expand Down
4 changes: 2 additions & 2 deletions cli/internal/runsummary/format_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (rsm Meta) FormatAndPrintText(workspaceInfos workspace.Catalog) error {
fmt.Fprintln(w, util.Sprintf(" ${GREY}Package\t=\t%s\t${RESET}", task.Package))
}
fmt.Fprintln(w, util.Sprintf(" ${GREY}Hash\t=\t%s\t${RESET}", task.Hash))
fmt.Fprintln(w, util.Sprintf(" ${GREY}Cached (Local)\t=\t%s\t${RESET}", strconv.FormatBool(task.CacheState.Local)))
fmt.Fprintln(w, util.Sprintf(" ${GREY}Cached (Remote)\t=\t%s\t${RESET}", strconv.FormatBool(task.CacheState.Remote)))
fmt.Fprintln(w, util.Sprintf(" ${GREY}Cached (Local)\t=\t%s\t${RESET}", strconv.FormatBool(task.CacheSummary.Local)))
fmt.Fprintln(w, util.Sprintf(" ${GREY}Cached (Remote)\t=\t%s\t${RESET}", strconv.FormatBool(task.CacheSummary.Remote)))

if !rsm.singlePackage {
fmt.Fprintln(w, util.Sprintf(" ${GREY}Directory\t=\t%s\t${RESET}", task.Dir))
Expand Down
56 changes: 17 additions & 39 deletions cli/internal/runsummary/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,17 @@ type spacesRunPayload struct {
// gitSha string
}

type spacesCacheStatus struct {
Status string `json:"status,omitempty"`
Source string `json:"source,omitempty"`
TimeSaved int `json:"timeSaved,omitempty"`
}

type spacesTask struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Workspace string `json:"workspace,omitempty"`
Hash string `json:"hash,omitempty"`
StartTime int64 `json:"startTime,omitempty"`
EndTime int64 `json:"endTime,omitempty"`
Cache spacesCacheStatus `json:"cache,omitempty"`
ExitCode int `json:"exitCode,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Dependents []string `json:"dependents,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Workspace string `json:"workspace,omitempty"`
Hash string `json:"hash,omitempty"`
StartTime int64 `json:"startTime,omitempty"`
EndTime int64 `json:"endTime,omitempty"`
Cache TaskCacheSummary `json:"cache,omitempty"`
ExitCode int `json:"exitCode,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Dependents []string `json:"dependents,omitempty"`
}

func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload {
Expand Down Expand Up @@ -85,33 +79,17 @@ func newSpacesDonePayload(runsummary *RunSummary) *spacesRunPayload {
}

func newSpacesTaskPayload(taskSummary *TaskSummary) *spacesTask {
// Set the cache source. Local and Remote shouldn't _both_ be true.
var source string
if taskSummary.CacheState.Local {
source = "LOCAL"
} else if taskSummary.CacheState.Remote {
source = "REMOTE"
}
status := "MISS"
if source != "" {
status = "HIT"
}

startTime := taskSummary.Execution.startAt.UnixMilli()
endTime := taskSummary.Execution.endTime().UnixMilli()

return &spacesTask{
Key: taskSummary.TaskID,
Name: taskSummary.Task,
Workspace: taskSummary.Package,
Hash: taskSummary.Hash,
StartTime: startTime,
EndTime: endTime,
Cache: spacesCacheStatus{
Status: status,
Source: source,
TimeSaved: taskSummary.CacheState.TimeSaved,
},
Key: taskSummary.TaskID,
Name: taskSummary.Task,
Workspace: taskSummary.Package,
Hash: taskSummary.Hash,
StartTime: startTime,
EndTime: endTime,
Cache: taskSummary.CacheSummary,
ExitCode: *taskSummary.Execution.exitCode,
Dependencies: taskSummary.Dependencies,
Dependents: taskSummary.Dependents,
Expand Down
19 changes: 15 additions & 4 deletions cli/internal/runsummary/task_summary.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package runsummary

import (
"github.com/vercel/turbo/cli/internal/cache"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
)

type TaskCacheSummary struct {

Check warning on line 10 in cli/internal/runsummary/task_summary.go

View workflow job for this annotation

GitHub Actions / Go linting

exported: exported type TaskCacheSummary should have comment or be unexported (revive)
Local bool `json:"local"`
Remote bool `json:"remote"`
TimeSaved int `json:"timeSaved"`
Local bool `json:"local"` // Deprecated, but keeping around for --dry=json
Remote bool `json:"remote"` // Deprecated, but keeping around for --dry=json
Status string `json:"status,omitempty"`
Source string `json:"source,omitempty"`
TimeSaved int `json:"timeSaved"`
}

func NewTaskCacheSummary(itemStatus cache.ItemStatus, timeSaved *int) TaskCacheSummary {

Check warning on line 18 in cli/internal/runsummary/task_summary.go

View workflow job for this annotation

GitHub Actions / Go linting

exported: exported function NewTaskCacheSummary should have comment or be unexported (revive)
return TaskCacheSummary{
Local: itemStatus.Local,
Remote: itemStatus.Remote,
TimeSaved: *timeSaved,
}
}

// TaskSummary contains information about the task that was about to run
Expand All @@ -23,7 +34,7 @@ type TaskSummary struct {
Hash string `json:"hash"`
ExpandedInputs map[turbopath.AnchoredUnixPath]string `json:"inputs"`
ExternalDepsHash string `json:"hashOfExternalDependencies"`
CacheState TaskCacheSummary `json:"cache"`
CacheSummary TaskCacheSummary `json:"cache"`
Command string `json:"command"`
CommandArguments []string `json:"cliArguments"`
Outputs []string `json:"outputs"`
Expand Down
31 changes: 11 additions & 20 deletions cli/internal/taskhash/taskhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/pyr-sh/dag"
gitignore "github.com/sabhiram/go-gitignore"
"github.com/vercel/turbo/cli/internal/cache"
"github.com/vercel/turbo/cli/internal/doublestar"
"github.com/vercel/turbo/cli/internal/env"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/hashing"
"github.com/vercel/turbo/cli/internal/inference"
"github.com/vercel/turbo/cli/internal/nodes"
"github.com/vercel/turbo/cli/internal/runsummary"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
"github.com/vercel/turbo/cli/internal/workspace"
Expand Down Expand Up @@ -47,7 +47,7 @@ type Tracker struct {
packageTaskHashes map[string]string // taskID -> hash
packageTaskFramework map[string]string // taskID -> inferred framework for package
packageTaskOutputs map[string][]turbopath.AnchoredSystemPath
packageTaskCacheStatus map[string]CacheDetails
packageTaskCacheStatus map[string]runsummary.TaskCacheSummary
}

// NewTracker creates a tracker for package-inputs combinations and package-task combinations.
Expand All @@ -60,7 +60,7 @@ func NewTracker(rootNode string, globalHash string, pipeline fs.Pipeline) *Track
packageTaskFramework: make(map[string]string),
packageTaskEnvVars: make(map[string]env.DetailedMap),
packageTaskOutputs: make(map[string][]turbopath.AnchoredSystemPath),
packageTaskCacheStatus: make(map[string]CacheDetails),
packageTaskCacheStatus: make(map[string]runsummary.TaskCacheSummary),
}
}

Expand Down Expand Up @@ -426,30 +426,21 @@ func (th *Tracker) SetExpandedOutputs(taskID string, outputs []turbopath.Anchore
}

// SetCacheStatus records the task status for the given taskID
func (th *Tracker) SetCacheStatus(taskID string, cacheStatus cache.ItemStatus, timeSaved int) {
func (th *Tracker) SetCacheStatus(taskID string, cacheSummary runsummary.TaskCacheSummary) {
th.mu.Lock()
defer th.mu.Unlock()
th.packageTaskCacheStatus[taskID] = CacheDetails{
ItemStatus: cacheStatus,
TimeSaved: timeSaved,
}
th.packageTaskCacheStatus[taskID] = cacheSummary
}

// GetCacheStatus records the task status for the given taskID
func (th *Tracker) GetCacheStatus(taskID string) CacheDetails {
func (th *Tracker) GetCacheStatus(taskID string) runsummary.TaskCacheSummary {
th.mu.Lock()
defer th.mu.Unlock()
status, ok := th.packageTaskCacheStatus[taskID]
if !ok {
return CacheDetails{
ItemStatus: cache.ItemStatus{Local: false, Remote: false},
TimeSaved: 0,
}

if status, ok := th.packageTaskCacheStatus[taskID]; ok {
return status
}
return status
}

type CacheDetails struct {
ItemStatus cache.ItemStatus
TimeSaved int
// Return an empty one, all the fields will be false and 0
return runsummary.TaskCacheSummary{}
}

0 comments on commit 047c882

Please sign in to comment.