-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (129 loc) · 3.5 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"strings"
"sync"
"github.com/DevAlone/parse_pikabu/amqphelper"
"github.com/DevAlone/parse_pikabu/core"
"github.com/DevAlone/parse_pikabu/core/config"
"github.com/DevAlone/parse_pikabu/core/logger"
"github.com/DevAlone/parse_pikabu/core/server/middlewares"
"github.com/DevAlone/parse_pikabu/globals"
"github.com/DevAlone/parse_pikabu/helpers"
"github.com/DevAlone/parse_pikabu/models"
"github.com/DevAlone/parse_pikabu/parser"
"github.com/go-errors/errors"
"github.com/go-pg/pg/orm"
"github.com/pkg/profile"
)
var commands = map[string]func(){
"single_process_mode": func() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
core.Main()
wg.Done()
}()
wg.Add(1)
go func() {
parser.Main()
wg.Done()
}()
wg.Wait()
},
"core": func() {
core.Main()
},
"parser": func() {
// TODO: modify to fetch tasks from amqp or something
parser.Main()
},
"clean_db": func() {
logger.Init()
err := models.InitDb()
helpers.PanicOnError(err)
// clear tables
for _, table := range models.Tables {
err := models.Db.DropTable(table, &orm.DropTableOptions{
IfExists: true,
Cascade: true,
})
helpers.PanicOnError(err)
}
},
"print_index_queries": func() {
logger.Init()
queries := models.GetIndexQueries()
for _, query := range queries {
fmt.Println(query)
}
},
"add_parser": func() {
redisClient := helpers.GetRedisClient()
if len(os.Args) < 2 {
helpers.PanicOnError(errors.New("too few arguments"))
}
key := "parse_pikabu_server_authentication_middleware_session_group_" + strings.TrimSpace(os.Args[1])
err := redisClient.Set(key, fmt.Sprint(middlewares.GROUP_PARSER), 0).Err()
helpers.PanicOnError(err)
},
"add_parsers_from_config": func() {
err := addParsersFromConfig()
helpers.PanicOnError(err)
},
"fix_usernames": func() {
fixUsernames()
},
}
func main() {
var err error
if len(os.Args) < 2 {
var commandsList string
for command := range commands {
commandsList += "\t-" + command + "\n"
}
_, err = os.Stderr.WriteString(fmt.Sprintf(`Please, specify a command.
Available commands are:
%s
`, commandsList))
helpers.PanicOnError(err)
return
}
command := strings.TrimSpace(os.Args[1])
os.Args = os.Args[1:]
configFilePath := flag.String("config", "core.config.json", "config file")
cpuProfile := flag.String("cpuprofile", "", "set to true to profile cpu")
memProfile := flag.String("memprofile", "", "set to true to profile memory")
doNotParseUsersFlag := flag.String("do-not-parse-users", "false", "do not parse users")
doNotParseStoriesFlag := flag.String("do-not-parse-stories", "false", "do not parse stories")
flag.Parse()
globals.DoNotParseUsers = strings.HasPrefix(strings.ToLower(*doNotParseUsersFlag), "t")
globals.DoNotParseStories = strings.HasPrefix(strings.ToLower(*doNotParseStoriesFlag), "t")
if configFilePath == nil || len(*configFilePath) == 0 {
panic(errors.New("configFilePath is nil"))
}
if *cpuProfile != "" {
f, err := os.Create("cpu.pprof")
helpers.PanicOnError(err)
err = pprof.StartCPUProfile(f)
helpers.PanicOnError(err)
defer pprof.StopCPUProfile()
}
if *memProfile != "" {
p := profile.Start(profile.MemProfile)
defer p.Stop()
}
err = config.UpdateSettingsFromFile(*configFilePath)
helpers.PanicOnError(err)
err = globals.Init()
helpers.PanicOnError(err)
if handler, found := commands[command]; found {
handler()
} else {
helpers.PanicOnError(errors.Errorf("wrong command"))
}
helpers.PanicOnError(amqphelper.Cleanup())
}