-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
293 lines (260 loc) · 11.4 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"context"
stdlog "log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"time"
"github.com/alextanhongpin/go-github-scraper/internal/app/mediatorsvc"
"github.com/alextanhongpin/go-github-scraper/internal/app/reposvc"
"github.com/alextanhongpin/go-github-scraper/internal/app/statsvc"
"github.com/alextanhongpin/go-github-scraper/internal/app/transport"
"github.com/alextanhongpin/go-github-scraper/internal/app/usersvc"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/client/github"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/cronjob"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/database"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/logger"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/null"
"github.com/alextanhongpin/go-github-scraper/internal/pkg/profiler"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
"github.com/spf13/viper"
"go.opencensus.io/exporter/jaeger"
"go.opencensus.io/trace"
)
func init() {
viper.AutomaticEnv()
viper.SetDefault("version", "0.0.1") // The application version, normally the git hash
viper.SetDefault("crontab_user_tab", "*/20 * * * * *") // The crontab for user, running every 20 seconds
viper.SetDefault("reset_repo", false) // Whether to fetch it from scratch or not
viper.SetDefault("crontab_repo_tab", "0 * * * * *") // The crontab for repo, running every minute
viper.SetDefault("crontab_stat_tab", "0 10 0 * * *") // The crontab for stat, running ten minutes after midnight
viper.SetDefault("crontab_profile_tab", "@midnight") // The crontab for profile, running at midnight
viper.SetDefault("crontab_match_tab", "0 15 0 * * *") // The crontab for matching, running fifteen minutes after midnight
viper.SetDefault("crontab_user_enable", false) // The enable state of the crontab for user
viper.SetDefault("crontab_repo_enable", false) // The enable state of the crontab for repo
viper.SetDefault("crontab_stat_enable", false) // The enable state of the crontab for stat
viper.SetDefault("crontab_profile_enable", false) // The enable state of the crontab for profile
viper.SetDefault("crontab_match_enable", false) // The enable state of the crontab for profile
viper.SetDefault("crontab_user_trigger", false) // Will run once if set to true
viper.SetDefault("crontab_repo_trigger", false) // Will run once if set to true
viper.SetDefault("crontab_stat_trigger", false) // Will run once if set to true
viper.SetDefault("crontab_profile_trigger", false) // Will run once if set to true
viper.SetDefault("crontab_match_trigger", false) // Will run once if set to true
viper.SetDefault("db_user", "root") // The username of the database
viper.SetDefault("db_pass", "example") // The password of the database
viper.SetDefault("db_name", "scraper") // The name of the database
viper.SetDefault("db_auth", "admin") // The name of the auth database
viper.SetDefault("db_host", "mongodb://localhost:27017") // The URI of the database
viper.SetDefault("github_location", "Malaysia") // The default country to scrape data from
viper.SetDefault("github_token", "") // The Github's access token used to make call to the GraphQL Endpoint
viper.SetDefault("github_uri", "https://api.github.com/graphql") // The Github's GraphQL Endpoint
viper.SetDefault("port", ":8080") // The TCP port of the application
viper.SetDefault("pprof_port", ":6060") // The TCP port of for the http profiling
viper.SetDefault("pprof_enable", false) // Toggle flag for pprof
viper.SetDefault("cpuprofile", "") // Write cpuprofile to file, e.g. cpu.prof
viper.SetDefault("memprofile", "") // Write memoryprofile to file, e.g. mem.prof
viper.SetDefault("httpprofile", false) // Toggle state for http profiler
viper.SetDefault("graceful_timeout", 15) // The duration for which the server gracefully wait for existing connections to finish
viper.SetDefault("trace_endpoint", "http://localhost:14268") // The endpoint of the jaeger image
viper.SetDefault("trace_service", "go-scraper") // The name of the service that appears in the dashboard
if viper.GetString("github_token") == "" {
panic("github_token environment variable is missing")
}
}
func main() {
// Create global context for cancellation
ctx := context.Background()
// Setup cpu profiler
profiler.MakeCPU(viper.GetString("cpuprofile"))
// Setup http client
httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 20,
IdleConnTimeout: time.Second * 5,
},
Timeout: time.Second * 5,
}
// Setup jaeger
exporter, err := jaeger.NewExporter(jaeger.Options{
Endpoint: viper.GetString("trace_endpoint"),
ServiceName: viper.GetString("trace_service"),
})
if err != nil {
stdlog.Fatal(err)
}
trace.RegisterExporter(exporter)
// For demoing purpose, always sample
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
// Setup logger
l := logger.New()
defer l.Sync()
// Setup database
db := database.New(
viper.GetString("db_host"),
viper.GetString("db_user"),
viper.GetString("db_pass"),
viper.GetString("db_name"),
viper.GetString("db_auth"))
defer db.Close()
// Setup services
m := mediatorsvc.Mediator{
Stat: statsvc.New(db,
statsvc.Logging(l.Named("statsvc")),
statsvc.Tracing()),
Github: github.New(httpClient,
viper.GetString("github_token"),
viper.GetString("github_uri"),
github.Logging(l.Named("github")),
github.Tracing()),
Repo: reposvc.New(db,
reposvc.Logging(l.Named("reposvc")),
reposvc.Tracing()),
User: usersvc.New(db,
usersvc.Logging(l.Named("usersvc")),
usersvc.Tracing()),
}
// Setup mediator services, which is basically an orchestration of multiple services
msvc := mediatorsvc.New(
m,
mediatorsvc.Logging(l.Named("mediatorsvc")),
mediatorsvc.Tracing())
// Setup cronjob
cronjob.Exec(ctx,
&cronjob.Config{
Name: "Fetch Users",
Description: "Fetch the Github users data periodically based on location and created date, which is stored as delta timestamp",
Start: viper.GetBool("crontab_user_enable"),
CronTab: viper.GetString("crontab_user_tab"),
Trigger: viper.GetBool("crontab_user_trigger"),
Fn: func(ctx context.Context) error {
ctx = logger.WrapContextWithRequestID(ctx)
location := "Malaysia"
months := 6
perPage := 30
return msvc.FetchUsers(ctx, location, months, perPage)
},
},
&cronjob.Config{
Name: "Fetch Repos",
Description: "Fetch the Github user's repos periodically based on the last fetched date",
Start: viper.GetBool("crontab_repo_enable"),
CronTab: viper.GetString("crontab_repo_tab"),
Trigger: viper.GetBool("crontab_repo_trigger"),
Fn: func(ctx context.Context) error {
ctx = logger.WrapContextWithRequestID(ctx)
userPerPage := 100
repoPerPage := 30
return msvc.FetchRepos(ctx, userPerPage, repoPerPage, viper.GetBool("reset_repo"))
},
},
&cronjob.Config{
Name: "Update Profile",
Description: "Compute the new user profile based on the repos that are scraped daily",
Start: viper.GetBool("crontab_profile_enable"),
CronTab: viper.GetString("crontab_profile_tab"),
Trigger: viper.GetBool("crontab_profile_trigger"),
Fn: func(ctx context.Context) error {
ctx = logger.WrapContextWithRequestID(ctx)
numWorkers := 4
return msvc.UpdateProfile(ctx, numWorkers)
},
},
&cronjob.Config{
Name: "Build Stats",
Description: "Compute the Github's analytic data of users in Malaysia based on the new repos that are scraped daily",
Start: viper.GetBool("crontab_stat_enable"),
CronTab: viper.GetString("crontab_stat_tab"),
Trigger: viper.GetBool("crontab_stat_trigger"),
Fn: func(ctx context.Context) error {
ctx = logger.WrapContextWithRequestID(ctx)
defaultLimit := 20
min := 3
max := 100
nullFns := []null.Fn{
func() error { return msvc.UpdateUserCount(ctx) },
func() error { return msvc.UpdateRepoCount(ctx) },
func() error { return msvc.UpdateReposMostRecent(ctx, defaultLimit) },
func() error { return msvc.UpdateRepoCountByUser(ctx, defaultLimit) },
func() error { return msvc.UpdateReposMostStars(ctx, defaultLimit) },
func() error { return msvc.UpdateReposMostForks(ctx, defaultLimit) },
func() error { return msvc.UpdateLanguagesMostPopular(ctx, defaultLimit) },
func() error { return msvc.UpdateMostRecentReposByLanguage(ctx, defaultLimit) },
func() error { return msvc.UpdateReposByLanguage(ctx, defaultLimit) },
func() error { return msvc.UpdateCompanyCount(ctx) },
func() error { return msvc.UpdateUsersByCompany(ctx, min, max) },
}
var wg sync.WaitGroup
wg.Add(len(nullFns))
for _, fn := range nullFns {
go func(f null.Fn) {
defer wg.Done()
f()
}(fn)
}
wg.Wait()
return nil
},
},
&cronjob.Config{
Name: "Update Matches",
Description: "Compute the new user recommendations based on the new repos pulled",
Start: viper.GetBool("crontab_match_enable"),
CronTab: viper.GetString("crontab_match_tab"),
Trigger: viper.GetBool("crontab_match_trigger"),
Fn: func(ctx context.Context) error {
ctx = logger.WrapContextWithRequestID(ctx)
return msvc.UpdateMatches(ctx)
},
},
)
// Setup router
r := httprouter.New()
// Setup endpoints, can also add feature toggle capabilities
tr := transport.New(r)
tr.Init(
transport.NewUserEndpoints(m.User),
transport.NewStatEndpoints(m.Stat),
transport.NewRepoEndpoints(m.Repo),
)
// Add cors support
handler := cors.Default().Handler(r)
// a http.Server with pre-configured timeouts to avoid Slowloris attack
srv := &http.Server{
Addr: viper.GetString("port"),
Handler: handler,
ReadTimeout: time.Second * 10, // Variable always on the right, not 10 * time.Second
WriteTimeout: time.Second * 10,
IdleTimeout: time.Second * 60,
MaxHeaderBytes: 1 << 20,
}
// Setup pprof net/http
if viper.GetBool("pprof_enable") {
go func() {
stdlog.Fatal(http.ListenAndServe(viper.GetString("pprof_port"), nil))
}()
}
// Run our server in a goroutine so that it doesn't block
go func() {
stdlog.Printf("listening to port *%s. press ctrl + c to cancel.\n", viper.GetString("port"))
stdlog.Fatal(srv.ListenAndServe())
}()
// Setup memory profiler
profiler.MakeMemory(viper.GetString("memprofile"))
c := make(chan os.Signal, 1)
// Accept graceful shutdowns when quit via SIGINT (Ctrl + C) SIGKILL,
// SIGQUIT or SIGTERM (Ctrl + /) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(ctx, time.Second*viper.GetDuration("graceful_timeout"))
defer cancel()
// Doesn't block if no connections, but will otherwise wait until the timeout
srv.Shutdown(ctx)
stdlog.Println("shutting down server")
os.Exit(0)
}