-
Notifications
You must be signed in to change notification settings - Fork 26
/
batchiepatchie.go
164 lines (143 loc) · 5.33 KB
/
batchiepatchie.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
package main
import (
"net/http"
"os"
"path"
"strconv"
"github.com/AdRoll/batchiepatchie/config"
"github.com/AdRoll/batchiepatchie/fetcher"
"github.com/AdRoll/batchiepatchie/handlers"
"github.com/AdRoll/batchiepatchie/jobs"
"github.com/AdRoll/batchiepatchie/syncer"
"github.com/bakatz/echo-logrusmiddleware"
"github.com/labstack/echo"
"github.com/opentracing/opentracing-go"
log "github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// fetchIndex fetches the index.html from s3
func fetchIndex() ([]byte, error) {
if config.Conf.FrontendAssets == "local" {
dir := path.Join(config.Conf.FrontendAssetsLocalPrefix, "index.html")
log.Info("Getting index.html from local file:", dir)
return fetcher.ReadAll(dir)
}
s3path := "s3://" + config.Conf.FrontendAssetsBucket + "/" + config.Conf.FrontendAssetsKey
log.Info("Downloading index.html from ", s3path)
return fetcher.ReadAll(s3path)
}
func pingHandler(c echo.Context) error {
c.String(http.StatusOK, "pong")
return nil
}
func main() {
configurationFile := ""
if len(os.Args) > 2 {
log.Fatal("batchiepatchie expects exactly one argument: filename to .toml configuration.")
} else if len(os.Args) == 2 {
configurationFile = os.Args[1]
} else {
/* Fallback to using environment variables */
configurationFile = os.Getenv("BATCHIEPATCHIE_CONFIG")
if configurationFile == "" {
log.Fatal("No configuration file passed through either command line argument or BATCHIEPATCHIE_CONFIG environment variable.")
}
}
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
// Sets the global config.Conf
err := config.ReadConfiguration(configurationFile)
if err != nil {
log.Fatal("Reading configuration failed, ", err)
}
if config.Conf.LogEntriesKey != "" {
log.Info("logentries_token supplied, will connect to LogEntries.")
logentries_host := "data.logentries.com:443"
if config.Conf.LogEntriesHost != "" {
logentries_host = config.Conf.LogEntriesHost
}
setUpLogEntriesHooks(logentries_host, config.Conf.LogEntriesKey)
}
var trace opentracing.Tracer
if config.Conf.UseDatadogTracing {
ip := os.Getenv("BATCHIEPATCHIE_IP")
if ip != "" {
// If we have been passed an IP explictly; attempt to
// use it to connect to DataDog tracer When we run
// batchiepatchie inside Docker container and ddtracer
// on the host; this lets us connect to the agent
// running on host.
agentAddr := ip + ":8126"
log.Info("Will attempt to ddtrace into ", agentAddr)
trace = opentracer.New(tracer.WithServiceName("batchiepatchie"), tracer.WithAgentAddr(agentAddr))
} else {
trace = opentracer.New(tracer.WithServiceName("batchiepatchie"))
}
} else {
trace = opentracing.NoopTracer{}
}
opentracing.SetGlobalTracer(trace)
storage, err := jobs.NewPostgreSQLStore(config.Conf.DatabaseHost, config.Conf.DatabasePort, config.Conf.DatabaseUsername, config.Conf.DatabaseName, config.Conf.DatabasePassword, config.Conf.DatabaseRootCertificate)
if err != nil {
log.Fatal("Creating postgresql store failed, ", err)
}
log.Info("Successfully connected to PostgreSQL database.")
killer, err := jobs.NewKillerHandler()
if err != nil {
log.Fatal("Creating killer handler failed, ", err)
}
log.Info("killer handler started.")
index, err := fetchIndex()
if err != nil {
log.Error("Falling back to basic index.html: ", err)
version := os.Getenv("VERSION")
if version == "" {
index = []byte("<h1>Cannot find index.html. VERSION environment variable is not set. Check that frontend has been deployed correctly and then restart backend.</h1>")
} else {
index = []byte("<h1>Cannot find index.html. (VERSION environment variable has been set but no file could be fetched). Check that frontend has been deployed correctly and then restart backend.</h1>")
}
}
// Launch the periodic synchronizer
syncer.RunPeriodicSynchronizer(storage, killer)
// Launch the periodic scaler
syncer.RunPeriodicScaler(storage)
// handle.Server is a structure to save context shared between requests
s := &handlers.Server{
Storage: storage,
Killer: killer,
Index: index,
}
e := echo.New()
// Logging middleware for API requests
e.Logger = logrusmiddleware.Logger{Logger: log.StandardLogger()}
e.Use(logrusmiddleware.Hook())
// Jobs API
api := e.Group("/api/v1")
{
api.GET("/jobs/:id", s.FindOne)
api.GET("/jobs", s.Find)
api.POST("/jobs/kill", s.KillMany)
api.GET("/jobs/:id/logs", s.FetchLogs)
api.GET("/job_queues/active", s.ListActiveJobQueues)
api.GET("/job_queues/all", s.ListAllJobQueues)
api.POST("/job_queues/:name/activate", s.ActivateJobQueue)
api.POST("/job_queues/:name/deactivate", s.DeactivateJobQueue)
api.GET("/jobs/:id/status", s.GetStatus)
api.POST("/jobs/notify", s.JobStatusNotification)
api.GET("/jobs/:id/status_websocket", s.SubscribeToJobEvent)
api.GET("/jobs/stats", s.JobStats)
}
e.GET("/ping", pingHandler)
e.GET("/", s.IndexHandler)
e.GET("/stats", s.IndexHandler)
e.GET("/index.html", s.IndexHandler)
// These are pseudo-URLs, the frontend will handle displaying the correct page
e.GET("/job/:id", s.IndexHandler)
e.GET("/job_queues", s.IndexHandler)
if config.Conf.FrontendAssets == "local" {
e.Static("/*", config.Conf.FrontendAssetsLocalPrefix)
}
// Launch web server
e.Logger.Fatal(e.Start(config.Conf.Host + ":" + strconv.Itoa(config.Conf.Port)))
}