-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanagement.go
More file actions
88 lines (77 loc) · 2.05 KB
/
Copy pathmanagement.go
File metadata and controls
88 lines (77 loc) · 2.05 KB
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
package server
import (
"net"
"os"
"path/filepath"
"sync"
"time"
)
// StartServer initiates the server using the current strategy (In-Memory or External)
func (h *ServerHandler) StartServer(wg *sync.WaitGroup) {
serverFilePath := filepath.Join(h.AppRootDir, h.SourceDir, h.mainFileExternalServer)
h.strategyMu.Lock()
if _, err := os.Stat(serverFilePath); err == nil && h.executionInternal {
h.log("Found existing server file, switching to External Server Mode")
h.executionInternal = false
h.strategy = newExternalStrategy(h)
if h.Store != nil {
_ = h.Store.Set(StoreKeyExternalServer, "true")
}
}
isInternal := h.executionInternal
strategy := h.strategy
h.strategyMu.Unlock()
if !isInternal {
if err := h.BeforeExternalServerStart(); err != nil {
h.log("BeforeExternalServerStart failed, aborting transition:", err)
if wg != nil {
wg.Done()
}
return
}
}
if err := strategy.Start(wg); err != nil {
h.log("StartServer error:", err)
}
}
// StopServer stops the server and waits for the port to be released.
func (h *ServerHandler) StopServer() error {
h.log("Stopping server...")
err := h.strategy.Stop()
if err != nil {
h.log("StopServer error:", err)
}
// Wait for port to be released (up to 5 seconds)
addr := ":" + h.AppPort
timeout := time.After(5 * time.Second)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-timeout:
h.log("Warning: Port", h.AppPort, "still seems occupied after timeout")
return err
case <-ticker.C:
ln, dialErr := net.Listen("tcp", addr)
if dialErr == nil {
ln.Close()
h.log("Port", h.AppPort, "is now free")
return err
}
}
}
}
func (h *ServerHandler) RestartServer() error {
return h.strategy.Restart()
}
// Restart restarts the server.
// It delegates to the current strategy's Restart method.
func (h *ServerHandler) Restart() error {
h.strategyMu.RLock()
defer h.strategyMu.RUnlock()
if h.strategy != nil {
h.log("Restarting server strategy:", h.strategy.Name())
return h.strategy.Restart()
}
return nil
}