forked from rudderlabs/rudder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (158 loc) · 5.01 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
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/bugsnag/bugsnag-go"
"github.com/rudderlabs/rudder-server/processor/transformer"
"github.com/rudderlabs/rudder-server/admin"
"github.com/rudderlabs/rudder-server/app"
"github.com/rudderlabs/rudder-server/app/apphandlers"
"github.com/rudderlabs/rudder-server/config"
backendconfig "github.com/rudderlabs/rudder-server/config/backend-config"
"github.com/rudderlabs/rudder-server/router"
"github.com/rudderlabs/rudder-server/rruntime"
"github.com/rudderlabs/rudder-server/services/stats"
"github.com/rudderlabs/rudder-server/utils/logger"
"github.com/rudderlabs/rudder-server/utils/misc"
"github.com/rudderlabs/rudder-server/utils/types"
"github.com/rudderlabs/rudder-server/warehouse"
// This is necessary for compatibility with enterprise features
_ "github.com/rudderlabs/rudder-server/imports"
)
var (
application app.Interface
warehouseMode string
enableSuppressUserFeature bool
pkgLogger logger.LoggerI
appHandler apphandlers.AppHandler
)
var version = "Not an official release. Get the latest release from the github repo."
var major, minor, commit, buildDate, builtBy, gitURL, patch string
// Test Function
func readIOforResume(router router.HandleT) {
for {
var u string
_, err := fmt.Scanf("%v", &u)
fmt.Println("from stdin ", u)
if err != nil {
panic(err)
}
router.ResetSleep()
}
}
func loadConfig() {
warehouseMode = config.GetString("Warehouse.mode", "embedded")
enableSuppressUserFeature = config.GetBool("Gateway.enableSuppressUserFeature", false)
}
func init() {
loadConfig()
pkgLogger = logger.NewLogger().Child("main")
}
func versionInfo() map[string]interface{} {
return map[string]interface{}{"Version": version, "Major": major, "Minor": minor, "Patch": patch, "Commit": commit, "BuildDate": buildDate, "BuiltBy": builtBy, "GitUrl": gitURL, "TransformerVersion": transformer.GetVersion()}
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
var version = versionInfo()
versionFormatted, _ := json.Marshal(&version)
w.Write(versionFormatted)
}
func printVersion() {
version := versionInfo()
versionFormatted, _ := json.MarshalIndent(&version, "", " ")
fmt.Printf("Version Info %s\n", versionFormatted)
}
func startWarehouseService() {
warehouse.Start()
}
func canStartServer() bool {
pkgLogger.Info("warehousemode ", warehouseMode)
return warehouseMode == config.EmbeddedMode || warehouseMode == config.OffMode
}
func canStartWarehouse() bool {
return warehouseMode != config.OffMode
}
func main() {
options := app.LoadOptions()
if options.VersionFlag {
printVersion()
return
}
application = app.New(options)
//application & backend setup should be done before starting any new goroutines.
application.Setup()
appTypeStr := strings.ToUpper(config.GetEnv("APP_TYPE", app.EMBEDDED))
appHandler = apphandlers.GetAppHandler(application, appTypeStr, versionHandler)
version := versionInfo()
bugsnag.Configure(bugsnag.Configuration{
APIKey: config.GetEnv("BUGSNAG_KEY", ""),
ReleaseStage: config.GetEnv("GO_ENV", "development"),
// The import paths for the Go packages containing your source files
ProjectPackages: []string{"main", "github.com/rudderlabs/rudder-server"},
// more configuration options
AppType: appHandler.GetAppType(),
AppVersion: version["Version"].(string),
PanicHandler: func() {},
})
ctx := bugsnag.StartSession(context.Background())
defer func() {
if r := recover(); r != nil {
defer bugsnag.AutoNotify(ctx, bugsnag.SeverityError, bugsnag.MetaData{
"GoRoutines": {
"Number": runtime.NumGoroutine(),
}})
misc.RecordAppError(fmt.Errorf("%v", r))
pkgLogger.Fatal(r)
panic(r)
}
}()
//Creating Stats Client should be done right after setting up logger and before setting up other modules.
stats.Setup()
var pollRegulations bool
if enableSuppressUserFeature {
if application.Features().SuppressUser != nil {
pollRegulations = true
} else {
pkgLogger.Info("Suppress User feature is enterprise only. Unable to poll regulations.")
}
}
var configEnvHandler types.ConfigEnvI
if application.Features().ConfigEnv != nil {
configEnvHandler = application.Features().ConfigEnv.Setup()
}
backendconfig.Setup(pollRegulations, configEnvHandler)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
application.Stop()
// clearing zap Log buffer to std output
if logger.Log != nil {
logger.Log.Sync()
}
stats.StopRuntimeStats()
os.Exit(1)
}()
misc.AppStartTime = time.Now().Unix()
if canStartServer() {
appHandler.HandleRecovery(options)
rruntime.Go(func() {
appHandler.StartRudderCore(options)
})
}
// initialize warehouse service after core to handle non-normal recovery modes
if appTypeStr != app.GATEWAY && canStartWarehouse() {
rruntime.Go(func() {
startWarehouseService()
})
}
rruntime.Go(admin.StartServer)
misc.KeepProcessAlive()
}