-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.go
More file actions
45 lines (36 loc) · 760 Bytes
/
serve.go
File metadata and controls
45 lines (36 loc) · 760 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
38
39
40
41
42
43
44
45
package services
import (
"os"
"os/signal"
"syscall"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type Serve struct {
servables []Servable
}
type Servable interface {
Serve() error
}
func NewServe(s ...Servable) *Serve {
return &Serve{servables: s}
}
func (s *Serve) Serve() error {
serveError := make(chan error, 1)
for _, ss := range s.servables {
go func(sss Servable) {
err := sss.Serve()
serveError <- err
}(ss)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigs:
log.WithField("signal", sig).Info("Got syscall")
case err := <-serveError:
return errors.Wrap(err, "Got serve error")
}
log.Info("Shooting down... at last!")
return nil
}