Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions commands/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"encoding/json"
"fmt"
"time"

Expand All @@ -16,6 +17,11 @@ import (
"github.com/filecoin-project/lily/lens/lily"
)

type SyncStatus struct {
Stage api.SyncStateStage
Height abi.ChainEpoch
}

var SyncCmd = &cli.Command{
Name: "sync",
Usage: "Inspect or interact with the chain syncer",
Expand All @@ -28,6 +34,14 @@ var SyncCmd = &cli.Command{
var SyncStatusCmd = &cli.Command{
Name: "status",
Usage: "Report sync status of a running lily daemon",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Usage: "Print only the current sync stage at the latest height. One of [text, json]",
Aliases: []string{"o"},
Required: false,
},
},
Action: func(cctx *cli.Context) error {
ctx := lotuscli.ReqContext(cctx)
lapi, closer, err := GetAPI(ctx)
Expand All @@ -41,9 +55,16 @@ var SyncStatusCmd = &cli.Command{
return err
}

fmt.Println("sync status:")
output := cctx.String("output")

var max abi.ChainEpoch = -1
maxStateSync := api.StageIdle
for _, ss := range state.ActiveSyncs {
fmt.Printf("worker %d:\n", ss.WorkerID)
if max < ss.Height && maxStateSync <= ss.Stage {
max = ss.Height
maxStateSync = ss.Stage
}

var base, target []cid.Cid
var heightDiff int64
var theight abi.ChainEpoch
Expand All @@ -58,21 +79,38 @@ var SyncStatusCmd = &cli.Command{
} else {
heightDiff = 0
}
fmt.Printf("\tBase:\t%s\n", base)
fmt.Printf("\tTarget:\t%s (%d)\n", target, theight)
fmt.Printf("\tHeight diff:\t%d\n", heightDiff)
fmt.Printf("\tStage: %s\n", ss.Stage)
fmt.Printf("\tHeight: %d\n", ss.Height)
if ss.End.IsZero() {
if !ss.Start.IsZero() {
fmt.Printf("\tElapsed: %s\n", time.Since(ss.Start))

switch output {
case "json":
j, err := json.Marshal(SyncStatus{Stage: maxStateSync, Height: max})
if err != nil {
return err
}
} else {
fmt.Printf("\tElapsed: %s\n", ss.End.Sub(ss.Start))
fmt.Printf(string(j) + "\n")
case "":
Copy link
Member

Choose a reason for hiding this comment

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

could call this "pretty" or "verbose" and set the default value of the --output flag as such, but not a big deal

fmt.Printf("worker %d:\n", ss.WorkerID)
fmt.Printf("\tBase:\t%s\n", base)
fmt.Printf("\tTarget:\t%s (%d)\n", target, theight)
fmt.Printf("\tHeight diff:\t%d\n", heightDiff)
fmt.Printf("\tStage: %s\n", ss.Stage)
fmt.Printf("\tHeight: %d\n", ss.Height)
if ss.End.IsZero() {
if !ss.Start.IsZero() {
fmt.Printf("\tElapsed: %s\n", time.Since(ss.Start))
}
} else {
fmt.Printf("\tElapsed: %s\n", ss.End.Sub(ss.Start))
}
case "text":
fallthrough
default:
fmt.Printf("%s %d\n", maxStateSync, max)
}
if ss.Stage == api.StageSyncErrored {

if ss.Stage == api.StageSyncErrored && output != "json" {
fmt.Printf("\tError: %s\n", ss.Message)
}

}
return nil
},
Expand Down