/
main.go
77 lines (65 loc) · 1.38 KB
/
main.go
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
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/undeconstructed/sample/common"
)
var log = logrus.StandardLogger()
func main() {
cmdTestMode := makeTestMode()
cmdRun := &cobra.Command{
Use: "run <service>",
Short: "Run a service",
Long: `run run run.`,
Args: cobra.MinimumNArgs(0),
}
cmdRunConfig := makeRunConfig()
cmdRunFetcher := makeRunFetcher()
cmdRunFrontend := makeRunFrontend()
cmdRunStore := makeRunStore()
var rootCmd = &cobra.Command{Use: "sample"}
rootCmd.AddCommand(cmdRun, cmdTestMode)
cmdRun.AddCommand(cmdRunConfig, cmdRunFetcher, cmdRunFrontend, cmdRunStore)
rootCmd.Execute()
}
func runService(service common.Service) {
log.Info("Starting")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, stop := context.WithCancel(context.Background())
errCh := make(chan error)
go func() {
err := service.Start(ctx)
if err != nil {
errCh <- err
}
close(errCh)
}()
select {
case e := <-errCh:
stop()
if e != nil {
log.WithError(e).Error("Error")
os.Exit(1)
}
os.Exit(0)
case s := <-c:
log.WithField("signal", s).Info("Got signal")
stop()
}
select {
case e := <-errCh:
if e != nil {
log.WithError(e).Error("Error")
os.Exit(1)
}
os.Exit(0)
case <-time.After(10 * time.Second):
log.Error("Shutdown timeout")
os.Exit(1)
}
}