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

BUG:Not all cached data was deleted by cleanup command #733

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
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
54 changes: 54 additions & 0 deletions cmd/qng/commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/Qitmeer/qng/common/system"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/consensus"
Expand All @@ -9,9 +10,11 @@ import (
"github.com/Qitmeer/qng/meerevm/amana"
"github.com/Qitmeer/qng/meerevm/cmd"
"github.com/Qitmeer/qng/meerevm/meer"
"github.com/Qitmeer/qng/p2p"
"github.com/Qitmeer/qng/services/common"
"github.com/Qitmeer/qng/version"
"github.com/urfave/cli/v2"
"os"
"runtime"
)

Expand All @@ -22,6 +25,7 @@ func commands() []*cli.Command {
cmds = append(cmds, blockchainCmd())
cmds = append(cmds, cmd.Commands...)
cmds = append(cmds, dbCmd())
cmds = append(cmds, cleanupCmd())

for _, cmd := range cmds {
cmd.Before = loadConfig
Expand Down Expand Up @@ -128,6 +132,56 @@ func consensusCmd() *cli.Command {
}
}

func cleanupCmd() *cli.Command {
removeAll := false
return &cli.Command{
Name: "cleanup",
Aliases: []string{"cl"},
Usage: "Delete all status data, including cache",
Description: "Delete all status data, including cache",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "Delete all data including blockchain data, peerstore, logs",
Destination: &removeAll,
},
},
Action: func(ctx *cli.Context) error {
cfg := config.Cfg
cfg.Cleanup = true
defer func() {
if log.LogWrite() != nil {
log.LogWrite().Close()
}
}()
interrupt := system.InterruptListener()
log.Info("System info", "QNG Version", version.String(), "Go version", runtime.Version())
log.Info("System info", "Home dir", cfg.HomeDir)
if cfg.NoFileLogging {
log.Info("File logging disabled")
}
_, err := database.New(cfg, interrupt)
if err != nil {
log.Error("load block database", "error", err)
return err
}
if !removeAll {
return nil
}
p2p.Cleanup(cfg.DataDir)
if len(cfg.LogDir) > 0 {
err = os.RemoveAll(cfg.LogDir)
if err != nil {
log.Error(err.Error())
}
log.Info(fmt.Sprintf("Finished cleanup:%s", cfg.LogDir))
}
return nil
},
}
}

func loadConfig(ctx *cli.Context) error {
_, err := common.LoadConfig(ctx, false)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions p2p/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ func ProtocolDHT() protocol.ID {
return "/qitmeer/kad/1.0.0/privnet"
}
}

func Cleanup(dataDir string) {
dsPath := path.Join(dataDir, PeerStore)
err := os.RemoveAll(dsPath)
if err != nil {
log.Error(err.Error())
}
log.Info(fmt.Sprintf("Finished cleanup:%s", dsPath))
}
Loading