Skip to content

Commit 38939ad

Browse files
committed
Fix multiple window issue
1 parent cc7a6cb commit 38939ad

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

config.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,37 +72,6 @@ func readConfig(file string) (*config, error) {
7272
return &c, nil
7373
}
7474

75-
// Start starts all of the processes in a configuration.
76-
func (c *config) Start() error {
77-
var merr multiError
78-
for _, p := range c.Processes {
79-
err := p.Start(os.Stdout, os.Stderr)
80-
if err != nil {
81-
merr = append(merr, err)
82-
}
83-
}
84-
if len(merr) > 0 {
85-
c.Wait()
86-
return merr
87-
}
88-
return nil
89-
}
90-
91-
// Wait completes all of the processes in a configuration.
92-
func (c *config) Wait() error {
93-
var merr multiError
94-
for _, p := range c.Processes {
95-
err := p.Wait()
96-
if err != nil {
97-
merr = append(merr, err)
98-
}
99-
}
100-
if len(merr) > 0 {
101-
return merr
102-
}
103-
return nil
104-
}
105-
10675
// Validate checks for inconsistencies in the config.
10776
func (c *config) Validate() error {
10877
var m multiError

demon.slide

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ See [[https://golang.org/pkg/os/exec/][golang.org/pkg/os/exec]]
137137

138138

139139
* Websockets - Upgrading the connection
140-
.code socket.go /ServeHTTP/,/stdoutDone/
140+
.code socket.go /ServeHTTP/,/p\.Wait/
141141
.background media/plane_bg.png
142142

143143
: Websocket code borrowed largely from https://github.com/gorilla/websocket/tree/master/examples/command.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func main() {
6666
fmt.Scanln()
6767

6868
log.Print("Stopping server...")
69-
err = conf.Wait()
69+
err = closeProcesses()
7070
if err != nil {
7171
log.Print(err)
7272
}

process.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ import (
1212
"time"
1313
)
1414

15+
// Clone makes a copy of the process, ready to be started.
16+
func (p *process) Clone() *process {
17+
return &process{
18+
Title: p.Title,
19+
Command: p.Command,
20+
Dir: p.Dir,
21+
Position: p.Position,
22+
ExitInput: p.ExitInput,
23+
}
24+
}
25+
1526
// Start starts a process after processing environment variables in the command line
1627
// and connecting pipes for I/O.
1728
func (p *process) Start(dest, errDest io.Writer) error {
1829
var err error
1930

31+
if p.cmd != nil {
32+
return fmt.Errorf(p.Title, ": Process already started")
33+
}
34+
2035
name := p.Command[0]
2136
args := make([]string, len(p.Command)-1)
2237
copy(args, p.Command[1:])

socket.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"log"
77
"net/http"
8+
"sync"
89
"time"
910

1011
"github.com/gorilla/websocket"
@@ -87,13 +88,46 @@ func ping(ws *websocket.Conn, done chan struct{}) {
8788
}
8889
}
8990

91+
var (
92+
// Slice of processes to close on exit. In a real high-capacity site, you'd
93+
// want to manage this list more carefully. But for our purposes, it's not
94+
// very significant.
95+
procsToClose []*process
96+
97+
// This mutex protects the above slice if multiple callers are trying to use it.
98+
procsToCloseMutex sync.Mutex
99+
)
100+
101+
func closeProcesses() error {
102+
procsToCloseMutex.Lock()
103+
defer procsToCloseMutex.Unlock()
104+
105+
var merr multiError
106+
for _, p := range procsToClose {
107+
err := p.Wait()
108+
if err != nil {
109+
merr = append(merr, err)
110+
}
111+
}
112+
if len(merr) > 0 {
113+
return merr
114+
}
115+
return nil
116+
}
117+
90118
func (p *process) ServeHTTP(w http.ResponseWriter, r *http.Request) {
91119
ws, err := upgrader.Upgrade(w, r, nil)
92120
if err != nil {
93121
log.Print("upgrade: ", err)
94122
return
95123
}
96124

125+
// Clone and add to list of processes to close later.
126+
p = p.Clone()
127+
procsToCloseMutex.Lock()
128+
procsToClose = append(procsToClose, p)
129+
procsToCloseMutex.Unlock()
130+
97131
defer ws.Close()
98132

99133
rdr, wtr := io.Pipe()

0 commit comments

Comments
 (0)