File tree Expand file tree Collapse file tree 3 files changed +39
-3
lines changed
Filter options
Expand file tree Collapse file tree 3 files changed +39
-3
lines changed
Original file line number Diff line number Diff line change @@ -91,6 +91,23 @@ func (cmd AppCmd) Kill() {
91
91
}
92
92
}
93
93
94
+ // Gracefully terminate the app server if it's running
95
+ func (cmd * AppCmd ) Terminate () {
96
+ if cmd .Cmd != nil && (cmd .ProcessState == nil || ! cmd .ProcessState .Exited ()) {
97
+ revel .TRACE .Println ("Terminating revel server pid" , cmd .Process .Pid )
98
+ cmd .Process .Signal (os .Interrupt )
99
+ select {
100
+ case <- cmd .waitChan ():
101
+ return
102
+ case <- time .After (10 * time .Second ):
103
+ revel .TRACE .Println ("Timeout; Force kill revel server pid" , cmd .Process .Pid )
104
+ cmd .Process .Kill ()
105
+ return
106
+ }
107
+ panic ("Impossible" )
108
+ }
109
+ }
110
+
94
111
// Return a channel that is notified when Wait() returns.
95
112
func (cmd AppCmd ) waitChan () <- chan struct {} {
96
113
ch := make (chan struct {}, 1 )
Original file line number Diff line number Diff line change @@ -311,6 +311,8 @@ package main
311
311
312
312
import (
313
313
"flag"
314
+ "os"
315
+ "os/signal"
314
316
"reflect"
315
317
"github.com/revel/revel"{{range $k, $v := $.ImportPaths}}
316
318
{{$v}} "{{$k}}"{{end}}
@@ -356,6 +358,20 @@ func main() {
356
358
(*{{index $.ImportPaths .ImportPath}}.{{.StructName}})(nil),{{end}}
357
359
}
358
360
361
+ signalChannel := make(chan os.Signal, 1)
362
+ signal.Notify(signalChannel,
363
+ os.Interrupt, os.Kill)
364
+
365
+ go func() {
366
+ sig := <- signalChannel
367
+ revel.INFO.Println("Server interrupt")
368
+ switch sig {
369
+ case os.Interrupt, os.Kill:
370
+ revel.StopServer()
371
+ break
372
+ }
373
+ } ()
374
+
359
375
revel.Run(*port)
360
376
}
361
377
`
Original file line number Diff line number Diff line change @@ -120,7 +120,8 @@ func NewHarness() *Harness {
120
120
// Rebuild the Revel application and run it on the given port.
121
121
func (h * Harness ) Refresh () (err * revel.Error ) {
122
122
if h .app != nil {
123
- h .app .Kill ()
123
+ //h.app.Kill()
124
+ h .app .cmd .Terminate ()
124
125
}
125
126
126
127
revel .TRACE .Println ("Rebuild" )
@@ -176,12 +177,14 @@ func (h *Harness) Run() {
176
177
}
177
178
}()
178
179
179
- // Kill the app on signal.
180
+ // Kill the app on signal
181
+ // TODO: can we make this work if the app is being run directly instead of via the `revel run` command aka the harness?
180
182
ch := make (chan os.Signal )
181
183
signal .Notify (ch , os .Interrupt , os .Kill )
182
184
<- ch
183
185
if h .app != nil {
184
- h .app .Kill ()
186
+ //h.app.Kill()
187
+ h .app .cmd .Terminate ()
185
188
}
186
189
os .Exit (1 )
187
190
}
You can’t perform that action at this time.
0 commit comments