From f198b23e1a697011129f392a97f846e50a25823d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 15 Aug 2023 14:15:45 -0700 Subject: [PATCH] fix: batch status responsiveness (#4661) --- cmd/batch-status.go | 64 +++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/cmd/batch-status.go b/cmd/batch-status.go index 801f166a..923787e5 100644 --- a/cmd/batch-status.go +++ b/cmd/batch-status.go @@ -14,6 +14,7 @@ import ( "github.com/minio/cli" "github.com/minio/madmin-go/v3" "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" "github.com/olekukonko/tablewriter" ) @@ -61,25 +62,42 @@ func mainBatchStatus(ctx *cli.Context) error { defer cancel() _, e := client.DescribeBatchJob(ctxt, jobID) + nosuchJob := madmin.ToErrorResponse(e).Code == "XMinioAdminNoSuchJob" + if nosuchJob { + e = nil + if !globalJSON { + console.Infoln("Unable to find an active job, attempting to list from previously run jobs") + } + } fatalIf(probe.NewError(e), "Unable to lookup job status") ui := tea.NewProgram(initBatchJobMetricsUI(jobID)) go func() { opts := madmin.MetricsOptions{ - Type: madmin.MetricsBatchJobs, - ByJobID: jobID, + Type: madmin.MetricsBatchJobs, + ByJobID: jobID, + Interval: time.Second, } e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) { if globalJSON { - printMsg(metricsMessage{RealtimeMetrics: metrics}) - return - } - if metrics.Aggregated.BatchJobs != nil { - job := metrics.Aggregated.BatchJobs.Jobs[jobID] - ui.Send(job) - if job.Complete { + if metrics.Aggregated.BatchJobs == nil { cancel() + return } + + job, ok := metrics.Aggregated.BatchJobs.Jobs[jobID] + if !ok { + cancel() + return + } + + printMsg(metricsMessage{RealtimeMetrics: metrics}) + if job.Complete || job.Failed { + cancel() + return + } + } else { + ui.Send(metrics) } }) if e != nil && !errors.Is(e, context.Canceled) { @@ -92,6 +110,8 @@ func mainBatchStatus(ctx *cli.Context) error { cancel() fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to get current batch status") } + } else { + <-ctxt.Done() } return nil @@ -128,9 +148,21 @@ func (m *batchJobMetricsUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { default: return m, nil } - case madmin.JobMetric: - m.current = msg - if msg.Complete { + case madmin.RealtimeMetrics: + metrics := msg + if metrics.Aggregated.BatchJobs == nil { + m.quitting = true + return m, tea.Quit + } + + job, ok := metrics.Aggregated.BatchJobs.Jobs[m.jobID] + if !ok { + m.quitting = true + return m, tea.Quit + } + + m.current = job + if job.Complete || job.Failed { m.quitting = true return m, tea.Quit } @@ -173,11 +205,9 @@ func (m *batchJobMetricsUI) View() string { s.WriteString(m.spinner.View()) } else { if m.current.Complete { - if m.current.Replicate.ObjectsFailed == 0 { - s.WriteString(m.spinner.Style.Render((tickCell + tickCell + tickCell))) - } else { - s.WriteString(m.spinner.Style.Render((crossTickCell + crossTickCell + crossTickCell))) - } + s.WriteString(m.spinner.Style.Render((tickCell + tickCell + tickCell))) + } else if m.current.Failed { + s.WriteString(m.spinner.Style.Render((crossTickCell + crossTickCell + crossTickCell))) } } s.WriteString("\n")