cmd/ipfs2: Ensure process exits smoothly
Matt Bell committed
Nov 9, 2014 at 00:56 UTC
cc519c47a3778a3a0b4160c980f3e5ff02913baf
1 file changed
+43
-31
cmd/ipfs2/main.go
+43
-31
@@ -28,19 +28,29 @@ const (
28
errorFormat = "ERROR: %v\n\n"
29
)
30
31
+var ofi io.WriteCloser
32
+
33
func main() {
34
args := os.Args[1:]
35
req, root := createRequest(args)
36
handleOptions(req, root)
35
- res := callCommand(req, root)
36
- outputResponse(res, root)
37
38
+ // if debugging, setup profiling.
39
if u.Debug {
39
- err := writeHeapProfileToFile()
40
+ var err error
41
+ ofi, err = os.Create("cpu.prof")
42
if err != nil {
41
- log.Critical(err)
43
+ fmt.Println(err)
44
+ return
45
}
46
+
47
+ pprof.StartCPUProfile(ofi)
48
}
49
+
50
+ res := callCommand(req, root)
51
+ outputResponse(res, root)
52
+
53
+ exit(0)
54
}
55
56
func createRequest(args []string) (cmds.Request, *cmds.Command) {
@@ -52,7 +62,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
62
options, err2 = getOptions(req, root)
63
if err2 != nil {
64
fmt.Println(err2)
55
- os.Exit(1)
65
+ exit(1)
66
}
67
}
68
@@ -88,19 +98,19 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
98
} else {
99
fmt.Println(helpText)
100
}
91
- os.Exit(1)
101
+ exit(1)
102
}
103
104
configPath, err := getConfigRoot(options)
105
if err != nil {
106
fmt.Println(err)
97
- os.Exit(1)
107
+ exit(1)
108
}
109
110
conf, err := getConfig(configPath)
111
if err != nil {
112
fmt.Println(err)
103
- os.Exit(1)
113
+ exit(1)
114
}
115
116
ctx := req.Context()
@@ -122,7 +132,7 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
132
options, err := getOptions(req, root)
133
if err != nil {
134
fmt.Println(err)
125
- os.Exit(1)
135
+ exit(1)
136
}
137
138
if help, found := options.Option("help"); found {
@@ -133,10 +143,10 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
143
} else {
144
fmt.Println(helpText)
145
}
136
- os.Exit(0)
146
+ exit(0)
147
} else if !ok {
148
fmt.Println("error: expected 'help' option to be a bool")
139
- os.Exit(1)
149
+ exit(1)
150
}
151
}
152
@@ -145,21 +155,9 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
155
u.Debug = true
156
157
u.SetAllLoggers(logging.DEBUG)
148
-
149
- // if debugging, setup profiling.
150
- if u.Debug {
151
- ofi, err := os.Create("cpu.prof")
152
- if err != nil {
153
- fmt.Println(err)
154
- return
155
- }
156
- pprof.StartCPUProfile(ofi)
157
- defer ofi.Close()
158
- defer pprof.StopCPUProfile()
159
- }
158
} else if !ok {
159
fmt.Println("error: expected 'debug' option to be a bool")
162
- os.Exit(1)
160
+ exit(1)
161
}
162
}
163
}
@@ -174,7 +172,7 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
172
options, err := getOptions(req, root)
173
if err != nil {
174
fmt.Println(err)
177
- os.Exit(1)
175
+ exit(1)
176
}
177
178
var found bool
@@ -185,7 +183,7 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
183
localBool, ok = local.(bool)
184
if !ok {
185
fmt.Println("error: expected 'local' option to be a bool")
188
- os.Exit(1)
186
+ exit(1)
187
}
188
}
189
@@ -193,13 +191,13 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
191
addr, err := ma.NewMultiaddr(req.Context().Config.Addresses.API)
192
if err != nil {
193
fmt.Println(err)
196
- os.Exit(1)
194
+ exit(1)
195
}
196
197
_, host, err := manet.DialArgs(addr)
198
if err != nil {
199
fmt.Println(err)
202
- os.Exit(1)
200
+ exit(1)
201
}
202
203
client := cmdsHttp.NewClient(host)
@@ -207,14 +205,14 @@ func callCommand(req cmds.Request, root *cmds.Command) cmds.Response {
205
res, err = client.Send(req)
206
if err != nil {
207
fmt.Println(err)
210
- os.Exit(1)
208
+ exit(1)
209
}
210
211
} else {
212
node, err := core.NewIpfsNode(req.Context().Config, false)
213
if err != nil {
214
fmt.Println(err)
217
- os.Exit(1)
215
+ exit(1)
216
}
217
defer node.Close()
218
req.Context().Node = node
@@ -239,7 +237,7 @@ func outputResponse(res cmds.Response, root *cmds.Command) {
237
}
238
}
239
242
- os.Exit(1)
240
+ exit(1)
241
}
242
243
out, err := res.Reader()
@@ -300,3 +298,17 @@ func writeHeapProfileToFile() error {
298
defer mprof.Close()
299
return pprof.WriteHeapProfile(mprof)
300
}
301
+
302
+func exit(code int) {
303
+ if u.Debug {
304
+ pprof.StopCPUProfile()
305
+ ofi.Close()
306
+
307
+ err := writeHeapProfileToFile()
308
+ if err != nil {
309
+ log.Critical(err)
310
+ }
311
+ }
312
+
313
+ os.Exit(code)
314
+}