Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add log message about remote caching #2122

Merged
22 changes: 22 additions & 0 deletions cli/internal/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ func NewHelper(turboVersion string) *Helper {
// GetCmdBase returns a CmdBase instance configured with values from this helper.
// It additionally returns a mechanism to set an error, so
func (h *Helper) GetCmdBase(flags *pflag.FlagSet) (*CmdBase, error) {
// terminal is for color/no-color output
terminal := h.getUI(flags)

// logger is configured with verbosity level using --verbosity flag from end users
logger, err := h.getLogger()

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -186,6 +190,7 @@ func (h *Helper) GetCmdBase(flags *pflag.FlagSet) (*CmdBase, error) {
h.TurboVersion,
h.clientOpts,
)

return &CmdBase{
UI: terminal,
Logger: logger,
Expand Down Expand Up @@ -216,3 +221,20 @@ func (b *CmdBase) LogError(format string, args ...interface{}) {
b.Logger.Error("error", err)
b.UI.Error(fmt.Sprintf("%s%s", ui.ERROR_PREFIX, color.RedString(" %v", err)))
}

// LogWarning logs an error and outputs it to the UI.
func (b *CmdBase) LogWarning(prefix string, err error) {
b.Logger.Warn(prefix, "warning", err)

if prefix != "" {
prefix = " " + prefix + ": "
}

b.UI.Error(fmt.Sprintf("%s%s%s", ui.WARNING_PREFIX, prefix, color.YellowString(" %v", err)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mehulkar Can you help me understand why this LogWarning method is using this ... unorthodox ... pair?

  • b.Logger.Warn
  • b.UI.Error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! It's because I'm a dumdum. #2135

}

// LogInfo logs an message and outputs it to the UI.
func (b *CmdBase) LogInfo(msg string) {
b.Logger.Info(msg)
b.UI.Info(fmt.Sprintf("%s%s", ui.INFO_PREFIX, color.WhiteString(" %v", msg)))
}
31 changes: 15 additions & 16 deletions cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Arguments passed after '--' will be passed through to the named tasks.
func GetCmd(helper *cmdutil.Helper, signalWatcher *signals.Watcher) *cobra.Command {
var opts *Opts
var flags *pflag.FlagSet

cmd := &cobra.Command{
Use: "run <task> [...<task>] [<flags>] -- <args passed to tasks>",
Short: "Run tasks across projects in your monorepo",
Expand All @@ -119,6 +120,7 @@ func GetCmd(helper *cmdutil.Helper, signalWatcher *signals.Watcher) *cobra.Comma
return nil
},
}

flags = cmd.Flags()
opts = optsFromFlags(flags)
return cmd
Expand Down Expand Up @@ -200,7 +202,7 @@ func (r *run) run(ctx gocontext.Context, targets []string) error {
} else if !r.opts.runOpts.noDaemon {
turbodClient, err := daemon.GetClient(ctx, r.base.RepoRoot, r.base.Logger, r.base.TurboVersion, daemon.ClientOpts{})
if err != nil {
r.logWarning("", errors.Wrap(err, "failed to contact turbod. Continuing in standalone mode"))
r.base.LogWarning("", errors.Wrap(err, "failed to contact turbod. Continuing in standalone mode"))
} else {
defer func() { _ = turbodClient.Close() }()
r.base.Logger.Debug("running in daemon mode")
Expand All @@ -221,7 +223,7 @@ func (r *run) run(ctx gocontext.Context, targets []string) error {
scmInstance, err := scm.FromInRepo(r.base.RepoRoot.ToStringDuringMigration())
if err != nil {
if errors.Is(err, scm.ErrFallback) {
r.logWarning("", err)
r.base.LogWarning("", err)
} else {
return errors.Wrap(err, "failed to create SCM")
}
Expand Down Expand Up @@ -699,17 +701,6 @@ func getDefaultOptions() *Opts {
}
}

// logError logs an error and outputs it to the UI.
func (r *run) logWarning(prefix string, err error) {
r.base.Logger.Warn(prefix, "warning", err)

if prefix != "" {
prefix = " " + prefix + ": "
}

r.base.UI.Error(fmt.Sprintf("%s%s%s", ui.WARNING_PREFIX, prefix, color.YellowString(" %v", err)))
}

func (r *run) initAnalyticsClient(ctx gocontext.Context) analytics.Client {
apiClient := r.base.APIClient
var analyticsSink analytics.Sink
Expand All @@ -727,12 +718,13 @@ func (r *run) initCache(ctx gocontext.Context, rs *runSpec, analyticsClient anal
apiClient := r.base.APIClient
// Theoretically this is overkill, but bias towards not spamming the console
once := &sync.Once{}

return cache.New(rs.Opts.cacheOpts, r.base.RepoRoot, apiClient, analyticsClient, func(_cache cache.Cache, err error) {
// Currently the HTTP Cache is the only one that can be disabled.
// With a cache system refactor, we might consider giving names to the caches so
// we can accurately report them here.
once.Do(func() {
r.logWarning("Remote Caching is unavailable", err)
r.base.LogWarning("Remote Caching is unavailable", err)
})
})
}
Expand All @@ -741,10 +733,17 @@ func (r *run) executeTasks(ctx gocontext.Context, g *completeGraph, rs *runSpec,
analyticsClient := r.initAnalyticsClient(ctx)
defer analyticsClient.CloseWithTimeout(50 * time.Millisecond)

useHTTPCache := !rs.Opts.cacheOpts.SkipRemote
if useHTTPCache {
r.base.LogInfo("• Remote caching enabled")
} else {
r.base.LogInfo("• Remote caching disabled")
}

turboCache, err := r.initCache(ctx, rs, analyticsClient)
if err != nil {
if errors.Is(err, cache.ErrNoCachesEnabled) {
r.logWarning("No caches are enabled. You can try \"turbo login\", \"turbo link\", or ensuring you are not passing --remote-only to enable caching", nil)
r.base.LogWarning("No caches are enabled. You can try \"turbo login\", \"turbo link\", or ensuring you are not passing --remote-only to enable caching", nil)
} else {
return errors.Wrap(err, "failed to set up caching")
}
Expand Down Expand Up @@ -856,7 +855,7 @@ func (r *run) executeDryRun(ctx gocontext.Context, engine *core.Scheduler, g *co

if err != nil {
if errors.Is(err, cache.ErrNoCachesEnabled) {
r.logWarning("No caches are enabled. You can try \"turbo login\", \"turbo link\", or ensuring you are not passing --remote-only to enable caching", nil)
r.base.LogWarning("No caches are enabled. You can try \"turbo login\", \"turbo link\", or ensuring you are not passing --remote-only to enable caching", nil)
} else {
return nil, errors.Wrap(err, "failed to set up caching")
}
Expand Down
1 change: 1 addition & 0 deletions cli/internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var gray = color.New(color.Faint)
var bold = color.New(color.Bold)
var ERROR_PREFIX = color.New(color.Bold, color.FgRed, color.ReverseVideo).Sprint(" ERROR ")
var WARNING_PREFIX = color.New(color.Bold, color.FgYellow, color.ReverseVideo).Sprint(" WARNING ")
var INFO_PREFIX = color.New(color.Bold, color.FgWhite, color.ReverseVideo).Sprint(" INFO ")

var ansiRegex = regexp.MustCompile(ansiEscapeStr)

Expand Down