-
Notifications
You must be signed in to change notification settings - Fork 6
/
sync_all.go
119 lines (111 loc) · 3.28 KB
/
sync_all.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2019 The fsn-go-sdk Authors
// This file is part of the fsn-go-sdk library.
//
// The fsn-go-sdk library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The fsn-go-sdk library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the fsn-go-sdk library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"github.com/fsn-dev/fsn-go-sdk/efsn/cmd/utils"
"github.com/fsn-dev/fsn-go-sdk/efsn/log"
"github.com/fsn-dev/fsn-go-sdk/mongosync/syncer"
"gopkg.in/urfave/cli.v1"
)
var (
mongoURLFlag = cli.StringFlag{
Name: "mongoURL",
Usage: "mongodb URL",
Value: "localhost:27017",
}
dbNameFlag = cli.StringFlag{
Name: "dbName",
Usage: "database name",
Value: "fusion",
}
dbUserFlag = cli.StringFlag{
Name: "dbUser",
Usage: "database user name",
}
dbPassFlag = cli.StringFlag{
Name: "dbPass",
Usage: "database password",
}
stableFlag = cli.Uint64Flag{
Name: "stable",
Usage: "sync blocks that is stable lower than the latest",
Value: 10,
}
startFlag = cli.Uint64Flag{
Name: "start",
Usage: "sync blocks from this height",
Value: 0,
}
endFlag = cli.Uint64Flag{
Name: "end",
Usage: "sync blocks to this height (not inclusive)",
Value: 0,
}
overwriteFlag = cli.BoolFlag{
Name: "overwrite",
Usage: "overwrite synced entries",
}
archiveModeFlag = cli.BoolTFlag{
Name: "archive",
Usage: "specify whether the server node is using archive mode. If not we may not get retreated tickets info etc.",
}
)
var commandSyncAll = cli.Command{
Name: "syncAll",
Usage: "sync all info into mongodb",
ArgsUsage: "<severURL>",
Description: `
Sync all info into mongodb.
severURL support "http", "https", "ws", "wss", "stdio", IPC file`,
Flags: []cli.Flag{
mongoURLFlag,
dbNameFlag,
dbUserFlag,
dbPassFlag,
stableFlag,
startFlag,
endFlag,
overwriteFlag,
archiveModeFlag,
},
Action: syncAll,
}
func syncAll(ctx *cli.Context) error {
log.SetLogger(ctx.GlobalInt(utils.VerbosityFlag.Name), ctx.GlobalBool(utils.JsonFlag.Name))
if len(ctx.Args()) == 0 {
cli.ShowCommandHelpAndExit(ctx, "syncAll", 1)
}
serverAddress := ctx.Args().First()
mongoURL := ctx.String(mongoURLFlag.Name)
dbName := ctx.String(dbNameFlag.Name)
dbUser := ctx.String(dbUserFlag.Name)
dbPass := ctx.String(dbPassFlag.Name)
stable := ctx.Uint64(stableFlag.Name)
start := ctx.Uint64(startFlag.Name)
end := ctx.Uint64(endFlag.Name)
overwrite := ctx.Bool(overwriteFlag.Name)
archiveMode := ctx.Bool(archiveModeFlag.Name)
jobs := ctx.GlobalUint64(jobsFlag.Name)
interval := ctx.GlobalUint64(intervalFlag.Name)
syncer.ServerURL = serverAddress
syncer.Overwrite = overwrite
syncer.ArchiveMode = archiveMode
syncer.BlockInterval = interval
syncer.SetJobCount(jobs)
syncer.InitMongoServer(mongoURL, dbName, dbUser, dbPass)
syncer.NewSyncer(stable, start, end).Sync()
return nil
}