summaryrefslogtreecommitdiff
path: root/cmd/rr/limit/debug.go
blob: b9d919dc4a33f70230dfc16e8c858b2b8107483a (plain)
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
package limit

import (
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spiral/roadrunner"
	rr "github.com/spiral/roadrunner/cmd/rr/cmd"
	"github.com/spiral/roadrunner/cmd/util"
	"github.com/spiral/roadrunner/service/limit"
)

func init() {
	cobra.OnInitialize(func() {
		if rr.Debug {
			svc, _ := rr.Container.Get(limit.ID)
			if svc, ok := svc.(*limit.Service); ok {
				svc.AddListener((&debugger{logger: rr.Logger}).listener)
			}
		}
	})
}

// listener provide debug callback for system events. With colors!
type debugger struct{ logger *logrus.Logger }

// listener listens to http events and generates nice looking output.
func (s *debugger) listener(event int, ctx interface{}) {
	if util.LogEvent(s.logger, event, ctx) {
		// handler by default debug package
		return
	}

	// watchers
	switch event {
	case limit.EventTTL:
		w := ctx.(roadrunner.WorkerError)
		s.logger.Debug(util.Sprintf(
			"<white+hb>worker.%v</reset> <yellow>%s</reset>",
			*w.Worker.Pid,
			w.Caused,
		))
		return

	case limit.EventIdleTTL:
		w := ctx.(roadrunner.WorkerError)
		s.logger.Debug(util.Sprintf(
			"<white+hb>worker.%v</reset> <yellow>%s</reset>",
			*w.Worker.Pid,
			w.Caused,
		))
		return

	case limit.EventMaxMemory:
		w := ctx.(roadrunner.WorkerError)
		s.logger.Error(util.Sprintf(
			"<white+hb>worker.%v</reset> <red>%s</reset>",
			*w.Worker.Pid,
			w.Caused,
		))
		return

	case limit.EventExecTTL:
		w := ctx.(roadrunner.WorkerError)
		s.logger.Error(util.Sprintf(
			"<white+hb>worker.%v</reset> <red>%s</reset>",
			*w.Worker.Pid,
			w.Caused,
		))
		return
	}
}