-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhive.go
More file actions
213 lines (182 loc) · 4.65 KB
/
Copy pathhive.go
File metadata and controls
213 lines (182 loc) · 4.65 KB
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
// Package hive provides common code for Hive services.
package hive
import (
"context"
"errors"
"fmt"
"io"
stdlog "log"
"os"
"os/signal"
"syscall"
"golang.org/x/term"
"gbenson.net/go/logger"
"gbenson.net/hive/logging/event"
"gbenson.net/hive/messaging"
"gbenson.net/hive/util"
)
type MessagingService interface {
// Start starts the service's goroutines.
Start(ctx context.Context, ch messaging.Channel) (<-chan error, error)
}
type StandaloneService interface {
// Start starts the service's goroutines.
Start(ctx context.Context) (<-chan error, error)
}
// Run runs a Hive service or application.
func Run(s any) {
log := logger.New(nil)
if !term.IsTerminal(int(os.Stdout.Fd())) {
log = log.With().
Str(event.LoggerTagField, "hive-service-go").
Logger()
}
RunContext(log.WithContext(context.Background()), s)
}
// RunContext runs a Hive service or application with the given
// context.
func RunContext(ctx context.Context, s any) {
if ctx == nil {
panic("nil context")
}
if s == nil {
panic("nil service")
}
if stdlog.Flags() == stdlog.LstdFlags {
stdlog.SetFlags(stdlog.Lshortfile)
}
if main, ok := s.(func(context.Context) error); ok {
if err := main(ctx); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
return
}
log := logger.Ctx(ctx).With().
Str("service", "hive-"+util.ServiceName()).
Logger()
log.Info().Msg("Starting")
if err := runContext(ctx, s); err != nil && err != context.Canceled {
log.WithLevel(logger.LevelFor(err)).
Err(err).
Msg("Error exit")
os.Exit(1)
}
log.Info().Msg("Normal exit")
}
func runContext(ctx context.Context, s any) error {
log := logger.Ctx(ctx)
// Connect to the message bus if required.
var ch messaging.Channel
var closeC <-chan error
if _, ok := s.(MessagingService); ok {
conn, err := messaging.Dial(ctx)
if err != nil {
return err
}
defer logger.LoggedClose(log, conn, "message bus connection")
closeC = conn.NotifyClose()
ch, err = conn.Channel()
if err != nil {
return err
}
defer logger.LoggedClose(log, ch, "messaging channel")
} else {
closeC = make(chan error)
}
// Start the service's goroutines.
if c, ok := s.(io.Closer); ok {
defer logger.LoggedClose(log, c, "service", "stop")
}
ctx, stop := signal.NotifyContext(
ctx,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
)
defer stop()
var errC <-chan error
var err error
switch ss := s.(type) {
case MessagingService:
errC, err = ss.Start(ctx, ch)
case StandaloneService:
errC, err = ss.Start(ctx)
default:
err = fmt.Errorf("service: unsupported type: %T", s)
}
if err != nil {
return err
}
// Block until stopped or interrupted.
//
// Shutdown sequence:
// 1. One of the following causes ctx to be canceled:
// - a service goroutine writes/closes errC
// - amqp client writes/closing closeC
// - one of the signals is recieved
// 2. Canceling ctx resets the signal handlers, allowing the
// shutdown to be interrupted if it locks up or whatever.
// 3. [If it wasn't a signal, select now reads context.Canceled]
// 4. This function returns.
// 5. [deferred service.Close is called, if installed]
// 6. [deferred channel.Close is called]
// 7. [deferred connection.Close is called. Thiswaits for the
// NotifyClose goroutine to exit, so if this wasn't the AMQP
// client closing the connection already then we will now
// wait for that to happen]
// 8. Control finally returns to RunContext, which logs any error.
for {
select {
case <-ctx.Done():
if err == nil {
// A signal was received.
err = ctx.Err()
if err == context.Canceled {
err = nil
}
}
return err
case re, ok := <-closeC:
err = channelShutdown(ctx, re, err, ok, "close notify")
stop()
case re, ok := <-errC:
err = channelShutdown(ctx, re, err, ok, "service error")
stop()
}
}
}
var closedChannels = make(map[string]bool)
func channelShutdown(
ctx context.Context,
thisEvent, shutdownCause error,
isChannelOpen bool,
channelName string,
) error {
log := logger.Ctx(ctx).With().Str("channel", channelName).Logger()
if thisEvent == nil {
if isChannelOpen {
thisEvent = errors.New("nil error")
} else {
thisEvent = channelClosed(channelName)
}
}
if shutdownCause != nil {
log.Debug().
Err(thisEvent).
Str("when", "during shutdown").
Msg("Activity")
return shutdownCause
}
log.Err(thisEvent).Msg("Initiating shutdown")
return thisEvent
}
func channelClosed(name string) error {
err := fmt.Errorf("%s channel: closed", name)
if closedChannels[name] {
err = fmt.Errorf("%w again!", err)
} else {
closedChannels[name] = true
}
return err
}