Skip to content

Commit

Permalink
refactor: refactor delete using PrettyPrint
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore committed Nov 20, 2023
1 parent 5c347df commit 8a2d305
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions pitr/cli/internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package cmd
import (
"fmt"
"os"
"time"

"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/jedib0t/go-pretty/v6/progress"
"github.com/jedib0t/go-pretty/v6/table"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -152,7 +152,10 @@ func _execDelete(lsBackup *model.LsBackup) error {
return nil
}

pw := prettyoutput.NewPW(totalNum)
pw := prettyoutput.NewProgressPrinter(prettyoutput.ProgressPrintOption{
NumTrackersExpected: totalNum,
})

go pw.Render()

for _, storagenode := range lsBackup.SsBackup.StorageNodes {
Expand All @@ -162,15 +165,26 @@ func _execDelete(lsBackup *model.LsBackup) error {
continue
} else {
as := pkg.NewAgentServer(fmt.Sprintf("%s:%d", convertLocalhost(sn.IP), AgentPort))
go doDelete(as, sn, dn, resultCh, pw)
backupInfo := &model.BackupInfo{
ID: dn.BackupID,
}
task := &deletetask{
As: as,
Sn: sn,
Dn: dn,
ResultCh: resultCh,
Backup: backupInfo,
}

tracker := &progress.Tracker{
Message: fmt.Sprintf("Deleting backup files # %s:%d", sn.IP, sn.Port),
}
pw.AppendTracker(tracker)
go pw.UpdateProgress(tracker, task.checkProgress)
}
}

time.Sleep(time.Millisecond * 100)

for pw.IsRenderInProgress() {
time.Sleep(time.Millisecond * 100)
}
pw.BlockedRendered()

close(resultCh)

Expand Down Expand Up @@ -200,3 +214,44 @@ func _execDelete(lsBackup *model.LsBackup) error {

return nil
}

type deletetask struct {
As pkg.IAgentServer
Sn *model.StorageNode
Dn *model.DataNode
ResultCh chan *model.DeleteBackupResult
Backup *model.BackupInfo
retries int
}

func (t *deletetask) checkProgress() (bool, error) {
var (
err error
)
in := &model.DeleteBackupIn{
DBPort: t.Sn.Port,
DBName: t.Sn.Database,
Username: t.Sn.Username,
Password: t.Sn.Password,
BackupID: t.Backup.ID,
DnBackupPath: BackupPath,
Instance: defaultInstance,
}

r := &model.DeleteBackupResult{
IP: t.Sn.IP,
Port: t.Sn.Port,
}

if err = t.As.DeleteBackup(in); err != nil {
r.Status = model.SsBackupStatusFailed
r.Msg = err.Error()
t.ResultCh <- r
return false, err
}

r.Status = model.SsBackupStatusCompleted
t.ResultCh <- r

return true, nil
}

0 comments on commit 8a2d305

Please sign in to comment.