-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
287 lines (245 loc) · 9.1 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
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"github.com/mergestat/mergestat/internal/cron"
"github.com/mergestat/mergestat/internal/jobs/repo"
"github.com/mergestat/mergestat/internal/syncer"
"github.com/mergestat/mergestat/internal/timeout"
"github.com/mergestat/sqlq"
"github.com/mergestat/sqlq/runtime/embed"
"github.com/mergestat/sqlq/schema"
"github.com/go-git/go-git/v5"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/mergestat/mergestat-lite/extensions"
"github.com/mergestat/mergestat-lite/extensions/options"
"github.com/mergestat/mergestat-lite/extensions/services"
"github.com/mergestat/mergestat-lite/pkg/locator"
_ "github.com/mergestat/mergestat-lite/pkg/sqlite"
"github.com/mergestat/mergestat/internal/scheduler"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/shurcooL/githubv4"
"go.riyazali.net/sqlite"
"golang.org/x/oauth2"
)
var (
postgresConnection = os.Getenv("POSTGRES_CONNECTION")
// baseCloneDir = os.Getenv("BASE_CLONE_DIR")
concurrencyEnv = os.Getenv("CONCURRENCY")
)
func repoLocator() services.RepoLocator {
return options.RepoLocatorFn(func(ctx context.Context, path string) (*git.Repository, error) {
if path == "" {
return nil, fmt.Errorf("no repo supplied")
}
return git.PlainOpen(path)
})
}
// type mutexRoundTripper struct {
// mut sync.Mutex
// }
// func (t *mutexRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
// t.mut.Lock()
// defer t.mut.Unlock()
// return http.DefaultTransport.RoundTrip(r)
// }
// logLevelFromEnv retrieves the log level from the LOG_LEVEL environment variable
// and returns the corresponding zerolog level.
func logLevelFromEnv() zerolog.Level {
switch os.Getenv("LOG_LEVEL") {
case "debug":
return zerolog.DebugLevel
case "info":
return zerolog.InfoLevel
case "warn":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
default:
return zerolog.InfoLevel
}
}
func main() {
logger := zerolog.New(os.Stderr).With().Timestamp().Logger().Level(logLevelFromEnv())
prettyLogs := os.Getenv("PRETTY_LOGS") == "1"
// if stdout is a terminal or if the PRETTY_LOGS environment variable is set
// to 1, use a human-friendly log formatter
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode()&os.ModeCharDevice) != 0 || prettyLogs {
logger = logger.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.Stamp})
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
var err error
concurrency := 1
if concurrencyEnv != "" {
if c, err := strconv.Atoi(concurrencyEnv); err != nil {
logger.Err(err).Msgf("could not parse CONCURRENCY env into an int: %s", concurrencyEnv)
} else {
concurrency = c
}
}
// https://www.alexedwards.net/blog/change-url-query-params-in-go
var u *url.URL
if u, err = url.Parse(postgresConnection); err != nil {
logger.Err(err).Msgf("could not parse database connection string: %v", err)
os.Exit(1)
}
v := u.Query()
v.Add("pool_max_conns", strconv.Itoa(concurrency+5))
u.RawQuery = v.Encode()
var pool *pgxpool.Pool
if pool, err = pgxpool.Connect(ctx, u.String()); err != nil {
logger.Err(err).Msgf("could not connect to database: %v", err)
os.Exit(1)
}
defer pool.Close()
var m *migrate.Migrate
if m, err = migrate.New("file://migrations", postgresConnection); err != nil {
logger.Err(err).Msgf("could not initialize migrations")
os.Exit(1)
}
if err := m.Up(); err != nil {
if !errors.Is(err, migrate.ErrNoChange) {
logger.Err(err).Msgf("could not run migrations: %v", err)
os.Exit(1)
}
}
srcErr, dbErr := m.Close()
if srcErr != nil {
logger.Err(srcErr).Msgf("could not close migrations with source error: %v", srcErr)
}
if dbErr != nil {
logger.Err(dbErr).Msgf("could not close migrations with db error: %v", dbErr)
}
logger.Info().Msg("starting syncer")
l := logger.Level(zerolog.InfoLevel).With().Bool("mergestat-query-exec", true).Logger()
ratelimitHandler := func(rlr *options.GitHubRateLimitResponse) {
untilResetDur := time.Until(rlr.ResetAt.Time)
secondsRemaining := untilResetDur.Seconds()
delayDur := 800 * time.Millisecond // Default delay is 800ms to prevent hitting the secondary rate limit
// if remaining points are 400 or less then we will delay until the quota is reset
if rlr.Remaining <= 400 {
delayDur = untilResetDur
}
logger.Info().
Int("remaining", rlr.Remaining).
Time("resets", rlr.ResetAt.Time).
Str("until-reset", untilResetDur.String()).
Float64("delay-seconds", delayDur.Seconds()).
Msgf("received rate limit info from GitHub API: %d remaining in next %ds. Delaying %ss", rlr.Remaining, int(secondsRemaining), strconv.FormatFloat(delayDur.Seconds(), 'f', 2, 64))
// Allow for shutdown during the delay
select {
case <-ctx.Done():
case <-time.After(delayDur):
}
}
// See here: https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits
// The GitHub API does not want users making concurrent API calls, on top of their point-based rate limiting
var githubRequestMutex sync.Mutex
githubPreRequestHook := func() {
githubRequestMutex.Lock()
}
githubPostRequestHook := func() {
githubRequestMutex.Unlock()
}
githubClientGetter := func() *githubv4.Client {
encryptionSecret := os.Getenv("ENCRYPTION_SECRET")
row := pool.QueryRow(context.TODO(), "SELECT pgp_sym_decrypt(credentials, $1) FROM mergestat.service_auth_credentials WHERE type = 'GITHUB_PAT' ORDER BY created_at DESC LIMIT 1", encryptionSecret)
var credentials []byte
if err := row.Scan(&credentials); err != nil && !errors.Is(err, pgx.ErrNoRows) {
logger.Err(err).Msgf("error retrieving GitHub PAT from database")
}
// default to GITHUB_TOKEN env var if nothing is in db
if credentials == nil {
logger.Info().Msg("no GitHub PAT found in DB, using GITHUB_TOKEN env")
credentials = []byte(os.Getenv("GITHUB_TOKEN"))
}
httpClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(credentials)},
))
// httpClient.Transport = &mutexRoundTripper{}
return githubv4.NewClient(httpClient)
}
sqlite.Register(
extensions.RegisterFn(
options.WithExtraFunctions(),
options.WithRepoLocator(locator.CachedLocator(repoLocator())),
options.WithGitHub(),
// options.WithContextValue("githubToken", os.Getenv("GITHUB_TOKEN")),
options.WithContextValue("githubPerPage", os.Getenv("GITHUB_PER_PAGE")),
options.WithContextValue("githubRateLimit", os.Getenv("GITHUB_RATE_LIMIT")),
options.WithGitHubRateLimitHandler(ratelimitHandler),
options.WithGitHubPreRequestHook(githubPreRequestHook),
options.WithGitHubPostRequestHook(githubPostRequestHook),
options.WithGitHubClientGetter(githubClientGetter),
options.WithNPM(),
options.WithLogger(&l),
),
)
var db *sqlx.DB
if db, err = sqlx.Open("sqlite3", ":memory:"); err != nil {
logger.Err(err).Msgf("could not open mergestat db: %v", err)
os.Exit(1)
}
// create a new sqlq worker to process tasks in background
var upstream *sql.DB
if upstream, err = sql.Open("pgx", postgresConnection); err != nil {
logger.Fatal().Err(err).Msg("failed to open connection to upstream")
}
// apply sqlq migrations
if err := schema.Apply(upstream); err != nil {
logger.Fatal().Err(err).Msg("failed to apply sqlq migrations")
}
var queues = []sqlq.Queue{"default"}
var worker, _ = embed.NewWorker(upstream, embed.WorkerConfig{Queues: queues})
// register job handlers for types implemented by this worker
_ = worker.Register("repos/auto-import", repo.AutoImport(&logger, pool))
// TODO all of the following "params" should be configurable
// either via the database/app or possibly with env vars
go scheduler.New(&logger, pool).Start(ctx, 1*time.Minute)
go timeout.New(&logger, pool).Start(ctx, time.Minute)
go syncer.New(pool, db, &logger, concurrency, 3*time.Second).Start(ctx)
// run a basic cron every minute to schedule a repos/auto-import job
// these jobs are idempotent, and so, multiple instances can run at same time without conflict
go cron.Basic(ctx, 1*time.Minute, func() {
if _, err := sqlq.Enqueue(upstream, "default", sqlq.NewJobDesc("repos/auto-import")); err != nil {
logger.Err(err).Msg("failed to enqueue repo sync job")
}
})
if os.Getenv("DEBUG") != "" {
go func() {
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8080", nil); err != nil {
logger.Err(err).Msgf("could not start HTTP handler")
}
}()
}
// start the worker
if err = worker.Start(); err != nil {
logger.Fatal().Err(err).Msg("failed to start background worker")
}
<-ctx.Done() // wait for ctrl+c
// stop the worker
if err = worker.Shutdown(30 * time.Second); err != nil {
logger.Err(err).Msg("failed to terminate worker gracefully")
}
}