-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
90 lines (72 loc) · 1.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"github.com/spf13/viper"
"github.com/tech-thinker/go-cookiecutter/config"
"github.com/tech-thinker/go-cookiecutter/instance"
"github.com/tech-thinker/go-cookiecutter/runner"
_ "github.com/lib/pq"
"github.com/urfave/cli"
)
func main() {
v := viper.New()
config := config.Init(v)
var shutDownChannel chan *bool
instance := instance.Init(config)
defer instance.Destroy()
clientApp := cli.NewApp()
clientApp.Name = "go-cookiecutter"
clientApp.Version = "0.0.1"
clientApp.Commands = []cli.Command{
{
Name: "start",
Usage: "Start the service",
Action: func(c *cli.Context) error {
ctx := context.Background()
var wg sync.WaitGroup
wg.Add(1)
go runner.NewAPI(config, instance).Go(ctx, &wg)
wg.Add(1)
go runner.NewGRPC(config, instance).Go(ctx, &wg)
wg.Wait()
return nil
},
},
{
Name: "start_workers",
Usage: "Start the workers",
Action: func(c *cli.Context) error {
ctx := context.Background()
var wg sync.WaitGroup
wg.Add(1)
go runner.NewWorker(config, instance).Go(ctx, shutDownChannel, &wg)
wg.Wait()
return nil
},
},
}
if err := clientApp.Run(os.Args); err != nil {
panic(err)
}
signalChannel := make(chan os.Signal, 2)
signal.Notify(
signalChannel,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
go func() {
<-signalChannel
shutDown(shutDownChannel)
}()
}
func shutDown(shutDownChannel chan *bool) {
shutDown := true
shutDownChannel <- &shutDown
close(shutDownChannel)
}