/
mgn.go
171 lines (137 loc) · 4.77 KB
/
mgn.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package instance
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/assetto-corsa-web/acweb/model"
"github.com/assetto-corsa-web/acweb/settings"
"github.com/assetto-corsa-web/acweb/util"
)
var (
instances = make([]Instance, 0)
m sync.Mutex
)
func GetAllInstances() []Instance {
return instances
}
func StartInstance(instanceName string, configuration int64, scriptBefore, scriptAfter string) error {
instanceName = util.Trim(instanceName)
scriptBefore = util.Trim(scriptBefore)
scriptAfter = util.Trim(scriptAfter)
if instanceName == "" {
return util.OpError{1, "Instance name must be set"}
}
// create log dir
if err := os.MkdirAll(os.Getenv("ACWEB_INSTANCE_LOGDIR"), 0755); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error creating server log folder")
return util.OpError{5, "Error creating server log folder"}
}
// read config
config, err := model.GetConfigurationById(configuration)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error reading configuration to start instance")
return util.OpError{2, "Error reading configuration"}
}
if err := config.Join(); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error joining entities to configuration to start instance")
return util.OpError{2, "Error reading configuration"}
}
// read settings
s := settings.GetSettings()
// write config
iniServerCfg, iniEntryList, err := writeConfig(config)
if err != nil {
return util.OpError{3, "Error writing configuration"}
}
// create log file
now := time.Now().Format("20060102_150405")
logName := now + "_" + int64ToStr(config.Id) + "_" + instanceName + ".log"
logfile, err := os.Create(filepath.Join(os.Getenv("ACWEB_INSTANCE_LOGDIR"), logName))
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error creating log file")
return util.OpError{6, "Error creating log file"}
}
// run script before server start (without process ID)
if scriptBefore != "" {
runScript(scriptBefore, 0, logfile)
}
// force server_cfg and entry_list ini paths
cmd := exec.Command(filepath.Join(s.Folder, s.Executable), "-c", iniServerCfg, "-e", iniEntryList)
cmd.Stdout = logfile
cmd.Stderr = logfile
cmd.Dir = s.Folder // run acServer from its folder so track and car data will be read for checksum
if err := cmd.Start(); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error starting instance")
return util.OpError{4, "Error starting instance"}
}
// run script after server start (with process ID)
if scriptAfter != "" {
runScript(scriptAfter, cmd.Process.Pid, logfile)
}
// add instance to list of running instances
instance := Instance{cmd.Process.Pid, instanceName, config.Id, cmd, logfile}
log.WithFields(log.Fields{"instance": instance}).Debug("Adding new instance")
m.Lock()
instances = append(instances, instance)
m.Unlock()
go observeProcess(cmd)
return nil
}
func runScript(scriptPath string, processId int, logfile *os.File) {
// pass the process ID of the server instance (or 0 if not set) and log output to the log file
cmd := exec.Command(scriptPath, strconv.Itoa(processId))
cmd.Stdout = logfile
cmd.Stderr = logfile
cmd.Dir = filepath.Dir(scriptPath)
if err := cmd.Start(); err != nil {
log.WithFields(log.Fields{"err": err, "script_path": scriptPath, "process_id": processId}).Error("Error starting script")
} else {
log.WithFields(log.Fields{"script_path": scriptPath, "process_id": processId}).Debug("Run script")
}
}
func observeProcess(cmd *exec.Cmd) {
if err := cmd.Wait(); err != nil {
exitErr, ok := err.(*exec.ExitError)
if !ok {
log.WithFields(log.Fields{"err": err}).Error("Error when instance stopped")
} else {
log.WithFields(log.Fields{"err": exitErr.Error(), "process_state": exitErr.ProcessState}).Error("Error when instance stopped")
}
}
// remove process
m.Lock()
defer m.Unlock()
for i, instance := range instances {
if instance.PID == cmd.Process.Pid {
if err := instance.File.Close(); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error closing log file")
}
instances = append(instances[:i], instances[i+1:]...)
log.WithFields(log.Fields{"instance": i, "pid": instance.PID}).Info("Instance removed")
return
}
}
}
func StopInstance(pid int) error {
m.Lock()
defer m.Unlock()
for _, instance := range instances {
if instance.PID == pid {
// instance is removed from instances by observeProcess
return stopProcess(&instance)
}
}
return util.OpError{2, "Instance not found"}
}
func stopProcess(instance *Instance) error {
// just kill it
if err := instance.Cmd.Process.Kill(); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error when stopping instance")
return util.OpError{1, "Error stopping instance"}
}
return nil
}