-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (124 loc) · 4.01 KB
/
Copy pathmain.go
File metadata and controls
141 lines (124 loc) · 4.01 KB
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"slices"
"syscall"
"api.audius.co/api"
"api.audius.co/config"
"api.audius.co/ddl"
"api.audius.co/esindexer"
eth_indexer "api.audius.co/eth/indexer"
core_indexer "api.audius.co/indexer"
"api.audius.co/logging"
solana_indexer "api.audius.co/solana/indexer"
etldb "github.com/OpenAudio/go-openaudio/pkg/etl/db"
)
func main() {
command := "server"
if len(os.Args) > 1 {
command = os.Args[1]
}
if !config.Cfg.RunMigrations && command != "migrate" {
fmt.Println("Skipping migrations. Set env runMigrations=true to run.")
} else {
fmt.Println("Running migrations...")
if err := ddl.RunMigrations(); err != nil {
fmt.Println("migration failed:", err)
os.Exit(1)
}
}
switch command {
case "server":
{
fmt.Println("Running server...")
as := api.NewApiServer(config.Cfg)
as.Serve()
}
case "indexer":
{
fmt.Println("Running indexer...")
indexer := core_indexer.NewIndexer(config.Cfg)
defer indexer.Close()
// Capture termination signals for graceful shutdown of the indexer
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
defer stop()
if err := indexer.Start(ctx); err != nil {
if !errors.Is(err, context.Canceled) {
panic(err)
}
}
}
case "es-indexer":
{
collections := os.Args[2:]
drop := slices.Contains(collections, "drop")
fmt.Printf("Reindexing ElasticSearch (collections=%s, drop=%t)...\n", collections, drop)
esindexer.ReindexLegacy(drop, collections...)
}
case "solana-indexer":
{
fmt.Println("Running solana-indexer...")
solanaIndexer := solana_indexer.New(config.Cfg)
defer solanaIndexer.Close()
healthServer := solana_indexer.NewServer(solanaIndexer)
// Capture termination signals for graceful shutdown of the indexer
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
defer stop()
go healthServer.Start(ctx)
if err := solanaIndexer.Start(ctx); err != nil {
if !errors.Is(err, context.Canceled) {
panic(err)
}
}
}
case "eth-indexer":
{
fmt.Println("Running eth-indexer...")
ethIndexer := eth_indexer.New(config.Cfg)
defer ethIndexer.Close()
healthServer := eth_indexer.NewServer(ethIndexer)
// Capture termination signals for graceful shutdown of the indexer
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
defer stop()
go healthServer.Start(ctx)
if err := ethIndexer.Start(ctx); err != nil {
if !errors.Is(err, context.Canceled) {
panic(err)
}
}
}
case "migrate":
{
// ddl migrations already ran above. Apply the ETL module's too, so a
// database migrated by this command matches a deployed one.
//
// They are otherwise only ever applied at indexer start, which left
// `make test-schema` unable to produce them: that target seeds from
// sql/01_schema.sql, runs this command, and dumps the result, so
// ETL-created objects could never enter the loop no matter how often
// it was regenerated. The four partial unique indexes from pkg/etl
// 0030 were consequently absent under test while present in every
// deployment — which is how ddl 0236 came to be written against the
// wrong constraint and still passed CI.
//
// Ordering is the useful side effect: ddl migrations run before these,
// in one process, so a ddl migration that has to precede an ETL one
// (0237 before 0035) is sequenced here rather than across two pods.
//
// Idempotent and tracked separately in etl_db_migrations, so the
// indexer applying them again at startup is a no-op.
logger := logging.NewZapLogger(config.Cfg).Named("migrate")
if err := etldb.RunMigrations(logger, config.Cfg.WriteDbUrl, false); err != nil {
fmt.Println("etl migration failed:", err)
os.Exit(1)
}
os.Exit(0)
}
default:
fmt.Printf("Unrecognized command: %s", command)
}
}