-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (146 loc) · 3.92 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"os"
"time"
"scraper/internal/api"
"scraper/internal/cli"
"scraper/internal/log"
"scraper/internal/scheduler"
"scraper/internal/scraper"
"scraper/internal/utils"
"github.com/go-co-op/gocron"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/spf13/cobra"
)
func CreateCommand(ctx context.Context) *cobra.Command {
cliFlags := cli.CliFlags{}
copts := &cli.CliOptions{}
cmd := &cobra.Command{
Use: "scraper [CMD]",
Short: "Scraper command line utility.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
for _, opt := range []cli.CliOption{
cli.WithDefaultLogger(cliFlags.LogLevel),
} {
if err := opt(copts); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
ctx = log.WithLogger(ctx, copts.Logger)
cmd.SetContext(ctx)
},
}
cmd.PersistentFlags().StringVar(&cliFlags.LogLevel, "log-level", "debug", "Specify log level {debug,warn,info}")
return cmd
}
const DB_MAX_CONN = 32
// Cron jobs, enqueue rabbitmq items
func CreateSheduler() *cobra.Command {
flags := scheduler.SchedulerFlags{}
cmd := &cobra.Command{
Use: "scheduler",
Short: "runs the scheduler",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
dbPool, err := utils.NewDBPool(ctx, DB_MAX_CONN)
utils.Fail(ctx, err, "Unable to connect to database")
defer dbPool.Close()
rmqConn, err := amqp.Dial(os.Getenv("RMQ_URL"))
utils.Fail(ctx, err, "Unable to connect to rabbitmq")
defer rmqConn.Close()
ch, err := rmqConn.Channel()
utils.Fail(ctx, err, "Unable to make channel")
defer ch.Close()
_, err = ch.QueueDeclare(
"job_queue",
true,
false,
false,
false,
nil,
)
utils.Fail(ctx, err, "Unable to open queue")
cron := gocron.NewScheduler(time.UTC)
scheduler := scheduler.NewScheduler(
ctx,
scheduler.SchedulerConfig{
DbPool: dbPool,
Ch: ch,
Flags: flags,
},
)
err = scheduler.DbPool.Ping(ctx)
utils.Fail(ctx, err, "Ping error")
log.FromContext(ctx).Debugf("Starting cron jobs")
cron.Every(1).Seconds().Do(scheduler.Run, ctx)
cron.StartBlocking()
},
}
cmd.PersistentFlags().BoolVar(&flags.NoResetTimerDB, "noreset", false, "do not reset scraping timers")
cmd.PersistentFlags().BoolVar(&flags.CreateTables, "create-tables", false, "create tables before running")
return cmd
}
// API server
func CreateAPIServer() *cobra.Command {
cmd := &cobra.Command{
Use: "api",
Short: "runs the api server",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
dbPool, err := utils.NewDBPool(ctx, DB_MAX_CONN)
utils.Fail(ctx, err, "Unable to connect to database")
defer dbPool.Close()
api.StartServer(api.APIServerConfig{
DbPool: dbPool,
})
return nil
},
}
return cmd
}
const (
WORKER_COUNT = 2
MAX_CONCURRENT_CONNECTIONS = 10
//REDIS_POOL_SIZE = 16
)
// Scraper worker
func CreateScraper() *cobra.Command {
cmd := &cobra.Command{
Use: "scraper",
Short: "runs the scraper worker",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
rmqURL := os.Getenv("RMQ_URL")
dbPool, err := utils.NewDBPool(ctx, DB_MAX_CONN)
utils.Fail(ctx, err, "Unable to connect to database")
defer dbPool.Close()
conn, err := amqp.Dial(rmqURL)
utils.Fail(ctx, err, "Dial error on worker side")
defer conn.Close()
ch, err := conn.Channel()
utils.Fail(ctx, err, "Failure when trying to open a RMQ Channel")
defer ch.Close()
ch.Qos(MAX_CONCURRENT_CONNECTIONS, 0, false)
conf := scraper.WorkerConfig{
Ch: ch,
DbPool: dbPool,
}
for workerID := 0; workerID < WORKER_COUNT; workerID++ {
go conf.Work(ctx, workerID)
}
select {}
},
}
return cmd
}
func main() {
ctx := context.Background()
cmd := CreateCommand(ctx)
cmd.AddCommand(CreateSheduler())
cmd.AddCommand(CreateScraper())
cmd.AddCommand(CreateAPIServer())
cmd.Execute()
}