/
module.go
130 lines (111 loc) · 3.25 KB
/
module.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
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
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
"flamingo.me/dingo"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"flamingo.me/flamingo/v3/framework/flamingo"
)
var once = sync.Once{}
var shutdownOnce = sync.Once{}
type (
eventRouterProvider func() flamingo.EventRouter
flagSetProvider func() []*pflag.FlagSet
// Module for DI
Module struct{}
)
// Configure DI
func (m *Module) Configure(injector *dingo.Injector) {
injector.Bind(new(cobra.Command)).AnnotatedWith("flamingo").ToProvider(
func(
commands []*cobra.Command,
eventRouterProvider eventRouterProvider,
logger flamingo.Logger,
flagSetProvider flagSetProvider,
config *struct {
Name string `inject:"config:flamingo.cmd.name"`
}) *cobra.Command {
signals := make(chan os.Signal, 1)
shutdownComplete := make(chan struct{}, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
once.Do(func() {
go func() {
<-signals
// shutdown by signal for infinite running commands (e.g. serve command)
shutdownOnce.Do(func() {
shutdown(eventRouterProvider(), signals, shutdownComplete, logger)
<-shutdownComplete
})
}()
})
rootCmd := &cobra.Command{
Use: config.Name,
Short: "Flamingo " + config.Name,
TraverseChildren: true,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// shutdown through command that is finite (e.g. help command)
shutdownOnce.Do(func() {
shutdown(eventRouterProvider(), signals, shutdownComplete, logger)
<-shutdownComplete
})
},
Example: `Run with -h or -help to see global debug flags`,
}
//rootCmd.SetHelpTemplate()
rootCmd.FParseErrWhitelist.UnknownFlags = true
for _, set := range flagSetProvider() {
rootCmd.PersistentFlags().AddFlagSet(set)
}
rootCmd.AddCommand(commands...)
return rootCmd
},
).In(dingo.Singleton)
}
// CueConfig specifies the command name
func (*Module) CueConfig() string {
return fmt.Sprintf(`flamingo: cmd: name: string | *"%s"`, filepath.Base(os.Args[0]))
}
// FlamingoLegacyConfigAlias maps legacy config to new
func (*Module) FlamingoLegacyConfigAlias() map[string]string {
return map[string]string{"cmd.name": "flamingo.cmd.name"}
}
func shutdown(eventRouter flamingo.EventRouter, signals <-chan os.Signal, complete chan<- struct{}, logger flamingo.Logger) {
logger.Info("start graceful shutdown")
stopper := make(chan struct{})
go func() {
eventRouter.Dispatch(context.Background(), &flamingo.ShutdownEvent{})
close(stopper)
}()
select {
case <-signals:
logger.Info("second interrupt signal received, hard shutdown")
os.Exit(130)
case <-time.After(30 * time.Second):
logger.Info("time limit reached, hard shutdown")
os.Exit(130)
case <-stopper:
logger.Info("graceful shutdown complete")
complete <- struct{}{}
}
}
// Run the root command
func Run(injector *dingo.Injector) error {
i, err := injector.GetAnnotatedInstance(new(cobra.Command), "flamingo")
if err != nil {
return err
}
cmd := i.(*cobra.Command)
i, err = injector.GetInstance(new(eventRouterProvider))
if err != nil {
return err
}
i.(eventRouterProvider)().Dispatch(context.Background(), &flamingo.StartupEvent{})
return cmd.Execute()
}