forked from the-anna-project/annad
/
command.go
162 lines (123 loc) · 6.24 KB
/
command.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package boot
import (
"os"
"os/signal"
"sync"
"github.com/spf13/cobra"
"github.com/the-anna-project/annad/object/config"
servicespec "github.com/the-anna-project/spec/service"
)
// New creates a new boot command.
func New() *Command {
command := &Command{}
command.SetConfigCollection(config.NewCollection())
return command
}
// Command represents the boot command.
type Command struct {
// Dependencies.
configCollection *config.Collection
serviceCollection servicespec.ServiceCollection
// Settings.
bootOnce sync.Once
gitCommit string
goArch string
goOS string
goVersion string
projectVersion string
shutdownOnce sync.Once
}
// Boot makes the neural network boot and run.
func (c *Command) Boot() {
go c.ListenToSignal()
c.serviceCollection = c.newServiceCollection()
go c.serviceCollection.Boot()
// Block the main goroutine forever. The process is only supposed to be ended
// by a call to Shutdown or ForceShutdown.
select {}
}
// Execute represents the cobra run method.
func (c *Command) Execute(cmd *cobra.Command, args []string) {
c.Boot()
}
// ForceShutdown forces the process to stop immediately.
func (c *Command) ForceShutdown() {
os.Exit(0)
}
// New creates a new cobra command for the boot command.
func (c *Command) New() *cobra.Command {
newCmd := &cobra.Command{
Use: "boot",
Short: "Boot and run the anna daemon.",
Long: "Boot and run the anna daemon.",
Run: c.Execute,
}
c.configCollection.Config().SetDir(newCmd.PersistentFlags().String("config.dir", ".", "directory where to find the config file"))
c.configCollection.Config().SetName(newCmd.PersistentFlags().String("config.name", "config", "name of the config file without extension"))
c.configCollection.Endpoint().Text().SetAddress(newCmd.PersistentFlags().String("endpoint.text.address", "127.0.0.1:9119", "host:port to bind the text endpoint to"))
c.configCollection.Endpoint().Metric().SetAddress(newCmd.PersistentFlags().String("endpoint.metric.address", "127.0.0.1:9120", "host:port to bind the metric endpoint to"))
c.configCollection.Space().Connection().SetWeight(newCmd.PersistentFlags().Int("space.connection.weight", 0, "default weight of new connections within the connection space"))
c.configCollection.Space().Dimension().SetCount(newCmd.PersistentFlags().Int("space.dimension.count", 3, "default number of directional coordinates within the connection space"))
c.configCollection.Space().Dimension().SetDepth(newCmd.PersistentFlags().Int("space.dimension.depth", 1000000, "default size of each directional coordinate within the connection space"))
c.configCollection.Space().Peer().SetPosition(newCmd.PersistentFlags().String("space.peer.position", "0,0,0", "default position of new peers within the connection space"))
c.configCollection.Storage().Connection().SetAddress(newCmd.PersistentFlags().String("storage.connection.address", "127.0.0.1:6379", "host:port to connect to connection storage"))
c.configCollection.Storage().Connection().SetKind(newCmd.PersistentFlags().String("storage.connection.kind", "memory", "storage kind to use for persistency (e.g. redis)"))
c.configCollection.Storage().Connection().SetPrefix(newCmd.PersistentFlags().String("storage.connection.prefix", "anna", "prefix used to prepend to connection storage keys"))
c.configCollection.Storage().Feature().SetAddress(newCmd.PersistentFlags().String("storage.feature.address", "127.0.0.1:6380", "host:port to connect to feature storage"))
c.configCollection.Storage().Feature().SetKind(newCmd.PersistentFlags().String("storage.feature.kind", "memory", "storage kind to use for persistency (e.g. redis)"))
c.configCollection.Storage().Feature().SetPrefix(newCmd.PersistentFlags().String("storage.feature.prefix", "anna", "prefix used to prepend to feature storage keys"))
c.configCollection.Storage().General().SetAddress(newCmd.PersistentFlags().String("storage.general.address", "127.0.0.1:6381", "host:port to connect to general storage"))
c.configCollection.Storage().General().SetKind(newCmd.PersistentFlags().String("storage.general.kind", "memory", "storage kind to use for persistency (e.g. redis)"))
c.configCollection.Storage().General().SetPrefix(newCmd.PersistentFlags().String("storage.general.prefix", "anna", "prefix used to prepend to general storage keys"))
c.configCollection.Storage().Peer().SetAddress(newCmd.PersistentFlags().String("storage.peer.address", "127.0.0.1:6381", "host:port to connect to peer storage"))
c.configCollection.Storage().Peer().SetKind(newCmd.PersistentFlags().String("storage.peer.kind", "memory", "storage kind to use for persistency (e.g. redis)"))
c.configCollection.Storage().Peer().SetPrefix(newCmd.PersistentFlags().String("storage.peer.prefix", "anna", "prefix used to prepend to peer storage keys"))
return newCmd
}
// ListenToSignal listens to OS signals to be catched and processed if desired.
func (c *Command) ListenToSignal() {
listener := make(chan os.Signal, 2)
signal.Notify(listener, os.Interrupt, os.Kill)
<-listener
go c.Shutdown()
<-listener
c.ForceShutdown()
}
// SetConfigCollection sets the config collection for the boot command to
// configure the neural network.
func (c *Command) SetConfigCollection(configCollection *config.Collection) {
c.configCollection = configCollection
}
// SetGitCommit sets the git commit for the version command to be displayed.
func (c *Command) SetGitCommit(gitCommit string) {
c.gitCommit = gitCommit
}
// SetGoArch sets the go architecture for the version command to be displayed.
func (c *Command) SetGoArch(goArch string) {
c.goArch = goArch
}
// SetGoOS sets the go OS for the version command to be displayed.
func (c *Command) SetGoOS(goOS string) {
c.goOS = goOS
}
// SetGoVersion sets the go version for the version command to be displayed.
func (c *Command) SetGoVersion(goVersion string) {
c.goVersion = goVersion
}
// SetProjectVersion sets the project version for the version command to be displayed.
func (c *Command) SetProjectVersion(projectVersion string) {
c.projectVersion = projectVersion
}
// Shutdown initializes the shutdown of the neural network.
func (c *Command) Shutdown() {
c.shutdownOnce.Do(func() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
c.serviceCollection.Shutdown()
wg.Done()
}()
wg.Wait()
c.ForceShutdown()
})
}