-
Notifications
You must be signed in to change notification settings - Fork 88
/
apm.go
202 lines (163 loc) · 5.93 KB
/
apm.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
APM is a lightweight process manager written in Golang for Golang applications. It helps you keep all of your applications alive forever, if you want to. You can also reload, start, stop, delete and query status on the fly.
APM also provide a way to start a process by compiling a Golang project source code.
The main APM module is the Master module, it's the glue that keep everything running as it should be.
If you need to use the remote version of APM, take a look at RemoteMaster on Master package.
To use the remote version of APM, use:
- remoteServer := master.StartRemoteMasterServer(dsn, configFile)
It will start a remote master and return the instance.
To make remote requests, use the Remote Client by instantiating using:
- remoteClient, err := master.StartRemoteClient(dsn, timeout)
It will start the remote client and return the instance so you can use to initiate requests, such as:
- remoteClient.StartGoBin(sourcePath, name, keepAlive, args)
*/
package main
import "github.com/kardianos/osext"
import "gopkg.in/alecthomas/kingpin.v2"
import "github.com/topfreegames/apm/lib/cli"
import "github.com/topfreegames/apm/lib/master"
import "github.com/sevlyar/go-daemon"
import "path"
import "path/filepath"
import "syscall"
import "os"
import "os/signal"
import log "github.com/Sirupsen/logrus"
var (
app = kingpin.New("apm", "Aguia Process Manager.")
dns = app.Flag("dns", "TCP Dns host.").Default(":9876").String()
timeout = app.Flag("timeout", "Timeout to connect to client").Default("30s").Duration()
serveStop = app.Command("serve-stop", "Stop APM server instance.")
serveStopConfigFile = serveStop.Flag("config-file", "Config file location").String()
serve = app.Command("serve", "Create APM server instance.")
serveConfigFile = serve.Flag("config-file", "Config file location").String()
resurrect = app.Command("resurrect", "Resurrect all previously save processes.")
bin = app.Command("bin", "Create bin process.")
binSourcePath = bin.Flag("source", "Go project source path. (Ex: github.com/topfreegames/apm)").Required().String()
binName = bin.Arg("name", "Process name.").Required().String()
binKeepAlive = bin.Flag("keep-alive", "Keep process alive forever.").Required().Bool()
binArgs = bin.Flag("args", "External args.").Strings()
restart = app.Command("restart", "Restart a process.")
restartName = restart.Arg("name", "Process name.").Required().String()
start = app.Command("start", "Start a process.")
startName = start.Arg("name", "Process name.").Required().String()
stop = app.Command("stop", "Stop a process.")
stopName = stop.Arg("name", "Process name.").Required().String()
delete = app.Command("delete", "Delete a process.")
deleteName = delete.Arg("name", "Process name.").Required().String()
save = app.Command("save", "Save a list of processes onto a file.")
status = app.Command("status", "Get APM status.")
)
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case serveStop.FullCommand():
stopRemoteMasterServer()
case serve.FullCommand():
startRemoteMasterServer()
case resurrect.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.Resurrect()
case bin.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.StartGoBin(*binSourcePath, *binName, *binKeepAlive, *binArgs)
case restart.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.RestartProcess(*restartName)
case start.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.StartProcess(*startName)
case stop.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.StopProcess(*stopName)
case delete.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.DeleteProcess(*deleteName)
case save.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.Save()
case status.FullCommand():
cli := cli.InitCli(*dns, *timeout)
cli.Status()
}
}
func isDaemonRunning(ctx *daemon.Context) (bool, *os.Process, error) {
d, err := ctx.Search()
if err != nil {
return false, d, err
}
if err := d.Signal(syscall.Signal(0)); err != nil {
return false, d, err
}
return true, d, nil
}
func startRemoteMasterServer() {
if *serveConfigFile == "" {
folderPath, err := osext.ExecutableFolder()
if err != nil {
log.Fatal(err)
}
*serveConfigFile = folderPath + "/.apmenv/config.toml"
os.MkdirAll(path.Dir(*serveConfigFile), 0777)
}
ctx := &daemon.Context{
PidFileName: path.Join(filepath.Dir(*serveConfigFile), "main.pid"),
PidFilePerm: 0644,
LogFileName: path.Join(filepath.Dir(*serveConfigFile), "main.log"),
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
}
if ok, _, _ := isDaemonRunning(ctx); ok {
log.Info("Server is already running.")
return
}
log.Info("Starting daemon...")
d, err := ctx.Reborn()
if err != nil {
log.Fatalf("Failed to reborn daemon due to %+v.", err)
}
if d != nil {
return
}
defer ctx.Release()
log.Info("Starting remote master server...")
remoteMaster := master.StartRemoteMasterServer(*dns, *serveConfigFile)
sigsKill := make(chan os.Signal, 1)
signal.Notify(sigsKill,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
<-sigsKill
log.Info("Received signal to stop...")
err = remoteMaster.Stop()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func stopRemoteMasterServer() {
if *serveStopConfigFile == "" {
folderPath, err := osext.ExecutableFolder()
if err != nil {
log.Fatal(err)
}
*serveStopConfigFile = folderPath + "/.apmenv/config.toml"
os.MkdirAll(path.Dir(*serveStopConfigFile), 0777)
}
ctx := &daemon.Context{
PidFileName: path.Join(filepath.Dir(*serveStopConfigFile), "main.pid"),
PidFilePerm: 0644,
LogFileName: path.Join(filepath.Dir(*serveStopConfigFile), "main.log"),
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
}
if ok, p, _ := isDaemonRunning(ctx); ok {
if err := p.Signal(syscall.Signal(syscall.SIGQUIT)); err != nil {
log.Fatalf("Failed to kill daemon %v", err)
}
} else {
ctx.Release()
log.Info("instance is not running.")
}
}