File: events.go

package info (click to toggle)
golang-github-thejerf-suture 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 260 kB
  • sloc: sh: 24; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 4,584 bytes parent folder | download | duplicates (2)
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
package suture

import (
	"fmt"
)

// Event defines the interface implemented by all events Suture will
// generate.
//
// Map will return a map with the details of the event serialized into a
// map[string]interface{}, with only the values suitable for serialization.
type Event interface {
	fmt.Stringer
	Type() EventType
	Map() map[string]interface{}
}

type (
	EventType int

	EventHook func(Event)
)

const (
	EventTypeStopTimeout EventType = iota
	EventTypeServicePanic
	EventTypeServiceTerminate
	EventTypeBackoff
	EventTypeResume
)

type EventStopTimeout struct {
	Supervisor     *Supervisor `json:"-"`
	SupervisorName string      `json:"supervisor_name"`
	Service        Service     `json:"-"`
	ServiceName    string      `json:"service"`
}

func (e EventStopTimeout) Type() EventType {
	return EventTypeStopTimeout
}

func (e EventStopTimeout) String() string {
	return fmt.Sprintf(
		"%s: Service %s failed to terminate in a timely manner",
		e.Supervisor,
		e.Service,
	)
}

func (e EventStopTimeout) Map() map[string]interface{} {
	return map[string]interface{}{
		"supervisor_name": e.SupervisorName,
		"service_name":    e.ServiceName,
	}
}

type EventServicePanic struct {
	Supervisor       *Supervisor `json:"-"`
	SupervisorName   string      `json:"supervisor_name"`
	Service          Service     `json:"-"`
	ServiceName      string      `json:"service_name"`
	CurrentFailures  float64     `json:"current_failures"`
	FailureThreshold float64     `json:"failure_threshold"`
	Restarting       bool        `json:"restarting"`
	PanicMsg         string      `json:"panic_msg"`
	Stacktrace       string      `json:"stacktrace"`
}

func (e EventServicePanic) Type() EventType {
	return EventTypeServicePanic
}

func (e EventServicePanic) String() string {
	return fmt.Sprintf(
		"%s, panic: %s, stacktrace: %s",
		serviceFailureString(
			e.SupervisorName,
			e.ServiceName,
			e.CurrentFailures,
			e.FailureThreshold,
			e.Restarting,
		),
		e.PanicMsg,
		string(e.Stacktrace),
	)
}

func (e EventServicePanic) Map() map[string]interface{} {
	return map[string]interface{}{
		"supervisor_name":   e.SupervisorName,
		"service_name":      e.ServiceName,
		"current_failures":  e.CurrentFailures,
		"failure_threshold": e.FailureThreshold,
		"restarting":        e.Restarting,
		"panic_msg":         e.PanicMsg,
		"stacktrace":        e.Stacktrace,
	}
}

type EventServiceTerminate struct {
	Supervisor       *Supervisor `json:"-"`
	SupervisorName   string      `json:"supervisor_name"`
	Service          Service     `json:"-"`
	ServiceName      string      `json:"service_name"`
	CurrentFailures  float64     `json:"current_failures"`
	FailureThreshold float64     `json:"failure_threshold"`
	Restarting       bool        `json:"restarting"`
	Err              interface{} `json:"error_msg"`
}

func (e EventServiceTerminate) Type() EventType {
	return EventTypeServiceTerminate
}

func (e EventServiceTerminate) String() string {
	return fmt.Sprintf(
		"%s, error: %s",
		serviceFailureString(e.SupervisorName, e.ServiceName, e.CurrentFailures, e.FailureThreshold, e.Restarting),
		e.Err)
}

func (e EventServiceTerminate) Map() map[string]interface{} {
	return map[string]interface{}{
		"supervisor_name":   e.SupervisorName,
		"service_name":      e.ServiceName,
		"current_failures":  e.CurrentFailures,
		"failure_threshold": e.FailureThreshold,
		"restarting":        e.Restarting,
		"error":             e.Err,
	}
}

func serviceFailureString(supervisor, service string, currentFailures, failureThreshold float64, restarting bool) string {
	return fmt.Sprintf(
		"%s: Failed service '%s' (%f failures of %f), restarting: %#v",
		supervisor,
		service,
		currentFailures,
		failureThreshold,
		restarting,
	)
}

type EventBackoff struct {
	Supervisor     *Supervisor `json:"-"`
	SupervisorName string      `json:"supervisor_name"`
}

func (e EventBackoff) Type() EventType {
	return EventTypeBackoff
}

func (e EventBackoff) String() string {
	return fmt.Sprintf("%s: Entering the backoff state.", e.Supervisor)
}

func (e EventBackoff) Map() map[string]interface{} {
	return map[string]interface{}{
		"supervisor_name": e.SupervisorName,
	}
}

type EventResume struct {
	Supervisor     *Supervisor `json:"-"`
	SupervisorName string      `json:"supervisor_name"`
}

func (e EventResume) Type() EventType {
	return EventTypeResume
}

func (e EventResume) String() string {
	return fmt.Sprintf("%s: Exiting backoff state.", e.Supervisor)
}

func (e EventResume) Map() map[string]interface{} {
	return map[string]interface{}{
		"supervisor_name": e.SupervisorName,
	}
}