forked from travis-ci/worker
/
cli.go
348 lines (284 loc) · 8.66 KB
/
cli.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package worker
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
// include for conditional pprof HTTP server
_ "net/http/pprof"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/getsentry/raven-go"
"github.com/mihasya/go-metrics-librato"
"github.com/rcrowley/go-metrics"
"github.com/streadway/amqp"
"github.com/travis-ci/worker/backend"
"github.com/travis-ci/worker/config"
"github.com/travis-ci/worker/context"
travismetrics "github.com/travis-ci/worker/metrics"
gocontext "golang.org/x/net/context"
)
// CLI is the top level of execution for the whole shebang
type CLI struct {
c *cli.Context
bootTime time.Time
ctx gocontext.Context
cancel gocontext.CancelFunc
logger *logrus.Entry
Config *config.Config
BuildScriptGenerator BuildScriptGenerator
BackendProvider backend.Provider
ProcessorPool *ProcessorPool
Canceller Canceller
JobQueue JobQueue
}
// NewCLI creates a new *CLI from a *cli.Context
func NewCLI(c *cli.Context) *CLI {
return &CLI{
c: c,
bootTime: time.Now().UTC(),
}
}
// Setup runs one-time preparatory actions and returns a boolean success value
// that is used to determine if it is safe to invoke the Run func
func (i *CLI) Setup() (bool, error) {
if i.c.String("pprof-port") != "" {
// Start net/http/pprof server
go func() {
http.ListenAndServe(fmt.Sprintf("localhost:%s", i.c.String("pprof-port")), nil)
}()
}
if i.c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
ctx, cancel := gocontext.WithCancel(gocontext.Background())
logger := context.LoggerFromContext(ctx)
i.ctx = ctx
i.cancel = cancel
i.logger = logger
logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
cfg := config.FromCLIContext(i.c)
i.Config = cfg
if i.c.Bool("echo-config") {
config.WriteEnvConfig(cfg, os.Stdout)
return false, nil
}
if i.c.Bool("list-backend-providers") {
backend.EachBackend(func(b *backend.Backend) {
fmt.Println(b.Alias)
})
return false, nil
}
logger.WithFields(logrus.Fields{
"cfg": fmt.Sprintf("%#v", cfg),
}).Debug("read config")
i.setupSentry()
i.setupMetrics()
err := i.setupJobQueueAndCanceller()
if err != nil {
logger.WithField("err", err).Error("couldn't create job queue and canceller")
return false, err
}
generator := NewBuildScriptGenerator(cfg)
logger.WithFields(logrus.Fields{
"build_script_generator": fmt.Sprintf("%#v", generator),
}).Debug("built")
i.BuildScriptGenerator = generator
provider, err := backend.NewBackendProvider(cfg.ProviderName, cfg.ProviderConfig)
if err != nil {
logger.WithField("err", err).Error("couldn't create backend provider")
return false, err
}
err = provider.Setup(ctx)
if err != nil {
logger.WithField("err", err).Error("couldn't setup backend provider")
return false, err
}
logger.WithFields(logrus.Fields{
"provider": fmt.Sprintf("%#v", provider),
}).Debug("built")
i.BackendProvider = provider
ppc := &ProcessorPoolConfig{
Hostname: cfg.Hostname,
Context: ctx,
HardTimeout: cfg.HardTimeout,
LogTimeout: cfg.LogTimeout,
ScriptUploadTimeout: cfg.ScriptUploadTimeout,
StartupTimeout: cfg.StartupTimeout,
}
pool := NewProcessorPool(ppc, i.BackendProvider, i.BuildScriptGenerator, i.Canceller)
pool.SkipShutdownOnLogTimeout = cfg.SkipShutdownOnLogTimeout
logger.WithFields(logrus.Fields{
"pool": pool,
}).Debug("built")
i.ProcessorPool = pool
return true, nil
}
// Run starts all long-running processes and blocks until the processor pool
// returns from its Run func
func (i *CLI) Run() {
if os.Getenv("START_HOOK") != "" {
_ = exec.Command("/bin/sh", "-c", os.Getenv("START_HOOK")).Run()
}
if os.Getenv("STOP_HOOK") != "" {
defer exec.Command("/bin/sh", "-c", os.Getenv("STOP_HOOK")).Run()
}
i.logger.Info("worker started")
defer i.logger.Info("worker finished")
go i.signalHandler()
i.logger.WithFields(logrus.Fields{
"pool_size": i.Config.PoolSize,
"queue": i.JobQueue,
}).Debug("running pool")
i.ProcessorPool.Run(i.Config.PoolSize, i.JobQueue)
err := i.JobQueue.Cleanup()
if err != nil {
i.logger.WithField("err", err).Error("couldn't clean up job queue")
}
}
func (i *CLI) setupSentry() {
if i.Config.SentryDSN == "" {
return
}
levels := []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
}
if i.Config.SentryHookErrors {
levels = append(levels, logrus.ErrorLevel)
}
sentryHook, err := NewSentryHook(i.Config.SentryDSN, levels)
if err != nil {
i.logger.WithField("err", err).Error("couldn't create sentry hook")
}
logrus.AddHook(sentryHook)
err = raven.SetDSN(i.Config.SentryDSN)
if err != nil {
i.logger.WithField("err", err).Error("couldn't set DSN in raven")
}
}
func (i *CLI) setupMetrics() {
go travismetrics.ReportMemstatsMetrics()
if i.Config.LibratoEmail != "" && i.Config.LibratoToken != "" && i.Config.LibratoSource != "" {
i.logger.Info("starting librato metrics reporter")
go librato.Librato(metrics.DefaultRegistry, time.Minute,
i.Config.LibratoEmail, i.Config.LibratoToken, i.Config.LibratoSource,
[]float64{0.50, 0.75, 0.90, 0.95, 0.99, 0.999, 1.0}, time.Millisecond)
} else if !i.c.Bool("silence-metrics") {
i.logger.Info("starting logger metrics reporter")
go metrics.Log(metrics.DefaultRegistry, time.Minute,
log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
}
}
func (i *CLI) signalHandler() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT,
syscall.SIGUSR1, syscall.SIGTTIN, syscall.SIGTTOU)
for {
select {
case sig := <-signalChan:
switch sig {
case syscall.SIGINT:
i.logger.Info("SIGINT received, starting graceful shutdown")
i.ProcessorPool.GracefulShutdown()
case syscall.SIGTERM:
i.logger.Info("SIGTERM received, shutting down immediately")
i.cancel()
case syscall.SIGTTIN:
i.logger.Info("SIGTTIN received, adding processor to pool")
i.ProcessorPool.Incr()
case syscall.SIGTTOU:
i.logger.Info("SIGTTOU received, removing processor from pool")
i.ProcessorPool.Decr()
case syscall.SIGUSR1:
i.logger.WithFields(logrus.Fields{
"version": VersionString,
"revision": RevisionString,
"generated": GeneratedString,
"boot_time": i.bootTime.String(),
"uptime": time.Since(i.bootTime),
"pool_size": i.ProcessorPool.Size(),
}).Info("SIGUSR1 received, dumping info")
i.ProcessorPool.Each(func(n int, proc *Processor) {
i.logger.WithFields(logrus.Fields{
"n": n,
"id": proc.ID,
"processed": proc.ProcessedCount,
"status": proc.CurrentStatus,
"last_job_id": proc.LastJobID,
}).Info("processor info")
})
default:
i.logger.WithField("signal", sig).Info("ignoring unknown signal")
}
default:
time.Sleep(time.Second)
}
}
}
func (i *CLI) setupJobQueueAndCanceller() error {
switch i.Config.QueueType {
case "amqp":
var amqpConn *amqp.Connection
var err error
if i.Config.AmqpInsecure {
amqpConn, err = amqp.DialTLS(
i.Config.AmqpURI,
&tls.Config{InsecureSkipVerify: true},
)
} else {
amqpConn, err = amqp.Dial(i.Config.AmqpURI)
}
if err != nil {
i.logger.WithField("err", err).Error("couldn't connect to AMQP")
return err
}
go i.amqpErrorWatcher(amqpConn)
i.logger.Debug("connected to AMQP")
canceller := NewAMQPCanceller(i.ctx, amqpConn)
i.logger.WithFields(logrus.Fields{
"canceller": fmt.Sprintf("%#v", canceller),
}).Debug("built")
i.Canceller = canceller
go canceller.Run()
jobQueue, err := NewAMQPJobQueue(amqpConn, i.Config.QueueName)
if err != nil {
return err
}
jobQueue.DefaultLanguage = i.Config.DefaultLanguage
jobQueue.DefaultDist = i.Config.DefaultDist
jobQueue.DefaultGroup = i.Config.DefaultGroup
jobQueue.DefaultOS = i.Config.DefaultOS
i.JobQueue = jobQueue
return nil
case "file":
canceller := NewFileCanceller(i.ctx, i.Config.BaseDir)
go canceller.Run()
i.Canceller = canceller
jobQueue, err := NewFileJobQueue(i.Config.BaseDir, i.Config.QueueName, i.Config.FilePollingInterval)
if err != nil {
return err
}
jobQueue.DefaultLanguage = i.Config.DefaultLanguage
jobQueue.DefaultDist = i.Config.DefaultDist
jobQueue.DefaultGroup = i.Config.DefaultGroup
jobQueue.DefaultOS = i.Config.DefaultOS
i.JobQueue = jobQueue
return nil
}
return fmt.Errorf("unknown queue type %q", i.Config.QueueType)
}
func (i *CLI) amqpErrorWatcher(amqpConn *amqp.Connection) {
errChan := make(chan *amqp.Error)
errChan = amqpConn.NotifyClose(errChan)
err, ok := <-errChan
if ok {
i.logger.WithField("err", err).Error("amqp connection errored, terminating")
i.cancel()
}
}