File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ func (c *GameController) Run() {
71
71
c .Engine .UiProtocol = c .Engine .History [len (c .Engine .History )- 1 ].UiProtocol
72
72
}
73
73
}
74
+ c .historyPreserver .CloseOnExit ()
74
75
75
76
c .ApiServer .PublishState (* c .Engine .State , c .Engine .EngineState )
76
77
c .ApiServer .PublishUiProtocol (c .Engine .UiProtocol )
@@ -99,7 +100,6 @@ func (c *GameController) setupTimeProvider() {
99
100
100
101
// mainLoop updates several states every full second and publishes the new state
101
102
func (c * GameController ) mainLoop () {
102
- defer c .historyPreserver .Close ()
103
103
for {
104
104
time .Sleep (time .Millisecond * 10 )
105
105
Original file line number Diff line number Diff line change 6
6
"io/ioutil"
7
7
"log"
8
8
"os"
9
+ "os/signal"
10
+ "syscall"
9
11
)
10
12
11
13
const maxHistorySize = 10
@@ -32,6 +34,18 @@ func (r *HistoryPreserver) Open() error {
32
34
return nil
33
35
}
34
36
37
+ // CloseOnExit makes sure to close the file when program exists
38
+ func (r * HistoryPreserver ) CloseOnExit () {
39
+ var gracefulStop = make (chan os.Signal )
40
+ signal .Notify (gracefulStop , syscall .SIGTERM )
41
+ signal .Notify (gracefulStop , syscall .SIGINT )
42
+ go func () {
43
+ <- gracefulStop
44
+ r .Close ()
45
+ os .Exit (0 )
46
+ }()
47
+ }
48
+
35
49
// Close closes the history file
36
50
func (r * HistoryPreserver ) Close () {
37
51
if r .historyFile == nil {
You can’t perform that action at this time.
0 commit comments