-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
37 lines (30 loc) · 765 Bytes
/
runner.go
File metadata and controls
37 lines (30 loc) · 765 Bytes
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
package hyper
import (
"context"
"os"
"os/signal"
"syscall"
)
func (h HTTP) Run() {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
for _, fn := range h.PreHooks {
fn(ctx)
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
finisher := make(chan error, 1)
go h.Serve(ctx, finisher)
go h.osSignalListener(ctx, cancelFunc, sig)
if err := <-finisher; err != nil {
h.Log.Errorf(ctx, err, "failed to run api")
}
for _, fn := range h.PostHooks {
fn(ctx)
}
}
func (h HTTP) osSignalListener(ctx context.Context, cancelFunc func(), sig chan os.Signal) {
osCall := <-sig
h.Log.Infof(ctx, "%s received, graceful shutdown started", osCall.String())
cancelFunc()
}